Professional Documents
Culture Documents
Simple Oscillator (Oscillator) : Yddot Ydot y 1 S 1 S Scope
Simple Oscillator (Oscillator) : Yddot Ydot y 1 S 1 S Scope
yddot 1 ydot 1 y
s s
Scope
0.1
c/m
1
k/m
Figure 1: Simulink model of a simple oscillator with c/m = 0.1 and k/m = 1
1
2 Pendulum
Here we consider the motion of a planar pendulum subject to a constant torque u:
J θ̈ + cθ̇ + W l sin θ = u
2*sin(u)
gravity
trim(’pend’)
ans =
0.0000
0.5236
2
3 Pendulum on cart
Pendulum on cart (pendcart)
ydot y
1 1
f(u)
s s
u
thetadot theta
Mux
1 1
f(u)
s s
Simulink model.
Top function block
(u[1]-m*l*sin(u[3])*u[2]*u[2]-m*g*sin(u[3])*cos(u[3]))/(M+m*sin(u[3])2 )
3
4 Cannonball
V̇ = −g sin γ − κV 2 /m
V γ̇ = −g cos γ
and
ṗ = V cos γ
ḣ = V sin γ
where κ = ρSCD /2.
Cannonball
p
1
u[1]*cos(u[2])
s
1 XY Graph
Dynamics u[1]*sin(u[2])
s
h
V
1
-g*sin(u[2]) -kappa*u[1]^2/m
s
1
gamma
1
-g*cos(u[2])/u[1]
s
4
%cannonballpar.m
%
%Parameters for cannonball
g = 9.81
m= 1
rho=0.3809
Cd=0.4
S=0.1
kappa = rho*S*Cd/2
5
5 S-Functions
S-functions are useful when one wants to use equations to describe a dynamical system.
Recall the simple pendulum example. Here we use an S-function to describe the pendulum
dynamics.
pend2
1 sfun_pend
theta
u S-Function Scope
The following Matlab M-file, called sfun pend.m, generates a S-function for the simple
pendulum with input u and output θ. You can use this file as a template for all your
S-function M-files. You need only change the lines in the boxes.
% sfun pend.m
% S-function to describe the dynamics of the simple pendulum
% Input is a torque and output is pendulum angle
function [sys,x0,str,ts] =sfun pend(t,x,u,flag)
% t is time
% x is state
% u is input
% flag is a calling argument used by Simulink.
% The value of flag determines what Simulink wants to be executed.
switch flag
case 0 % Initialization
[sys,x0,str,ts]=mdlInitializeSizes;
6
case 9 % Not needed here
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% mdlInitializeSizes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
function [sys,x0,str,ts]=mdlInitializeSizes
%
% Create the sizes structure
sizes=simsizes;
sizes.NumDiscStates = 0;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1; % Need at least one sample time
sys = simsizes(sizes);
%
x0=[0;0]; % Set initial state
%
str=[]; % str is always an empty matrix
ts=[0 0]; % ts must be a matrix of at least one row and two columns
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% mdlDerivatives
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
function sys = mdlDerivatives(t,x,u)
%
% Compute xdot based on (t,x,u) and set it equal to sys
%
sys(1) = x(2);
sys(2) = (-2*sin(x(1))-0.4*x(2) + u)/2;
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7
% mdlOutput
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
function sys = mdlOutputs(t,x,u)
%
% Compute output based on (t,x,u) and set it equal to sys
sys = x(1);
8
6 Using ODE files in S-functions
pend3
theta
sfun_pend3 Demux
S-Function
theta-dot
The following Matlab M-file generates a S-function for the simple planar pendulum with no
input and two output variables θ and θ̇. The applied torque u is treated as a parameter.
This example also illustrates how one can use ode files in an S-function.
% sfun_pend3.m %CHANGE
% t is time
% x is state
% u is input
% flag is a calling argument used by Simulink.
% The value of flag determines what Simulink wants to be executed.
switch flag
case 0 % Initialization
[sys,x0,str,ts]=mdlInitializeSizes;
9
sys = mdlOutputs(t,x,u);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% mdlInitializeSizes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
function [sys,x0,str,ts]=mdlInitializeSizes
%
% Create the sizes structure
sizes=simsizes;
sizes.NumContStates = 2; %Set number of continuous-time state variables %CHANGE
sizes.NumDiscStates = 0;
sizes.NumOutputs = 2; %Set number of outputs %CHANGE
sizes.NumInputs = 0; %Set number of inputs %CHANGE
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1; %Need at least one sample time
sys = simsizes(sizes);
%
x0=[1;0]; % Set initial state %CHANGE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% mdlDerivatives
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
function sys = mdlDerivatives(t,x,u)
%
% Compute xdot based on (t,x,u) and set it equal to sys
%
sys= pendode(t,x); %CHANGE
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10
% mdlOutput
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
function sys = mdlOutputs(t,x,u)
%
% Compute xdot based on (t,x,u) and set it equal to sys
%pendode.m
%pendpar.m
global W l J c u
J = 2
c=0.4
W= 2
l=1;
11
7 Trim and Linearization
Recall that the simple planar pendulum is described by
J θ̈ + cθ̇ + W l sin θ = u
Linearization
Jδ θ̈ + cδ θ̇ + (W l cos θe )δθ = 0
Hence " #
0 1
A=
−W l cos θe /J −c/J
For the chosen parameters " #
0 1
A= e
− cos θ −0.2
Trim
xe=trim(’pend3’)
xe =
0.5236
-0.0000
Linearization
A=linmod(’pend3’,xe)
A =
0 1.0000
-0.8660 -0.2000
More
u=0;
xe=trim(’pend3’)
xe =
1.0e-023 *
-0.6617
-0.0009
A=linmod(’pend3’,xe)
A =
0 1.0000
-1.0000 -0.2000
12
8 Input output stuff
pend4
theta
1 sfun_pend4 1
In1 Out1
S-Function
J θ̈ + cθ̇ + W l sin θ = u
We will view this as an input output system with input u and output
y = θ.
Trim. Suppose that we wish that y = y e = π/6 = 0.5236 when the system is in equilibrium.
So, we need to compute ue and xe so that y e = 1. From the differential equation above, we
obtain
ue = W l sin(y e ) = (2)(1) sin(π/6) = 1;
also " # " #
e ye 0.5236
x = =
0 0
Using the trim command, we obtain
ue = 1.0000
ye = 0.5236
xdote =
0
0
13
Linearization. The linearized system is described by
δ ẋ1 = δx2
δ ẋ2 = −(W l cos y e /J)δx1 − (c/J)δx2 + (1/J)δu
δy = δx1
Hence,
" # " #
0 1 0 h i
A= e , B= , C= 1 0 , D = 0.
−W l cos y /J −c/J 1/J
MATLAB yields:
[A B C D] = linmod(’pend4’,xe,ue)
A =
0 1.0000
-0.8660 -0.2000
B =
0
0.5000
C =
1.0000 0
D =
0
lambda =eig(A)
lambda =
-0.1000 + 0.9252i
-0.1000 - 0.9252i
[num den]=ss2tf(A,B,C,D)
num = 0 0 0.5000
14
poles=roots(den)
poles =
-0.1000 + 0.9252i
-0.1000 - 0.9252i
zeros=roots(num)
15