Beruflich Dokumente
Kultur Dokumente
MATLAB Session 2
1
%
where solver is one of (read MATLAB Help for when they should be used): ode45, ode23, ode113,
ode15s, ode23s, ode23t, or ode23tb.
Example:
Model Parameters
Alpha = -5.;
Beta = 2.;
Initial values
Integration limits
Use loop
steps=100;
for i=1:1:steps
[Tt Yy] = ode45(@dydt, [t0 t0+tf/steps], y0);
iT(i) = Tt(end);
2
iY1(i) = Yy(end,1);
iY2(i) = Yy(end,2);
t0=t0+tf/steps;
y0=Yy(end,1:end);
end
IT=iT';
IY=[iY1',iY2'];
figure(3)
plot(T, Y,'-',IT,IY,'o')
Formated
figure(4)
p=plot(T, Y,'-',IT,IY,'o')
xlabel('t (min)')
ylabel('y or T')
title('1st-order reactor dynamics')
set(p,'LineWidth',2)
%
p =
567.0143
568.0138
569.0138
570.0138
3
clear all; clc
syms t s
Next you define the function f(t). The actual command to calculate the transform is
F=laplace(f,t,s)
To make the expression more readable one can use the commands, simplify and pretty.
Example
syms t s
f=-1.25+3.5*t*exp(-2*t)+1.25*exp(-2*t);
4
pretty(f)
F=laplace(f,t,s);
Fs=simplify(F);
pretty(Fs)
5 7 t 5
---------- + ---------- - -
4 exp(2 t) 2 exp(2 t) 4
s - 5
----------
2
s (s + 2)
syms t s
F=(s-5)/(s*(s+2)^2);
pretty(F)
f=ilaplace(F);
fs=simplify(f);
pretty(fs)
%
clear all; clc;
s - 5
----------
2
s (s + 2)
5 7 t 5
---------- + ---------- - -
4 exp(2 t) 2 exp(2 t) 4
syms t s
yt=sym('y(t)');
dytdt=diff(yt,1);
ODE is:
ODE=5*dytdt+4*yt-2;
pretty(ODE)
5
5 diff(y(t), t) + 4 y(t) - 2
Laplace transform
Y=laplace(ODE);
2
4 Ys + 5 Ys s - - - 5
s
Ys is symbolic
Syms Ys
Solve for Ys
Ys=solve(Yr,Ys);
y=ilaplace(Ys);
Make it readable
pretty(y)
1 1
------------ + -
/ 4 t \ 2
2 exp| --- |
\ 5 /
Residue Function
The “residue” function of MATLAB can be used to compute the partial fraction expansion of a ratio of
two polynomials. The “residue” command gives three pieces of information:
Example
6
Do the partial fraction expansion of the Laplace transform
Nominator=[6 0 -12];
Denominator=[1 1 -4 -4];
SymN=poly2sym(Nominator);
SymD=poly2sym(Denominator);
tfx=SymN/SymD;
tfs=subs(tfx, 's', 'x');
pretty(tfs)
2
6 s - 12
- -------------------
3 2
- s - s + 4 s + 4
Fstf=tf(Nominator,Denominator)
Transfer function:
6 s^2 - 12
-------------------
s^3 + s^2 - 4 s - 4
Then calculate the partial fraction expansion and print the result
[RN,RD,RK]=residue(Nominator,Denominator);
DD=diff(int(SymN/SymD));
Fs=subs(DD, 's', 'x');
pretty(Fs)
2 1 3
----- + ----- + -----
s + 1 s - 2 s + 2
ft=ilaplace(Fs);
pretty(ft)
2 3
------ + -------- + exp(2 t)
exp(t) exp(2 t)
7
ftt=ilaplace(SymN/SymD);
pretty(ftt)
2 3
------ + -------- + exp(2 t)
exp(t) exp(2 t)
Roots of a polynomial
Define two polynomials and find their roots
q=[1 4 3];
qroots=roots(q); % should return -3, -1
p=[1 -7 11 7 -12];
proots=roots(p); % should return 4, 3, 1, -1
qnew=poly(qroots);
pnew=poly(proots);
[a,b,k]=residue(q,p)
[anew,bnew,knew]=residue(q,p) % of course they are identical
a =
2.3333
-3.0000
0.6667
-0.0000
b =
4.0000
3.0000
1.0000
-1.0000
k =
[]
anew =
2.3333
-3.0000
0.6667
-0.0000
8
bnew =
4.0000
3.0000
1.0000
-1.0000
knew =
[]
Plot results
subplot(2,1,1); plot(t,ystep)
xlabel('t'); ylabel('y');
title('Step')
subplot(2,1,2); plot(t,yimpulse)
xlabel('t'); ylabel('y');
title('Impulse')
9
Publish Results
A few tips and rules
If you are asked to plot something it means that you have to publish
Try it
10