Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
308 views

Matlab Simulink Tutorial

The document describes three examples for using Matlab and Simulink: 1. Plotting a trajectory profile including position, velocity, and acceleration vs. time by calculating the profiles from given equations and plotting the results. 2. Simulating a first-order dynamic system with a unit step input and the trajectory from example 1 as inputs, and viewing the outputs. 3. Designing a proportional controller to control a system using feedback of the error between a reference input and system output.

Uploaded by

richard
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
308 views

Matlab Simulink Tutorial

The document describes three examples for using Matlab and Simulink: 1. Plotting a trajectory profile including position, velocity, and acceleration vs. time by calculating the profiles from given equations and plotting the results. 2. Simulating a first-order dynamic system with a unit step input and the trajectory from example 1 as inputs, and viewing the outputs. 3. Designing a proportional controller to control a system using feedback of the error between a reference input and system output.

Uploaded by

richard
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

MTE360 Automatic Control Prepared by: M.Heng, Revised by: K.

Erkorkmaz

Tutorial #1: An Introduction to Matlab/Simulink

Example #1: Plotting a trajectory vs. time

In motion control systems, a reference trajectory describes the desired motion from
position A to position B. Taking derivatives of the trajectory corresponds to getting the
velocity, acceleration, jerk, snap and so on.

1. Given Equations 1 to 4, calculate the desired trajectory, s (t ) , velocity, s(t ) , and


acceleration, s(t ) .
2. Plot the trajectory, velocity and acceleration profiles with respect to time as in
Figure 1.
3. Save the time and trajectory profile data to a text file.

A Simple Trajectory
60
displacement

L
40
[mm]

20

0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7
100
F
[mm/sec]
feedrate

50

0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7
500 A
acceleration
[mm/sec 2]

D
-500
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7
1 2Time [msec] 3
T1 T2 T3

Figure 1. A simple trajectory with trapezoidal velocity.

Given:
Acceleration: A = 500 mm/sec2
Deceleration: D = -500 mm/sec2
Feedrate (Velocity): F = 100 mm/sec
Travel Length: L = 50 mm

1
MTE360 Automatic Control Prepared by: M.Heng, Revised by: K. Erkorkmaz

Sampling Period: Ts = 0.001 sec

F 2
2T 1 , 0 1 T1
1
FT1
s (t ) = + F2 , 0 < 2 T2 (1)
2
L FT3 + F3 F 32 , 0 < 3 T3
2 2T3
F
T1 = (2)
A
F
T3 = (3)
D
L T +T
T2 = 1 3 (4)
F 2

Solution: Matlab code (partially complete)


% file: tutorial01_example01.m

clear all % clears the workspace


close all % closes all figure windows

% Given:
A=500; % acceleration mm/sec^2
D=-500; % deceleration mm/sec^2
F=100; % feedrate (velocity) mm/sec
L=50; % travel length mm
Ts=0.001; % sampling period sec

T1=F/A; % from equation 2


T3=-F/D; % from equation 3
T2=L/F - (T1+T3)/2; % from equation 4

if T1<0 || T2<0 || T3<0 % kinematic compatibility conditions


