Beruflich Dokumente
Kultur Dokumente
By:
Amrit Lal Saha
OUTLINE:
What is Matlab?
Matlab Screen
Variables, array, matrix, indexing
Operators (Arithmetic, relational, logical )
Matrices Operations
Equations and Polynomials
Flow Control
Use of M-File
Writing User Defined Functions
Display Facilities
THE END
WHAT IS MATLAB?
Matlab
High Level
Languages such as
C, Pascal etc.
Assembly
MATLAB is an interactive system
whose basic data is a matrix that
does not require dimensioning.
Y=X’
Y=
1
2
3
4
2-D Array
A=[1 2 3; 4 5 6; 7 8 9]
A=
1 2 3
4 5 6
7 8 9
GENERATING VECTORS FROM FUNCTIONS
zeros(M,N) MxN matrix of zeros x = zeros(1,3)
x =
0 0 0
A(-2), A(0)
A(4,2)
Error: ??? Index exceeds matrix dimensions.
OPERATORS :
ARITHMETIC:
+ addition
- subtraction
* multiplication
/ division
^ power
ELEMENT-BY-ELEMENT
.* element-by-element multiplication
./ element-by-element division
.^element-by-element power
MATRICES OPERATIONS
>>A = [1 2 3; 4 5 6; 7 8 9] >>B = [3 5 2; 5 2 8; 3 6 9]
Given A and B: A= B=
1 2 3 3 5 2
4 5 6 5 2 8
7 8 9 3 6 9
x= y= b= c= d=
1 2 3 3 4 -1 3 8 -3 0.33 0.5 -3 1 4 9
K= x^2
Error:
??? Error using ==> mpower Matrix must be square.
B=x*y
Error:
??? Error using ==> mtimes Inner matrix dimensions must agree.
OPERATORS (RELATIONAL , LOGICAL)
== Equal to
~= Not equal to
| Or operator
MATRICES OPERATIONS
>>A = [1 2 3; 5 1 4; 3 4 -1]
A=
1 2 3
5 1 4
3 4 -1
x+y=4
In matrix form
2 3 x 1
1 1 y = 4
>> A = [2 -3 ; 1 1];
>> C = [1 ; 4];
>> format short
>> B = A/C
>> B
B=
B=
13/5
2.6
7/5
1.4
POLYNOMIALS AND ROOTS:
Finding roots of equation:
let the equation be 𝑥 2 + 5𝑥 + 6
>>coef = [1 5 6]
coef =
1 5 6
>>roots(coef)
ans =
-3.0000
-2.0000
Method 1 Method 2
>>G= tf([1,4],[1,6,9])
G=
𝒔+𝟒
𝒔𝟐 +𝟔𝒔+𝟗
>> step(G)
>> impulse(G)
FLOW CONTROL
if
for
while
break
….
CONTROL STRUCTURES:
Some Examples
If Statement Syntax if ((a>3) & (b==5))
if (Condition_1) Some Matlab Commands;
Matlab Commands end
elseif (Condition_2) if (a<3)
Matlab Commands
Some Matlab Commands;
elseif (Condition_3) elseif (b~=5)
Matlab Commands Some Matlab Commands;
end
else
Matlab Commands
if (a<3)
end Some Matlab Commands;
else
Some Matlab Commands;
end
CONTROL STRUCTURES
Some Examples
For loop syntax for i=1:100
Some Matlab Commands;
end
for i = Index_Array
for j=1:3:200
Some Matlab Commands;
Matlab Commands end
for m=13:-0.2:-21
end Some Matlab Commands;
end
while (condition)
Matlab Commands
end
Example
Click to create
a new M-File
• Extension “.m”
• A text file containing script or function or program to
run.
Use of M-File Save file as Untitled.m
Same Name
WRITING USER DEFINED FUNCTIONS
Another function which takes an
input array and returns the sum The function sumprod(.) can be called
and product of its elements as from command window or an m-file as
Outputs.
PLOT THE FUNCTION SIN(X) BETWEEN
0≤X≤4Π
Create an x-array of 100 samples between 0 and 4π.
0.8
0.4
0.2
>>y=sin(x); 0
-0.2
-0.4
-0.6
>>plot(y) -0.8
-1
0 10 20 30 40 50 60 70 80 90 100
GRAPH PLOTTING:
Plot command is used to produce graphs.
Syntax : plot(xvalues, yvalues, ’style option’)
>>y2=y*y1;
PLOT THE FUNCTION E-X/3SIN(X) BETWEEN
0≤X≤4Π
Multiply the arrays y and y1 correctly
>>y2=y.*y1; 0.7
0.6
0.5
0.3
>>plot(y2)
0.2
0.1
-0.1
-0.2
-0.3
0 10 20 30 40 50 60 70 80 90 100
DISPLAY FACILITIES
plot(.)
Example:
>>x=linspace(0,4*pi,100);
>>y=sin(x);
>>plot(x,y)
stem(.)
Example:
>>stem(y)
DISPLAY FACILITIES
title(.)
This is the sinus function
>>title(‘This is the sinus function’) 1
0.8
xlabel(.) 0.6
0.4
0.2
>>xlabel(‘x (secs)’)
sin(x)
0
-0.2
ylabel(.) -0.4
-0.6
>>ylabel(‘sin(x)’)
-0.8
-1
0 10 20 30 40 50 60 70 80 90 100
x (secs)
PLOT THE RESPONSE OF SERIES R-L
CIRCUIT WITH R=10Ω , L=2H, V=10V,
I(0)=0
AN INTERESTING FUNCTION:
sound(y,Fs) Enter
THE END