Pages

Monday, 17 September 2012

Defining Scalar Variables

 

A variable is a name made of a letter or a combination of several letters (and digits) that is assigned a numerical value. Name of variables must begin with a letter. After the first letter, any number of digits or underscores may be used.

Rules About Variable Name
· Variable name can be up to 31 characters long.
· Can contain letters, digits, and the underscore character.
· Must begin with a letter.
· MATLAB is case sensitive; it distinguishes between uppercase and lowercase letters. For example, FF, Ff, fF, and ff are the names of four different variables.
· Avoid using the names of a built-in function for a variable (i.e. avoid using: sin, tan, exp, sqrt, etc.)

Predefined Variables
A number of frequently used variables are already defined when MATLAB is started. Some of the predefined variables are:



Table : predefined Variables
Predefined Variables Description
ans A variable that has the last expression that was not assigned to a specific variable. If the user does not assign the value of an expression to a variable, MATLAB automatically stores the result in ans.
pi The π number, π = 3.14159
eps The smallest difference between two numbers. Equal to 2^(-52), which is approximately 2.2204e-016.
inf Used for infinity.
i Imaginary unit, defined as , which is 0+1.0000i.
j Same as i.
NaN Stands for Not a number. Used when MATLAB cannot determine a valid numeric value. For example 0/0.














Scalar mathematics with MATLAB notations

Scalar mathematics involves operations on single-valued variables. Matlab provides support for scalar mathematics similar to that provided by a calculator.

Arithmetic Operation with Scalars
The symbols of arithmetic operations are:
Table: Arithmetic Operators
Operation MATLAB Symbol Algebraic
form
Matlab
form
Example
Addition + a + b a + b 7+3=10
Subtraction - a b a – b 7-3=4
Multiplication * a × b a * b 7*3=21
Left-division \ b ÷ a a \ b 7\3=0.4286
Right-division / a ÷ b a / b 7/3=2.33
Exponentiation ^ ab a ^ b 7^3 (means 7^3= 343)

Order of Precedence
MATLAB executes the calculations according to the order of precedence displayed below. This order is the same as used in most calculators.
Table: Order of Precedence
Precedence Mathematical Operation
First Parenthess. For nested parenthess, the innermost are executed first.
Second Exponentiation.
Third Multiplication, division (Equal precedence).
Fourth Addition and subtraction

Numeric Display Formats
MATLAB by default displays only 4 decimals in the result of the calculations, However, MATLAB does numerical calculations in double precision, which is 15 digits.

Table: Display Format
Command Description Example


format short

Fixed-point with 4 decimal digits for:
0.001≤number≤1000 Otherwise display format short e
>> x=250/7;
>> format short
>> x
x =
35.7143





format long

Fixed-point with 15 decimal digits for:
0.001≤number≤100
Otherwise display format long e

>> x=250/7;
>> format long
>> x
x =
35.714285714285715





format short e

Scientific notation with 4 decimal digits. >> x=250/7;
>> format short e
>> x
x =
3.5714e+001





format long e

Scientific notation with 15 decimal digits. >> x=250/7;
>> format long e
>> x
x =
3.571428571428572e+001





format short g

Best of 5-digit fixed or floating point. >> x=250/7;
>> format short g
>> x
x =
35.714





format long g

Best of 15-digit fixed or floating point. >> x=250/7;
>> format long g
>> x
x = 35.7142857142857




format bank

Two decimal digits. >> x=250/7;
>> format bank
>> x
x =
35.71



format compact Suppresses many of the blank lines that appear in the output.
format loose Adds empty lines (opposite of compact)

Note: If the largest element of a matrix is larger than 103 or smaller than 10-3, MATLAB applies a common scale factor for the short and long formats.
Elementary Math Built-in Function
Lists of some commonly used elementary MATLAB mathematical built-in functions are given in TABLES.

Table: Elementary Math Functions
Function Description Example
sqrt(x) Square root; x. >> sqrt(81)
ans =
9

exp(x) Exponential; ex. >> exp(3)
ans =
20.0855

log(x) Natural logarithm; ln(x). >> log(200)
ans =
5.2983

