Lab 2- Overview of MATLAB and Simulink
Lab 2- Overview of MATLAB and Simulink
Faculty of Engineering
Mechatronics Department
773215130
FIRST: OVERVIEW OF MATLAB
OVERVIEW OF MATLAB
Variable Assignment. Plotting
Matrix manipulation. Conditionals and loops.
Element-wise operator. Timing.
Right and left division. Animation.
Scalar, vector and matrix functions. Polynomials.
Complex numbers. Others.
M-Files Help.
Scripts. Matlab Cheat sheet.
Functions.
Polynomials.
OVERVIEW OF MATLAB - ASSIGNMENT
Single Variable assignment.
A row vector – values are separated by spaces or comma (,).
A column vector – values are separated by semi–colon (;).
Matrices: Combination of row and column are created using the
combination of the previous rules, for each new row item put(spaces or
comma (,)) and for each now column put (semi–colon (;)). >> C = [1 2 3 ; 4 5 6 ; 7 8 9]
>> B = [10;12;14;16]
>> A = [1 2 3 4 5]
A = B =
>> v=3 C =
1 2 3 4 5 10
v=
12
3 >>> A = [1,2,3,4,5] 1 2 3
A = 14
4 5 6
1 2 3 4 5 16
7 8 9
OVERVIEW OF MATLAB - ASSIGNMENT
Linspace: If we want to construct a vector of, say, 100 elements between
0 and 2*pi.
a = linspace(0,(2*pi),100);
1 2 3 1 1 1 14 14 14
A*B 4 5 6 2 2 2 = 32 32 32
7 8 9 3 3 3 50 50 50
>> A.^2
ans =
>> c=[1 2]; 1 4 9 A^2 = A * A
>> d=[3 4]; 16 25 36
>> c*d 49 64 81
??? Error using _*_
times >> A^2
ans =
Matrix dimensions must
30 36 42
agree. 66 81 96
102 126 150
OVERVIEW OF MATLAB - MATRIX MANIPULATION
MATRICES (DIVIDE AND ELEMENT-WISE OPERATOR)
The right division: the conventional division we all day use. >> 8/2
The left division: Contrary to the right division, the left division reverse ans =
4
the division.
>> 8\2
ans =
0.2500
ans = ans =
[i] is one of the special variables used to represent complex >> c = 1+i
Number. c =
>> angle(c)*(180/pi)
ans =
45
SOLVE EQUATIONS IN MATRIX FORM
Linsolve
-j5
Task -1:
Solve for V1 and V2 ?
V1 V2
j10
2-90o 10 1.50o
>> solution
solution =
14.0000+ 8.0000i
28.0000+ 1.0000i (0.1 + j0.2)V1 – j0.2V2 = -j2
- j0.2V1 + j0.1V2 = 1.5
M-files
Script Functio
User defined commands
Collections of commands n
Executed in sequence when called Normally has input &
output
Saved with extension “.m”
Saved with extension “.m”
OVERVIEW OF MATLAB – FUNCTIONS
Script and function files.
The typical syntax for a function is to call the function using the desired input arguments.
The function will return its outputs, which should be saved in new variables
OVERVIEW OF MATLAB – FUNCTIONS
Some other useful matrix functions are the following:
OVERVIEW OF MATLAB - PLOTTING:
t = 0:0.25:7;
[X,Y] = meshgrid(-2:.2:2);
y = sin(t);
plot(t,y)
Z = X .* exp(-X.^2 - Y.^2);
title('Sine Wave as a Function of
surf(X,Y,Z)
Time')
xlabel('Time (secs)')
ylabel('Amplitude')
OVERVIEW OF MATLAB – M-FILES &
PLOTTING
Task-2:
RLC circuit
Write an m–file to plot Z, Xc and XL versus frequency for R =10, C = 100 uF, L =
0.01 H.
Solution: R = 10 C
+
V
L
–
1
Z R j L
C
OVERVIEW OF MATLAB – CONDITIONALS AND
LOOPS:
You can loop over sections of code and conditionally execute sections
using the keywords: [for, while, if, switch].
for n = 0:0.25:7
t = 0:0.25:n;
y = sin(t);
plot(t,y)
title('Sine Wave as a Function of Time')
xlabel('Time (secs)')
ylabel('Amplitude')
axis([0 7 -1 1])
pause(0.1)
end
OVERVIEW OF MATLAB - ANIMATION:
Task-3:
Animation of sine function ?
OVERVIEW OF MATLAB - POLYNOMIALS:
Polynomials as Vectors
p = [1 3 -15
-2 9];
References:
- https://www.mathworks.com/help/index.html
- http://ctms.engin.umich.edu/CTMS/index.php
ANY QUESTIONS ?!
773215130
ANY QUESTIONS ?!