In previous lesson we learned about MATLAB environment. In this lesson we will learn the syntax. So let’s start.
- MATLAB is just like a supper fast calculator. You can enter commands in front of these greater than symbol ( >> ) in command prompt. MATLAB can solve it immediately.
- Type a valid expression in MATLAB, for example, 5 + 5 in command window and press ENTER it immediately returns the result ans = 10 It store the answer in default variable name ans. As shown in picture.
Let us look few more examples. The example given below is according to MATLAB syntax.
- 1. 2 ^ 3 ( 2 raised to the power of 3 ) ans = 8 2.
- sin (pi / 2) ( sine of angle 90 ) ans = 1 3.
- 7/0 ( Divide by zero ) ans = Inf 4.
- 732 * 20.3 ans = 1.4860 e + 04
- MATLAB 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’.
- MATLAB retains your previous keystrokes. Use the up-arrow key to scroll back through the commands. Press the key once to see the previous entry, and so on. Edit a line using the left- and right-arrow keys the Backspace key, and the Delete key.
- Press the Enter key to execute the command.
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+5
When you click the Execute button, MATLAB executes it immediately and the result returned is: y = 8 It will not show x = 3.
Comments in MATLAB:
In MATLAB we use percent symbol ( % ) for comments. For example,
x = 9 % assign the value 9 to x
You can also write a block of comments using the block comment operators % { and % }. It start with % { and end with % }. The MATLAB editor includes tools and context menu items to help you add, remove, or change the format of comments.
Arithmetic Operations:
Here are some arithmetic operations.
+ | Addition operator |
– | 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. |
: | Generates regularly spaced elements and represents an entire row or column. |
( ) | Encloses function arguments and array indices; overrides precedence. |
[ ] | Enclosures array elements |
. | Decimal point. |
… | Ellipsis. Line-continuation operator |
, | Separates statements and elements in a row |
; | Separates columns and suppresses display. |
% | Designates a comment and specifies formatting. |
_ | Quote sign and transpose operator. |
._ | Non conjugated transpose operator. |
= | Assignment operator. |
Order of Precedence:
- First : Parentheses, evaluated starting with the innermost pair.
- Second : Exponentiation, evaluated from left to right.
- Third : Multiplication and division with equal precedence, evaluated from left to right.
- Fourth : Addition and subtraction with equal precedence, evaluated from left to right.
Examples of Precedence:
Try it yourself in MATLAB
8+3*5
ans=23
|
8+(3*5)
ans=3
|
3*4^2+5
ans=53
|
(3*4)^2+5
ans=149
|
4^2128/4*2
ans=0 |
27^(1/3)+32^0.2
ans=5
|
27^1/3+32^0.2
ans=11
|
27^(1/3)+32^(0.2)
ans=5
|
4^2128/(4*2)
ans=3 |
(8+3)*5
ans=55 |
This is the end of part 1 of Syntax section. Please share this article if you like and do not forget to comment.