MATLAB environment behaves like a super-complex calculator. You can enter commands at the >> command prompt.
MATLAB is an interpreted environment. In other words, you give a command and MATLAB executes it right away.
When you click the Execute button, or type Ctrl+E, MATLAB executes it immediately and the result returned is −
For example,
The MATLAB editor includes tools and context menu items to help you add, remove, or change the format of comments.
MATLAB is case-sensitive.
Variable names can be of any length, however, MATLAB uses only first N characters, where N is given by the function namelengthmax.
For example,
MATLAB is an interpreted environment. In other words, you give a command and MATLAB executes it right away.
Hands on Practice
Type a valid expression, for example,5 + 5And press ENTER
When you click the Execute button, or type Ctrl+E, MATLAB executes it immediately and the result returned is −
ans = 10Let us take up few more examples −
3 ^ 2 % 3 raised to the power of 2When you click the Execute button, or type Ctrl+E, MATLAB executes it immediately and the result returned is −
ans = 9Another example,
sin(pi /2) % sine of angle 90oWhen you click the Execute button, or type Ctrl+E, MATLAB executes it immediately and the result returned is −
ans = 1Another example,
7/0 % Divide by zeroWhen you click the Execute button, or type Ctrl+E, MATLAB executes it immediately and the result returned is −
ans = Inf warning: division by zeroAnother example,
732 * 20.3When you click the Execute button, or type Ctrl+E, MATLAB executes it immediately and the result returned is −
ans = 1.4860e+04MATLAB provides some special expressions for some mathematical symbols, like pi for π, Inf for ∞, i (and j) for √-1 etc. Nan stands for 'not a number'.
Use of Semicolon (;) in MATLAB
Semicolon (;) indicates end of statement. However, if you want to suppress and hide the MATLAB output for an expression, add a semicolon after the expression.For example,
x = 3; y = x + 5When you click the Execute button, or type Ctrl+E, MATLAB executes it immediately and the result returned is −
y = 8
Adding Comments
The percent symbol (%) is used for indicating a comment line. For example,x = 9 % assign the value 9 to xYou can also write a block of comments using the block comment operators % { and % }.
The MATLAB editor includes tools and context menu items to help you add, remove, or change the format of comments.
Commonly used Operators and Special Characters
MATLAB supports the following commonly used operators and special characters −Operator | Purpose |
---|---|
+ | Plus; addition operator. |
- | Minus; subtraction operator. |
* | Scalar and matrix multiplication operator. |
.* | Array multiplication operator. |
^ | Scalar and matrix exponentiation operator. |
.^ | Array exponentiation operator. |
\ | Left-division operator. |
/ | Right-division operator. |
.\ | Array left-division operator. |
./ | Array right-division operator. |
: | Colon; generates regularly spaced elements and represents an entire row or column. |
( ) | Parentheses; encloses function arguments and array indices; overrides precedence. |
[ ] | Brackets; enclosures array elements. |
. | Decimal point. |
… | Ellipsis; line-continuation operator |
, | Comma; separates statements and elements in a row |
; | Semicolon; separates columns and suppresses display. |
% | Percent sign; designates a comment and specifies formatting. |
_ | Quote sign and transpose operator. |
._ | Nonconjugated transpose operator. |
= | Assignment operator. |
Special Variables and Constants
MATLAB supports the following special variables and constants:Name | Meaning |
---|---|
ans | Most recent answer. |
eps | Accuracy of floating-point precision. |
i,j | The imaginary unit √-1. |
Inf | Infinity. |
NaN | Undefined numerical result (not a number). |
pi | The number π |
Naming Variables
Variable names consist of a letter followed by any number of letters, digits or underscore.MATLAB is case-sensitive.
Variable names can be of any length, however, MATLAB uses only first N characters, where N is given by the function namelengthmax.
Saving Your Work
The save command is used for saving all the variables in the workspace, as a file with .mat extension, in the current directory.For example,
save myfile
You can reload the file anytime later using the load command.load myfile
No comments:
Post a Comment