Matlab - Basics-VSC-2024 02 01
Matlab - Basics-VSC-2024 02 01
>> a2 = [0 1+8];
>> b2 = [a2(2) 7 a];
>> c2(2,3) = 5;
>> d2 = [1 2];
>> d2(4) = 4;
‘;’ semicolon suppresses the automatic echoing of values but it
slows down the execution.
Special Values
pi: value up to 15 significant digits
Inf: infinity (such as division by 0)
NaN: Not-a-Number (division of zero by zero)
clock: current date and time in the form of a 6-element row
vector containing the year, month, day, hour, minute, and
second
date: current date as a string such as ‘22-Jan-24’
eps: epsilon is the smallest difference between two numbers
ans: stores the result of an expression
Special Values
• MATLAB includes a number of predefined special values.
These values can be used at any time without initializing
them.
• These predefined values are stored in ordinary variables.
They can be overwritten or modified by a user.
• If a new value is assigned to one of these variables, then that
new value will replace the default one in all later
calculations.
>> circ1 = 2 * pi * 10 % returns circ1 as 62.8319
>> pi = 3; % pi stored as 3
>> circ2 = 2 * pi * 10 % returns circ2 as 60
Never change the values of predefined variables.
Changing the data format
>> value = 12.345678901234567;
format short 12.3457
format long 12.34567890123457
format short e 1.2346e+001
format long e 1.234567890123457e+001
format short g 12.346
format long g 12.3456789012346
format rat 1000/81
Arithmetic operators Function Matlab syntax
+ Addition sin sin()
- Subtraction cos cos()
* Multiplication tan tan()
\ Left division sin-1 asin()
/ Right division cos-1 acos()
^ Exponentiation tan-1 atan()
x = 0, 1.5, 3, 4, 5, 7, 9, 10.
Examples:
A = 0 : 10 : 100
B = 0 : pi/10 : 2*pi
C = 1 : 10 brackets not needed in these examples
D = [1:10 15:-1:10]
Built in functions to generate vectors
linspace(a,b,n) generates a linearly spaced vector of length n from a to b.
>> A<B
>> A=1:3; B=[2;3];
ans =
0 1 0 >> A>=B
1 0 0
ans =
>> A>7 0 1 1
0 0 1
ans =
0 0 0
1 1 1
>> A>=B
ans =
1 0 1
0 1 1
Built in logical functions
all true (= 1) if all elements of a vector are true
any true (= 1) if any element of a vector is true
exist true (= 1 ) if the argument (a variable or a function) exists
arr4 =
1 1 3 4
1 1 7 8
9 10 11 12
Make following matrices using MATLAB commands
Plot y = sin(x) in the interval 0 to 2 . Select suitable number of points so that a smooth
curve is obtained. Put the x-label “angle in radians”, y-label “sin(x)”, and title “Sine wave
form”.
Plot the graph again changing the line style of graph to dashed.
Plot y1 = x and y2 = x - x3/6 + x5/240 in the same graph. Use 101 points within 0 to 2
including 0.
For the variation of x from 0 to 2 plot y1 = x; y2 = x - x3/6 + x5/240 and y3 = sin(x) in the
same graph with red, green and black coloured lines.
end
For loop exercise:
while expression
end
while loop exercise:
if expression if expression
statement
statement elseif expression
satement
end elseif expression
satement
end
if else exercise:
Exercise on functions:
1. For a = [8 9 6 7 5] and b = [4 3 3 2 2] obtain matrices c, d & e such that
they are elementwise summation, multiplication and division respectively,
of the matrices a & b. Use a function to obtain answer.