Upload
others
View
23
Download
0
Embed Size (px)
Citation preview
MATLABExamples
Hans-PetterHalvorsen
DiscreteSystems
DiscreteSystems• MATLABhasbuilt-inpowerfulfeaturesforsimulationofcontinuousdifferentialequationsanddynamicsystems.
• SometimeswewanttoorneedtodiscretizeacontinuoussystemandthensimulateitinMATLAB.
• Thismeansweneedtomakeadiscreteversionofourcontinuousdifferentialequations.• Actually,thebuilt-inODEsolversinMATLABusedifferentdiscretizationmethods
DiscretizationMethods
• Euler;–Eulerforwardmethod,–Eulerbackwardmethod
• ZeroOrderHold(ZOH)• Tustin• ...
DiscreteApproximationofthetimederivativeEulerbackwardmethod:
Eulerforwardmethod:
DiscreteSystems
DiscretizationMethods
Eulerforwardmethod:
Eulerbackwardmethod:
OthermethodsareZeroOrderHold(ZOH),Tustin’smethod,etc.
Simplertouse!
MoreAccurate!
DiscreteSystems
DifferentDiscreteSymbolsandmeanings
Present Value:
Previous Value:
Next (Future)Value:
Note!DifferentNotationisusedindifferentlitterature!
DiscreteSystems
DiscreteSimulationGiventhefollowingdifferentialequation:
�̇� = 𝑎𝑥
where𝑎 = − &',where𝑇 isthetimeconstant
Note!�̇� = )*)+
FindthediscretedifferentialequationandplotthesolutionforthissystemusingMATLAB.Set𝑇 = 5andtheinitialcondition𝑥(0) = 1.CreateascriptinMATLAB(.mfile)whereweplotthesolution𝑥 𝑡 inthetimeinterval0 ≤ 𝑡 ≤ 30
DiscreteSimulationWecanusee.g.,theEulerApproximation:
�̇� ≈𝑥67& − 𝑥6
𝑇8Thenweget:
𝑥67& − 𝑥6𝑇8
= 𝑎𝑥6
Whichgives:𝑥67& = (1 + 𝑎𝑇8)𝑥6
clear, clc
% Model ParametersT = 5;a = -1/T;
% Simulation ParametersTs = 0.1; %sTstop = 30; %sx(1) = 1;
% Simulationfor k=1:(Tstop/Ts)
x(k+1) = (1+a*Ts)*x(k);end
% Plot the Simulation Resultst=0:Ts:Tstop;plot(t,x)grid on
BacteriaSimulationInthistaskwewillsimulateasimplemodelofabacteriapopulationinajar.Themodelisasfollows:
birthrate=𝑏𝑥deathrate=𝑝𝑥2
Thenthetotalrateofchangeofbacteriapopulationis:�̇� = 𝑏𝑥 − 𝑝𝑥=
Setb=1/hourandp=0.5 bacteria-hourWewillsimulatethenumberofbacteriainthejarafter1hour,assumingthatinitiallythereare100bacteriapresent.→FindthediscretemodelusingtheEulerForwardmethodbyhandandimplementandsimulatethesysteminMATLABusingaForLoop.
BacteriaSimulationWecreateadiscretemodel.anduseEulerForwarddifferentiationmethod:
�̇� ≈𝑥67& − 𝑥6
𝑇8Where𝑇8 istheSamplingTime.Weget:
𝑥67& − 𝑥6𝑇8
= 𝑏𝑥6 − 𝑝𝑥6=
Thisgives:
𝑥67& = 𝑥6 + 𝑇8(𝑏𝑥6 − 𝑝𝑥6=)
clear, clc% Model Parametersb = 1;p = 0.5;
% Simulation ParametersTs=0.01;Tstop = 1; %sx(1)=100;
% Simulation Loopfor k=1:(Tstop/Ts)
% Discrete modelx(k+1) = x(k) + Ts*(b*x(k) - p*x(k)^2);
end
% Plot the simulation resultst=0:Ts:Tstop;plot(t,x)grid on
Simulationwith2variables
Giventhefollowingsystem:
𝑑𝑥&𝑑𝑡 = −𝑥=
𝑑𝑥=𝑑𝑡 = 𝑥&
FindthediscretesystemandsimulatethediscretesysteminMATLAB.
Simulationwith2variablesUsingEuler:
�̇� ≈𝑥67& − 𝑥6
𝑇8Thenweget:
𝑥& 𝑘 + 1 = 𝑥& 𝑘 − 𝑇8𝑥= 𝑘
𝑥= 𝑘 + 1 = 𝑥= 𝑘 + 𝑇8𝑥& 𝑘
WhichwecanimplementinMATLAB.Theequationswillbesolvedinthetimespan[−11] withinitialvalues[1, 1].
clear, clc
Ts = 0.1;
Tstart = -1;Tstop = 1;
x1(1) = 1;x2(1) = 1;
for k=1:((Tstop-Tstart)/Ts)x1(k+1) = x1(k) - Ts.*x2(k);x2(k+1) = x2(k) + Ts.*x1(k);
end
t = Tstart : Ts : Tstop;plot(t,x1,t,x2)grid on
Hans-PetterHalvorsen,M.Sc.
UniversityCollegeofSoutheastNorwaywww.usn.no
E-mail:[email protected]:http://home.hit.no/~hansha/