MATLAB: Operators, Constants, Variables and Expressions

Leave a Comment
MATLAB

An M-File contains a set of instructions written by the user using certain words and symbols according to the syntax rules specified in MATLAB. Any MATLAB command given in the command window can be used in an M-File. MATLAB contains various features and commands that make it a widely used and preferred language. This is because
Ø  It has incredibly powerful matrix and vector notations that make the coding compact.
Ø  It invisibly handles all memory allocations and supports file input/output, and string manipulation.
Ø  It contains a large set of functions that are available
Ø  It is equipped with powerful debugging features to locate and remove errors in M-Files


DATA TYPES
There are 14 fundamental data types in MATLAB. Each of these types is in the form of an array. The array may be of a single element in size, or it may be an n-dimensional array of any size. The arrays can be of character, numeric, cell or structure data type.
CONSTANTS
Constants are fixed values that do not change during the execution process of a program. There are two types of constants –
Ø  Character Constants: Character Constants may be classified into 3 types –
1)      Single Character Constants: Contain a single character enclosed in single quotes. For example, ‘a’, ‘Y’, ‘B’ etc.
2)      String Constants: Sequence of characters enclosed within single quotes. For example, ‘Name’, ‘151211’, ’gate1’ etc.
A word containing a single quote, such as wasn’t is specified by using two single quotes. For example, ‘wasn’’t’, ‘didn’’t’ etc. Here, two single quotes are used, and not a double quote.

3)      Escape Sequence Constants: These are used in output functions. For example, \t is used to move five spaces ahead, \n is used for new line etc.

Ø  Numeric Constants: Numeric constants are classified into 3 types –
1)      Integer Constants: The set of all integers with an optional + or – sign.
2)      Real Constants: Exponential numbers or floating point constants.
3)      Complex Constants: Imaginary parts of numbers.

In MATLAB, i  =  j  =  (-1)^1/2
The real and imaginary parts of a number in MATLAB can be separated by using functions like real() and imag().
X_real = real(x);
X_imag = imag(x);
Here, the real part of the number x will be stored in the variable X_real, and the imaginary value will be stored in the variable X_imag.
For example, if x holds the value 24.0000 + 1.6000i
X_real = 24.0000
X_imag = 1.6000

In MATLAB, Cartesian and Polar co-ordinates can be converted into one another. The commands are:
[theta,r] = cart2pol(x,y)
Likewise,
[x,y]=pol2cart(theta,r)

MATLAB

Here,
theta, r à Polar Co-ordinates
x,y à Polar Co-ordinates

VARIABLES
Variables in MATLAB need not be declared in advance at the start of the program. This makes programming in MATLAB easier for the programmer. The rules for declaring a MATLAB variable are:
1)      It may consist of an alphabet as the first character, followed by a number of characters or numbers or underscore.
2)      The maximum number of characters in a MATLAB variable is 131. If this limit is exceeded, the extra characters are ignored.
3)      Variables containing blank spaces are invalid.
4)      Variables can be defined at any step in MATLAB by simply initializing them with the required value. The type of data assigned to the variable automatically decides the data type of the variable.
5)      Variable names are case sensitive. ‘ABC’ and ‘abc’ are different variable names.
6)      Certain words are reserved as keywords in MATLAB and these words cannot be used as variable names. ‘for’, ‘while’ etc. are invalid variable names as they are keywords.
OPERATORS
There are three basic types of Operators in MATLAB. They are:
Ø  Arithmetic Operators: Arithmetic Operators used in MATLAB are the results of the arithmetic operations performed with two variables, such as +, -, *, /, \, ^ etc.
Ø  Relational Operators: Relational Operators include <, >, <=, >=, ==, ~= etc. The result is ‘1’ if the result obtained is true and ‘0’ if the result obtained is false. All relational operators do not operate on complex numbers. Some operate only on the real part of the complex numbers.
Ø  Logical Operators: They can assume two values, ‘TRUE’ expressed as ‘1’ and ‘FALSE’ expressed as ‘0’. Logical operators include and (&), or (|) and not(~).

This concludes the post on Variables, Expressions, Constants and Operators. Hope you found it interesting and informative. Any doubts are welcome in the comments section.





0 comments:

Post a Comment

Powered by Blogger.