disp('Error: acceleration, deceleration and travel length are not
kinematically compatible.');
else
% create row vector for time, initial time : step size : final time
tau1=0:Ts:T1;
% ... (do same for tau2 and tau3)

Continues on page 3.

2
MTE360 Automatic Control Prepared by: M.Heng, Revised by: K. Erkorkmaz

Continued from page 2.

% preallocate arrays for speed


s1=zeros(1,length(tau1));
sd1=zeros(1,length(tau1));
sdd1=zeros(1,length(tau1));
%... (do same for s2 and s3)

% from equation 1
for index=1:length(tau1)
s1(index)=F*tau1(index)*tau1(index)/2/T1;
sd1(index)=F*tau1(index)/T1; % first derivative: velocity
sdd1(index)=A; % second derivative: acceleration
end
for index=1:length(tau2)
s2(index)=F*tau2(index) + F*T1/2;
sd2(index)=F;
sdd2(index)=0;
end
for index=1:length(tau3)
s3(index)=-F*tau3(index)*tau3(index)/2/T3 + F*tau3(index) +
L - F*T3/2;
sd3(index)=-F*tau3(index)/T3 + F;
sdd3(index)=D;
end
end

t1=tau1'; % change tau1 from a row to a column vector tau1'


t2=tau2(2:end)' + T1; % shift bounds and drop first vector element
t3=tau3(2:end)' + T1 + T2;
t = [t1;t2;t3]; % concatenation of time vector

s1=s1';
s2=s2(2:end)';
s3=s3(2:end)';
s = [s1;s2;s3]; % concatenation of displacement vector
%... (do same for sd and sdd)

figure(1); % opens a figure window


subplot(3,1,1); % subplot(rows, columns, position)
plot(t,s); % plots trajectory versus time
title('A Simple Trajectory'); % creates a title for the plot
ylabel('displacement [mm]'); % labels the y-axis
xlabel('Time [sec]') % labels the x-axis
subplot(3,1,2);
%... (do same for sd and sdd)

% save data to file


data = [t s]; % t and s should be column vectors
save ex1_data.txt -ASCII -DOUBLE data
%... (save also sd and sdd to the same file)

%-- End of File --%

3
MTE360 Automatic Control Prepared by: M.Heng, Revised by: K. Erkorkmaz

Example #2: Simulation of a dynamic system

Given a defined set for time, t , a dynamic system can be described with a function that
receive inputs, u (t ) , and produces outputs, y (t ) .

1. Create a Simulink model with a first order system, with gain, K , and time
constant, T . Simulate a unit step input and view both the input, u (t ) , and output,
y (t ) , through a scope, as in Figure 2. Experiment with K , T , the step input and
observe how the system response, y (t ) , behaves. Save data and plot results.

Hint: In order to save your data in the Workspace as Structure with time, you need to
double click on the Scope, go to Parameters > History, and modify the data format.

2. Apply trajectory from Example 1 as the input, as in Figure 3. Save data and plot
results.

4
MTE360 Automatic Control Prepared by: M.Heng, Revised by: K. Erkorkmaz

Figure 2: A simple first order system.

Figure 3: A simple first order system with trajectory input from Example 1.

5
MTE360 Automatic Control Prepared by: M.Heng, Revised by: K. Erkorkmaz

Solution to Part 1

Figure 4: Scope of first order system simulink model.

Solution to Part 1: Matlab Code


% file: tutorial01_example02_q1.m

K=1; % set gain in workspace


T=1; % set time constant in workspace

open('fosystem.mdl'); % opens the model file


sim('fosystem.mdl'); % runs the simulation

% extract data from Scope Data struct


t = ScopeData.time;
u = ScopeData.signals.values(:,1);
y = ScopeData.signals.values(:,2);

% plot step input and output response


figure(2);
plot(t,u,t,y);
title('Step Input Response');
ylabel('Response, y');
xlabel('Time, t');
legend('Input','Output'); % creates a legend for the plot

% save data to file


data = [t u y];
%... (save as shown in Example 1)

%-- End of File --%

6
MTE360 Automatic Control Prepared by: M.Heng, Revised by: K. Erkorkmaz

Solution to Part 2: Matlab Code


% file: tutorial01_example02_q1.m

S = load('ex1_data.txt'); % loads data from file to the workspace


t = S(:,1); % extract time vector
u = S(:,3); % extract velocity profile

Tend = t(end); % get total time of simulation

K = 1; % set gain in workspace


T = 0.01; % set time constant in workspace

simin = [t u]; % array format for 1-D input signal

open('fosystem_traj.mdl'); % opens the model file


sim('fosystem_traj.mdl'); % runs the simulation

time = simout.time; % if the save format of simout is "Structure with Time"


y = simout.signals.values; % output vector

% plot input and output in the same figure


figure(3);
plot(t,u,'b-'); hold on % "hold on" retains the current plot
plot(time,y,'r-.');
title('Velocity Response');
ylabel('Response [mm/sec]');
xlabel('Time [sec]');
legend('Input','Output');

% save data to file


%... (as shown in Example 1)

%-- End of File --%

7
MTE360 Automatic Control Prepared by: M.Heng, Revised by: K. Erkorkmaz

Example #3: Design a Proportional Controller (p-controller)

A proportional controller is a simple feedback control design where the control signal, u ,
is the system error ( e = xr x ) multiplied by a gain, K p .

1. Create a Simulink model of a first order system cascaded with an integrator. The
gain should be K = 1 and time constant T =0.1 s. Simulate a square wave input
with unit amplitude and frequency of 0.3 Hz. The sample time is 0.001 sec. View
the reference position, xr (t ) , control signal, u (t ) , and actual position, x(t ) ,
through a scope, as in Figure 5. Experiment with different values of K p and
observe how the system response changes. Plot the results as in Figure 6.

Figure 5. A proportional control system.

Position Response
2
Position [mm]

0
Reference
-1
Actual
-2
5 6 7 8 9 10

20
Control signal, u

10

-10

-20
5 6 7 8 9 10
Time [sec]
Figure 6. Position response and control signal.

8
MTE360 Automatic Control Prepared by: M.Heng, Revised by: K. Erkorkmaz

Example #4: Frequency Response Function

2n
Given a transfer function: G (s) = , with n =5 rad/s, =0.1
s 2 + 2n s + 2n

Plot its frequency response as Bode and Nyquist diagrams.

Procedure (refer to textbook, Chapter 6):


1. Substitute s j , resulting in G ( s ) G ( j)
2. Evaluate complex gain ( G ( j) ) of transfer function at each frequency
3. Solve for the magnitude |G(j)| and the phase angle <G(j).

G ( j ) = Re(G ( j )) + Im(G ( j )) Equation (5)


2 2

Im G ( j )
G ( j ) = tan 1 Equation (6)
Re G ( j )

4. Use a logarithmic frequency scale from 10-1 to 102 rad/s, with 1000 points.
Magnitude and frequency should be log scales (help loglog) while Phase and
Frequency are linear and log scales (help semilogx), respectively, as shown in
Figure 7.

9
MTE360 Automatic Control Prepared by: M.Heng, Revised by: K. Erkorkmaz

Solution for Question #4


% m file to demonstrate Bode and Nyquist plots

clear;

% define 2nd order transfer function %


wn = 5; % natural frequency [rad/s]
zeta = 0.1; % damping ratio [ ]
G = tf([wn*wn],[1 2*zeta*wn wn*wn]);

% generate frequency array of interest [rad/s]%


w = logspace(-1,2,1000)'; % logarithmic, from 1e-2 to 1e2

% method 1: directly calculate frequency dependent complex gain %


% ============================================================= %
jw = j*w; % j*w vector
r2d = 180/pi; % radians to degrees conversion factor
numerator = G.num{1}; % extract numerator coefficients [0 0
25]
denominator = G.den{1}; % extract denominator coefficients [1 1
25]
Gf = polyval(numerator,jw)./polyval(denominator,jw); % evaluateu
complex gain

% generate Bode plot %


figure(1); clf; zoom on;
subplot(2,1,1); loglog(w,abs(Gf),'b');
title('Bode Plot'); ylabel('Magnitude [ ]'); grid on;
subplot(2,1,2); semilogx(w,r2d*angle(Gf),'b');
ylabel('Phase [deg]'); xlabel('Frequency [rad/s]'); grid on;

% generate Nyquist plot %


range = 1:800;
figure(2); clf;
plot3(w(range),real(Gf(range)),imag(Gf(range)),'b'); view(90,0);
xlabel('Frequency [rad/s]'); ylabel('Real{G(jw)}');
zlabel('Imag{G(jw)}');
grid on; title('Nyquist Plot');

% method 2: use matlab's built-in commands %


% ======================================== %
figure(3);
bode(G);

figure(4);
nyquist(G);

10
MTE360 Automatic Control Prepared by: M.Heng, Revised by: K. Erkorkmaz

Figure 7. Bode plot a second order system.

G(j)

|G(j)|

Figure 8. Nyquist plot a second order system


(try performing a 3D rotation of the Matlab figure!).

11
MTE360 Automatic Control Prepared by: M.Heng, Revised by: K. Erkorkmaz

For full solutions, Matlab code (.m files) and Simulink models (.mdl) will be available on
the course website.

12

You might also like