Sie sind auf Seite 1von 10

Table of Contents

Create .m files and user functions ................................................................................... 1


Ordinary Differential Equations (ODE) and Differential Algebraic Equations (DAE) ............... 2
Laplace Transforms in MATLAB ................................................................................... 4
Calculate the Laplace Transform using Matlab .................................................................. 4
Example .................................................................................................................... 4
Inverse Laplace Transform ............................................................................................ 5
PDC Example 3.1 ........................................................................................................ 5
Residue Function ........................................................................................................ 6
Roots of a polynomial .................................................................................................. 8
Unit step and impulse changes ....................................................................................... 9
Publish Results ......................................................................................................... 10

MATLAB Session 2

Create .m files and user functions


%{
Open MATLAB
% Create a .m file
% In your .m file write some code (e.g., y=1+2)
% Save it in a directory for this course
% Run your .m file
%
% *Create a function*
function dydt = dydt(t, y)
% Simulate a non-isothermal 1st-order reactor:
% material balance: dc/dt = 1 - c - xk*c
% enthalpy balance: dT/dt = 1 - T - alpha*xk*c
%
% global is used to bring the parameter values from our main code to the
% Function
global Alpha Beta
%%
% t : time
% y(1)=c : concentration
% y(2)=T : temperature
%%
% As a system of equations Matlab need to work with matrices. Howver, we
% can back convert them to their original symbols
c = y(1);
T = y(2);
%%
% Reaction rate constant
xk = Beta*T*T;
%%
% System of ODEs
dydt(1) = 1. - c - xk*c ;
dydt(2) = 1. - T - Alpha*xk*c ;
dydt = dydt';
end
%}
% Save the function as dydt

1
%

Ordinary Differential Equations (ODE) and Dif-


ferential Algebraic Equations (DAE)
ode23, ode45, ode113, ode15s, ode23s, ode23t, ode23tb Solve initial value problems for ordinary dif-
ferential equations

Syntax: [T,Y] = solver(odefun,tspan,y0) [T,Y] = solver(odefun,tspan,y0,options) [T,Y,TE,YE,IE] =


solver(odefun,tspan,y0,options) sol = solver(odefun,[t0 tf],y0...)

where solver is one of (read MATLAB Help for when they should be used): ode45, ode23, ode113,
ode15s, ode23s, ode23t, or ode23tb.

Example:

Simulate a non-isothermal 1st-order reactor:


dc/dt = 1 - c - xk*c ... material balance
dT/dt = 1 - T - alpha*xk*c ... enthalpy balance

Clear memory and variables

clear all; clc

Make Alpha and Beta model parameters global (pass to function)

global Alpha Beta

Model Parameters

Alpha = -5.;
Beta = 2.;

Initial values

y0= [1. 0.1] ;

Integration limits

t0 = 0.; % Start time


tf = 1.; % Finish time

Call ode45 to solve the system of ODEs

[T Y] = ode45(@dydt, [t0 tf], y0);

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'];

Plot the result

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

Laplace Transforms in MATLAB


Calculate the Laplace Transform using Matlab
Calculating the Laplace F(s) transform of a function f(t) is quite simple in Matlab. First you need to spe-
cify that the variable t and s are symbolic ones. This is done with the command

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)

Inverse Laplace Transform


The command one uses now is ilaplace. One also needs to define the symbols t and s. Lets calculate the
inverse of the previous function F(s),

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

PDC Example 3.1


Define symbolic variables

syms t s

Define y(t) and dy(t)/dt as symbolic functions

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

Make it readable and assign initial value

Yr=subs(Y,{'laplace(y(t), t, s)', 'y(0)'}, {'Ys',1});


pretty(Yr)

2
4 Ys + 5 Ys s - - - 5
s

Ys is symbolic

Syms Ys

Solve for Ys

Ys=solve(Yr,Ys);

Inverse Laplace Transform

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:

- the residues are given in output vector r,

- the poles are given in output vector p,

- the so-called direct terms are given in output vector k.

Example

6
Do the partial fraction expansion of the Laplace transform

F(s) = (6*s^2 – 12)/(s^3 + s^2 – 4*s – 4)

First Print F(s)

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

It is of course much easier to use MATLAB's tf

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

Take the inverse Laplace transform of Fs

ft=ilaplace(Fs);
pretty(ft)

2 3
------ + -------- + exp(2 t)
exp(t) exp(2 t)

Of course you could have done

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

For fun create the polynomial from its roots

qnew=poly(qroots);
pnew=poly(proots);

Test the 2 results

[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 =
[]

Unit step and impulse changes


t=0:.2:12; % time interval
tau=2; K=0.05; % time constant and gain
G=tf(K,[tau 1]); % simple transfer function
ystep=step(G,t); % step change
yimpulse=impulse(G,t); % impulse change

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

Do NOT upload m files.

Publish results as Word or pdf files.

Combine all in one document.

If you are asked to plot something it means that you have to publish

Try it

Published with MATLAB® 7.10

10

Das könnte Ihnen auch gefallen