Brief Introduction to Matlab
Brief Introduction to Matlab
Matrix +
Laboratory
https://www.mathworks.com/
1
Matlab 2020
What is Matlab
MATLAB is a mathematical and graphical software package with numerical, graphical, and
programming capabilities.
Review
Variable in Matlab
Remember!
String
Arrays and Matrices
TRY
A=[1 2 3] what is difference between “.*” and “*”
B=[1;2;3]
C=A.*B
C=A*B
Logical Conditions
if (x == 3)
disp(‘The value of x is 3.’);
elseif (x == 5)
disp(‘The value of x is 5.’);
else
disp(‘The value of x is not 3 or 5.’);
end;
% x = input('nhap gia tri x= ');
% if (x == 3)
% disp('The value of x is 3.')
% end
% a=input('Nhap gia tri a=');
% b=input('Nhap gia tri b=');
% c=input('Nhap gia tri c=');
% if (a>=b)
% if(a>=c)
% disp('gia tri lon nhat la a')
% else disp('gia tri lon nhat la c')
% end
% else
% if (b>=c) disp('gia tri lon nhat la b')
% else disp('gia tri lon nhat la c')
% end
% end;
For loops
x = 0;
for i=1:2:5 % start at 1, increment by 2
x = x+i; % end with 5.
end
x=7;
while (x > = 0)
x = x-2;
end;
This computes x = 7-2-2-2-2 = -1.
TRY!
x=7
while (x>0)
x=x+2
disp(x) How to exit ?Ctrl+C
end
How to write the functions
x = 0 : 20/300 : 20;
plot(x, h(x)), grid
MATLAB Graphics
Ctr+R and Ctr+T
Plotting
TRY
x = 0:pi/100:2*pi;
y1 = sin(x);
y2 = sin(x-0.25);
y3 = sin(x-0.5);
figure plot(x,y1,x,y2,'--',x,y3,':')
MATLAB Graphics
BASIC 2-D GRAPHS
Probably the most common form of plot is plot(x, y) where x and y are
vectors of the same length
MATLAB has a set of ‘easy-to-use’ plotting commands, all starting with the
string ‘ez’. The easy-to-use form of plot is ezplot
ezplot(’tan(x)’)
MATLAB Graphics
Labels
Code:
gtext(’text’)
grid /*adds/removes grid lines to/from the current graph*/
title(’text’)
xlabel(’horizontal’)
ylabel(’vertical’)
Line styles, markers and colors may be selected for a graph with a string argument to plot
plot(x, y, ’--’)
plot(x, y, ’o’)
plot(x,sin(x), x, cos(x), ’om--’)
subplot(m, n, p)
[x, y] = meshgrid(-3:0.3:3);
z = x .* exp(-x.^2 - y.^2);
subplot(2,2,1)
mesh(z),title(’subplot(2,2,1)’)
subplot(2,2,2)
mesh(z)
view(-37.5,70),title(’subplot(2,2,2)’)
subplot(2,2,3)
mesh(z)
view(37.5,-10),title(’subplot(2,2,3)’)
subplot(2,2,4)
mesh(z)
view(0,0),title(’subplot(2,2,4)’)
MATLAB Graphics
3-D PLOTS
The function plot3 is the 3-D version of plot. The command
t = 0:0.1:10;
x = sin(t);
y = cos(t);
z = t;
figure;
plot3(x, y, z, 'LineWidth', 2);
grid on;
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
title('3D Plot of sin(t) and cos(t)');
MATLAB Graphics
Mesh surfaces
[x y ] = meshgrid(-8 : 0.5 : 8);
r = sqrt(x.^2 + y.^2) + eps;
z = sin(r) ./ r;
mesh(z)
Contour plots
[x y] = meshgrid(-2.1:0.15:2.1, -6:0.15:6); % x- y-grids different
u = 80 * y.^2 .* exp(-x.^2 - 0.3*y.^2);
contour(u)
Visualizing vector fields
[x y] = meshgrid(-2:.2:2, -2:.2:2);
V = x.^2 + y;
dx = 2*x;
dy = dx; % dy same size as dx
dy(:,:) = 1; % now dy is same size as dx but all 1’s
contour(x, y, V), hold on
quiver(x, y, dx, dy), hold off
1. Matlab: numerical computation, symbolic
computation, programming, plotting
數值計算,符號運算,程式,畫圖
2. Simulink: Block diagram simulation
方塊圖模擬
numerical computation: eg. D=A+B*C
At Command Window:
跡數
15
num =
numerical computation 5 3
den =
partial fraction expansion of a rational function 1 6 11 6
ans =
num=[5 3] % numerator -0.6000 zero
den=conv([1 1],conv([1 2],[1 3])) %denominator ans =
-3.0000
roots(num) % zeros -2.0000 poles
roots(den) % poles -1.0000
[R,P,K]=residue(num,den) R=
% partial fraction expansion -6.0000 residues
7.0000
部份分數展開 -1.0000
P=
5s +3 5s +3
= -3.0000 poles
3 2
(s +1)(s + 2)(s +3) s +6s +11s +6 -2.0000
–6 7 –1 -1.0000
= + + K=
s +3 s +2 s +1 []
16
Programming in a filename.m (eg. demo1.m)
run
1. Press button
2. type demo1 at
command window
17
plotting: demo2.m
clear
t=-8:0.01:8; 1
Fig. 1: time response
0
tangent','sine wave')
-0.2
title('Fig. 1:
time response') -0.4
xlabel('Time(sec)') -0.6
ylabel('Disp.(cm)') -0.8
-1
-8 -6 -4 -2 0 2 4 6 8
Time(sec)
30
Matlab + Toolbox (各類工具箱)
31
3. PM DC Motors PID Control 永磁直流馬達PID控制
Automatic Control Systems by B.C. Kuo, 8th edition
37
電
機電
38
PID Control
0.6 Ku
KI = = 18638
0.5 Pu
32
θm (t)
θm (t)
try auto-tune
in Matlab ?
GA tuned ?
PSO tuned ?
4. Magnetic Ball Suspension System Control 磁浮球PID控制
linear or
nonlinears
plant ?
Stable?
nonlinear!
and
unstable!
http://www.youtube.com/watch?v=7o4TdX3o098
a + A s in ( x t+ ф)
red line yr (t) = 0.1+ 0.05sin(π t)