log10(x) Common (base 10) logarithm; log(x)= log10(x). >> log10(400)
ans =
2.6021

abs(x) Absolute value. >> abs(-27)
ans =
27

factorial (x) The factorial function x!
(x must be a positive integer.)
>> factorial(4)
ans =
24


Trigonometric Math Functions:
Table : Trigonometric Math Functions
Function Description Example
sin(x) Sine; sin(x).
(x in radians)
>> sin(pi/6)
ans =
0.5000

cos(x) Cosine; cos(x).
(x in radians)
>> cos(pi/6)
ans =
0.8660

tan(x) Tangent; tan(x).
(x in radians)
>> tan(pi/6)
ans =
0.5774

cot(x) Cotangent; cot(x).
(x in radians)
>> cot(pi/6)
ans =
1.7321

sec(x) Secant; sec(x).
(x in radians)
>> sec(pi/6)
ans =
1.1547

csc(x) Cosecant; csc(x).
(x in radians)
>> csc(pi/6)
ans =
2.0000

asin(x) Inverse sine; sin–1 (x). >> asin(1)
ans =
1.5708

acos(x) Inverse cosine; cos–1 (x). >> acos(.5)
ans =
1.0472

atan(x) Inverse tangent; tan –1 (x). >> atan(.5)
ans =
0.4636

acot(x) Inverse cotangent; cot –1(x). >> acot(.5)
ans =
1.1071

asec(x) Inverse secant; sec –1 (x). >> asec(.5)
ans =
0 + 1.3170i

acsc(x) Inverse cosecant; csc –1 (x). >> acsc(.2)
ans =
1.5708 - 2.2924i


NOTE: Trigonometric functions are sind(x), cosd(x), tand(x) and others, in which x is in degree.
Table: Hyperbolic Functions
Function Description
cosh(x) Hyperbolic cosine; cosh(x).
coth(x) Hyperbolic cotangent; cosh(x)/sinh(x).
csch(x) Hyperbolic cosecant; 1/sinh(x).
sech(x) Hyperbolic secant; 1/cosh(x).
sinh(x) Hyperbolic sine; sinh(x).
tanh(x) Hyperbolic tangent; sinh(x)/cosh(x).

Rounding Functions
Table: Rounding Functions
Function Description Example
Round(x) Rounds towards the nearest integer. >> round(17/3)
ans =
6

fix(x) Rounds to the nearest integer toward zero. >> fix(13/5)
ans =
2

ceil(x) Rounds to the nearest integer toward +∞. >> ceil(9/5)
ans =
2

floor(x) Rounds to the nearest integer toward - ∞. >> floor(-9/5)
ans =
-2

sign(x) Signum function. Returns 1 if x>0, -1 if x<0, and 0 if x=0. >> sign(7)
ans =
1

rem(x,y) Returns the remainder after x is divided by y. >> rem(9,5)
ans =
4























Basic of MATLAB

  1. MATLAB Windows
  2. File Types
                               
clip_image001[14]

Types of Files

There are mainly five different types of files for storing data or programs:

a) M File

M-Files are standard ASCII text files, with a .m extension to the filename. There are two types of these files: script files and function files. Most programs user write in MATLAB Editor Window are saved as M-files..

b) Mat File

Mat-files are binary data files, with a .mat extension to the filename. Mat-files are created by MATLAB when user save data of Workspace window with the save command.

c) Figure File

Figure files are binary files with a .fig extension that can be opened again in MATLAB as figures. Such files are created by saving a figure in this format using the Save or Save As. A fig-file contains all the information required to create the figure. Such files can be open filename.fig command

d) P File

P-files are compiled M-files with a .p extension that can be executed in MATLAB. These files are created with the pcode command. If use develop an application that other people can use but user do not want give them the source code (M-files), then use give them the corresponding p-code or the p-file.

e) Mex File

Mex-files are MATLAB callable Fortran and C programs, with a .mex extension to the filename.

Notes for working in the Command Window:

1. Where to type commands?

All MATLAB commands or expression are entered in the command window at the MATLAB prompt (>>).

2. How to execute commands?

To execute a command or statement, you must press return or enter at the end.

3. What to do if the command is very long?

