A blog to help electrical engineers and students to learn electrical engineering topics. Basic Electrical Engineering, Power Systems, Electrical Machines, tec.. Now special course on MATLAB for Beginners.

Class

Class

Friday, January 29, 2016

MATLAB - Variables and Operators


Variables

Like most programming languages, MATLAB allows you to create variables, and assign values to them. Variables are used to represent all data types including scalars, arrays, matrices and strings. MATLAB variables are created with an assignment statement, which creates the “variable” and assigns to it the value of the “expression” on the right hand side. 
The syntax is
variable name = a value or an expression
For example,
>> x = 5
>> y = 2*x+4 (expression can be a combination of numerical values, mathematical operators, variables, and functions).

Variable Name

A variable name should begin with a letter but may be followed by any combination of letters, digits or underscores (don’t use space or punctuation marks). MATLAB is case (upper and lower case) sensitive (upper and lowercase letters are not interchangeable).  In other words, student, Student, and STUDENT are three distinct variables.  It is useful to keep all your writing in lower case to avoid errors associated with case sensitivity.
Variables in MATLAB can have any names except keywords, MATLAB built-in function names (cos,sin, exp, sqrt, etc.) and special variables. It is a good practice to use variable names that describe the quantity they represent. Once a function name is used to define a variable, the function cannot be used. There are 20 words, called keywords, which are reserved by MATLAB for various purposes and cannot be used as variable names. (iskeyword command will display the list of al keywords.)

Special Variables

ans       A variable that has the value of the last expression that was not assigned to a specific variable
Pi         3.14159265…
i           Imaginary unit,            √-1
j           Same as i
eps       Floating-point relative precision, 2
realmin Smallest floating-point number, 2
realmax Largest floating-point number, (2-e)2
Inf       Infinity
NaN    Not-a-number

Syntax Error

The proper form of a Matlab statement is its syntax. A violation of the form is called a syntax error. Matlab's commands must be typed correctly. If they are not, then an error message is given:
>> 1 = x
??? 1 = x
      |
Error: The expression to the left of the equals sign is not a valid target for an assignment.
>> 3uˆ2
??? 3u^2
|
Error: Missing operator, comma, or semicolon.
Matlab is trying to tell the user what is wrong.  The first example is violation of assignment statement. Putting a constant on the left side of an equals sign does not fit the meaning of the assignment statement, and it is not allowed. In the second example the error is a missing multiplication operator *.

Commands on Variables

There are commands that can be used to eliminate variables or to obtain information about variables that have been created. You can type who to see a summary of the names of your currently defined variables. The whos command displays a list of the variables currently in the memory and their sizes together with information about their bytes and class. The command whos shows information about all defined variables, but it does not show the values of the variables. To see the value of a variable, simply type the name of the variable and press ENTER or RETURN. To clear all defined variables, type clear or clear all. You can also type, for example, clear x y to clear only x and y.

Change the Display Format

The default format for displaying the numerical values in Matlab is using the four decimal values. The user can change the format in which Matlab displays output on the screen. The format command is used to change the display format. The two main formats are short and long. Short is the format with four decimal points. The long format shows the values with 15 decimal values.

Operators

Expressions use familiar arithmetic operators and precedence rules.
+          Addition
-           Subtraction
*          Multiplication
/           Division
\           Left division (mostly used for operations with arrays, we will discuss it with another post)
^          Power 
'           Complex conjugate transpose
( )         Specify evaluation order

Precedence of Operators

All operators in MATLAB are ranked according to their precedence.  Exponentiation has the highest precedence, followed by multiplication and division, and finally addition and subtraction.  Operations are evaluated from left to right.  Parenthesis can be used to over-ride these usual rules of precedence.
1.      ( )         Parentheses  (For nested parentheses, the innermost are executed first.)
2.      ^          Exponentiation
3.      * /        Multiplication & Division
4.      + -        Addition and Subtraction

Examples

>> a = 5+4/2
a =
    7
>> b = (2+5)/2
b =
    3.5000
>> c= 4+5/3+2
c =
    7.6667
>> d = 5^3/2
d =
   62.5000
>> e = 27^(1/3)+32^0.2
e =
     5

Other MATLAB Symbols

>>        prompt
. . .       continue statement on next line
,           separate statements and data
%         start comment which ends at end of line
;           (1)        suppress output
            (2)        used as a row separator in a matrix
:           specify range

Relational Operators

Less Than                                <
            Less Than or Equal                 <=
            Greater Than                           >
            Greater Than or Equal             >=
            Equal To                                              ==
            Not Equal To                          ~=

Logical Operators

not                   ~          % highest precedence
and                  &         % equal precedence with or
or                     |          % equal precedence with and

Reference: 

MATLAB An Introduction with Applications, Amos Gilat, JOHN WILEY & SONS, INC.
submit to reddit

0 comments:

Post a Comment