0% found this document useful (0 votes)
3 views

Brief Introduction to Matlab

MATLAB is a mathematical and graphical software package that provides numerical, graphical, and programming capabilities. The document covers various aspects of MATLAB including variables, arrays, matrices, logical conditions, loops, functions, and plotting techniques. It also briefly mentions Simulink for block diagram simulation and lists several MATLAB toolboxes available for different applications.

Uploaded by

phanvinh18102003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Brief Introduction to Matlab

MATLAB is a mathematical and graphical software package that provides numerical, graphical, and programming capabilities. The document covers various aspects of MATLAB including variables, arrays, matrices, logical conditions, loops, functions, and plotting techniques. It also briefly mentions Simulink for block diagram simulation and lists several MATLAB toolboxes available for different applications.

Uploaded by

phanvinh18102003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Brief Introduction to Matlab/Simulink

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

v = [-2 3 0 4.5 -1.5]; % length 5 row


vector.
v = v’; % transposes v.
v(1); % first element of v.
v(2:4); % entries 2-4 of v.
v([3,5]); % returns entries 3 & 5.
v=[4:-1:2]; % same as v=[4 3 2];
a=1:3; b=2:3; c=[a b];  c = [1 2 3 2 3];
Arrays and Matrices (2)

x = linspace(-pi,pi,10); % creates 10 linearly-spaced


elements from –pi to pi.
logspace is similar.
A = [1 2 3; 4 5 6]; % creates 2x3 matrix
A(1,2) % the element in row 1, column 2.
A(:,2) % the second column.
A(2,:) % the second row.
Arrays and Matrices (3)

A+B, A-B, 2*A, A*B % matrix addition, matrix subtraction,


scalar multiplication, matrix multiplication
A.*B % element-by-element mult.
A’ % transpose of A (complex-
conjugate transpose)
det(A) % determinant of A

TRY
A=[1 2 3] what is difference between “.*” and “*”
B=[1;2;3]
C=A.*B
C=A*B
Logical Conditions

==, <, >, <=, >=, ~= (not equal), ~ (not)


& (element-wise logical and), | (or)
find(‘condition’) – Return indices of A’s elements that satisfies the condition.
Example: A = [7 6 5; 4 3 2];
find (‘A == 3’); --> returns 5.
More matrix/vector operations

length(v) % determine length of vector.


size(A) % determine size of matrix.
rank(A) % determine rank of matrix.
norm(A), norm(A,1), norm(A,inf)
% determine 2-norm, 1-norm,
and infinity-norm of A.
• norm(v) % compute vector 2-norm.
If statements

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

This computes x = 0+1+3+5=9.


While loops

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

The Syntax: Simple function


How to write the functions
The Syntax: Function with arguments and two or more return values
PROGRAMMING MATLAB FUNCTIONS

To represent h(t) at the command line by creating an inline


object as follows

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

Code: WDY see?


x = 0:pi/40:4*pi;
plot(x, sin(x)) plot([0 4], [1 3])

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’)

Multiple plots on the same axes


1.The easiest way is simply to use hold to keep the current plot on the axes.
2. second way: plot(x1, y1, x2, y2, x3, y3, ... ) must same lengths
3. plotyy(x,sin(x), x, 10*cos(x))
MATLAB Graphics
Line styles, markers and color

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--’)

Note: The available colors are denoted by the symbols c, m, y, k, r, g, b, w


MATLAB Graphics
Multiple plots in a figure: subplot
You can show a number of plots in the same figure window with the subplot function

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:

跡數

results are not shown on screen.


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

y1=tanh(t); hyperbolic tangent


0.8 sine wave
y2=sin(0.5*t);
0.6
plot(t,y1,'r-',t,y2,'b-.')
0.4
grid
0.2
legend('hyperbolic
Disp.(cm)

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 (各類工具箱)

1. Control System Toolbox


2. Fuzzy Logic Toolbox
3. Image Processing Toolbox
4. Neural Network Toolbox
5. System Identification
Toolbox

31
3. PM DC Motors PID Control 永磁直流馬達PID控制
Automatic Control Systems by B.C. Kuo, 8th edition

37

機電

38
PID Control

So Ku=1320.2, Pu=0.085. Now PID controller parameters are


K p = 0.6 Ku = 792 .12

0.6 Ku
KI = = 18638
0.5 Pu

K D = 0.6 Ku * 0.125 Pu = 8.42

32
θm (t)

PID Controller : P=792.12; I=18638; D=8.42

θ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)

y(t) yellow line P=I=D=10

You might also like