If a command is too long to fit in one line, it can be continued to the next line by typing three periods … (called an ellipsis) and pressing the Enter key.

4. How to name variables?

Name of variables must begin with a letter. After the first letter, any number of digits or underscores may be used.

5. What is the precision of computation?

All computations are carried out internally in double precision unless specified otherwise. The appearance of numbers on the screen, however, depends on the format in use.

6. How to control the display format of the output?

The output appearance of floating point numbers (number of digits after the decimal, etc.) is controlled with the format command.

7. How to suppress the screen output?

A semicolon (;) at the end of a command suppress the screen output, although the command is carried out and the result is saved in the variable assigned to the command or in the default variable ans.

8. How to recall previously typed commands?

- A previously typed command can be recalled to the command prompt with the up-arrow key (↑). When the command is displayed at the command prompt, it can be modified if needed and executed. The down-arrow key (↓) can be used to move down the previously typed commands.

- Double click on any command in the command history sub window to execute it in the command window.

9. What about comments?

When the symbol % (percent symbol) is typed in the beginning of a line, the line is designed as a comment. This means that when the Enter key is pressed the line is not executed. The % character followed by text (comment) can also be typed after a command (in the same line). This has no effect on the execution of the command.

Special Notes:

1. It is not possible to go back to previous line in the command window, make a correction, and then re-execute the command window.

2. Several commands can be typed in the same line. This is done by typing a comma between the commands. When the Enter key is pressed the commands are executed from left to right.

3. Once is command is typed and the Enter key is pressed, the command is executed However, only the last command is executed. Everything executed previously is unchanged.

Introduction to MATLAB


The primary developer, Professor Cleve Moler at the University of New Mexico, eventually founded Mathworks Inc. The software package has been commercially available since 1984. In industry; MATLAB is the tool of choice for high-productivity research, development, and analysis.

What is MATLAB?


  • MATLAB is a high-performance language (software package) for handling technical computation involved in scientific and engineering problems.
  • The name MATLAB stands for matrix laboratory because it provides an environment for matrix calculations.
  • MATLAB integrates computation, visualization, and programming in an easy-to-use environment where problems and solutions are expressed in familiar mathematical notation.
  • It provides an interactive environment with hundreds of built-in-functions for technical computation, graphics, and animation.

WHY MATLAB is used?


  • MATLAB is relatively easy to learn
  • MATLAB code is optimized to be relatively quick when performing matrix operations
  • MATLAB may behave like a calculator or as a programming language
  • MATLAB programs are easier to interpret, and errors are easier to fix.

MatLab can be used for:


  1. Mathematics and computation
  2. Algorithm development
  3. Data acquisition
  4. Modeling, simulation, and prototyping
  5. Data analysis, exploration, and visualization
  6. Scientific and engineering graphics
  7. Application development, including graphical user interface building
MATLAB features a family of application-specific solutions called toolboxes. Toolboxes are comprehensive collections of MATLAB functions (M-files) that extend the MATLAB environment to solve particular classes of problems. Some toolboxes are:
· Control System
· Financial
· Fuzzy Logic
· Simulink
· Symbolic Math
· Image Processing
· Neural Network
· Optimization
· Partial Differential Equation
· Signal Processing

The MATLAB System


The MATLAB system consists of five main parts:

1. Development Environment


This is a set of tools and facilities use of MATLAB functions and files. Many of these tools are graphical user interfaces. It includes the MATLAB desktop and Command Window, a command history, an editor and debugger, and browsers for viewing help, the workspace, files, and the search path.

2. The MATLAB Mathematical Function Library


This is a vast collection of computational algorithms ranging from elementary functions like sum, sine, cosine, and complex arithmetic, to more sophisticated functions like matrix inverse, matrix eigenvalues, Bessel functions, and fast Fourier transforms.

3. The MATLAB Language


This is a high-level matrix/array language with control flow statements, functions, data structures, input/output, and object-oriented programming features.

4. Graphics


MATLAB has extensive facilities for displaying vectors and matrices as graphs, as well as annotating and printing these graphs.

5. The MATLAB Application Program Interface (API).


This is a library that allows you to write C and Fortran programs that interact with MATLAB.