matlab examples - differential equations - the … · matlab examples hans-petter halvorsen solving...

13
MATLAB Examples Hans-Petter Halvorsen Solving Differential Equation using ODE Solvers

Upload: duongthuan

Post on 29-Jun-2018

297 views

Category:

Documents


12 download

TRANSCRIPT

Page 1: MATLAB Examples - Differential Equations - The … · MATLAB Examples Hans-Petter Halvorsen Solving Differential Equation using ODE Solvers

MATLABExamples

Hans-PetterHalvorsen

SolvingDifferentialEquationusingODESolvers

Page 2: MATLAB Examples - Differential Equations - The … · MATLAB Examples Hans-Petter Halvorsen Solving Differential Equation using ODE Solvers

SolvingDifferentialEquationsinMATLABMATLABhavelotsofbuilt-infunctionalityforsolvingdifferentialequations.MATLABincludesfunctionsthatsolveordinarydifferentialequations(ODE)oftheform:

𝑑𝑦𝑑𝑡 = 𝑓 𝑡, 𝑦 , 𝑦 𝑡' = 𝑦'

MATLABcansolvetheseequationsnumerically.Higherorderdifferentialequationsmustbereformulatedintoasystemoffirstorderdifferentialequations.Note! Differentnotationisused:

𝑑𝑦𝑑𝑡 = 𝑦( = �̇�

Notalldifferentialequationscanbesolvedbythesametechnique,soMATLABofferslotsofdifferentODEsolversforsolvingdifferentialequations,suchasode45,ode23,ode113,etc.

Page 3: MATLAB Examples - Differential Equations - The … · MATLAB Examples Hans-Petter Halvorsen Solving Differential Equation using ODE Solvers

BacteriaPopulationInthistaskwewillsimulateasimplemodelofabacteriapopulationinajar.Themodelisasfollows:

birthrate=𝑏𝑥deathrate=𝑝𝑥2

Thenthetotalrateofchangeofbacteriapopulationis:�̇� = 𝑏𝑥 − 𝑝𝑥/

Setb=1/hourandp=0.5 bacteria-hour→Simulatethenumberofbacteriainthejarafter1hour,assumingthatinitiallythereare100bacteriapresent.

Page 4: MATLAB Examples - Differential Equations - The … · MATLAB Examples Hans-Petter Halvorsen Solving Differential Equation using ODE Solvers

function dx = bacteriadiff(t,x)% My Simple Differential Equation

b=1;p=0.5;

dx = b*x - p*x^2;

clearclc

tspan=[0 1];x0=100;

[t,y]=ode45(@bacteriadiff, tspan,x0);plot(t,y)

[t,y]

Page 5: MATLAB Examples - Differential Equations - The … · MATLAB Examples Hans-Petter Halvorsen Solving Differential Equation using ODE Solvers

PassingParameterstothemodel

Giventhefollowingsystem(1.orderdifferentialequation):�̇� = 𝑎𝑥 + 𝑏

where𝑎 = − 23,where𝑇 isthetimeconstant

Inthiscasewewanttopass𝑎 and𝑏 asparameters,tomakeiteasytobeabletochangevaluesfortheseparametersWeset𝑏 = 1Wesetinitialcondition𝑥(0) = 1 and𝑇 = 5.

SolvetheEquationandPlottheresultswithMATLAB

Page 6: MATLAB Examples - Differential Equations - The … · MATLAB Examples Hans-Petter Halvorsen Solving Differential Equation using ODE Solvers

function dx = mysimplediff(t,x,param)% My Simple Differential Equation

a=param(1);b=param(2);

dx=a*x+b;

tspan=[0 25];x0=1;a=-1/5;b=1;param=[a b];

[t,y]=ode45(@mysimplediff, tspan, x0,[], param);plot(t,y)

Bydoingthis,itisveryeasytochangesvaluesfortheparametersaandb.

Note!Weneedtousethe5.argumentintheODEsolverfunctionforthis.The4.argumentisforspecialoptionsandisnormallysetto“[]”,i.e.,nooptions.

Page 7: MATLAB Examples - Differential Equations - The … · MATLAB Examples Hans-Petter Halvorsen Solving Differential Equation using ODE Solvers

DifferentialEquation

Usetheode23functiontosolveandplottheresultsofthefollowingdifferentialequationintheinterval[𝑡', 𝑡<]:

𝑤( + 1.2 + 𝑠𝑖𝑛10𝑡 𝑤 = 0Where:

𝑡' = 0𝑡< = 5𝑤 𝑡' = 1

Page 8: MATLAB Examples - Differential Equations - The … · MATLAB Examples Hans-Petter Halvorsen Solving Differential Equation using ODE Solvers

DifferentialEquation

Westartbyrewritingthedifferentialequation:

𝑤( = − 1.2 + 𝑠𝑖𝑛10𝑡 𝑤

ThenwecanimplementitinMATLAB

Page 9: MATLAB Examples - Differential Equations - The … · MATLAB Examples Hans-Petter Halvorsen Solving Differential Equation using ODE Solvers

function dw = diff_task3(t,w)

dw = -(1.2 + sin(10*t))*w;

tspan=[0 5];w0=1;

[t,w]=ode23(@diff_task3, tspan, w0);plot(t,w)

Page 10: MATLAB Examples - Differential Equations - The … · MATLAB Examples Hans-Petter Halvorsen Solving Differential Equation using ODE Solvers

2.orderdifferentialequationUsetheode23/ode45functiontosolveandplottheresultsofthefollowingdifferentialequationintheinterval[𝑡', 𝑡<]:

1 + 𝑡/ �̈� + 2𝑡�̇� + 3𝑤 = 2

Where; , 𝑡'= 0, 𝑡< = 5,𝑤 𝑡' = 0, �̇� 𝑡' = 1Note! Higherorderdifferentialequationsmustbereformulatedintoasystemoffirstorderdifferentialequations.Tip1: Reformulatethedifferentialequationso�̈� isaloneontheleftside.Tip2:Set:

𝑤 = 𝑥2�̇� = 𝑥/

Page 11: MATLAB Examples - Differential Equations - The … · MATLAB Examples Hans-Petter Halvorsen Solving Differential Equation using ODE Solvers

2.orderdifferentialequationTip1:Firstwerewritelikethis:

�̈� =2 − 2𝑡�̇� − 3𝑤

1 + 𝑡/Tip2:InordertosolveitusingtheodefunctionsinMATLABithastobeasetwith1.orderode’s.Soweset:

𝑤 = 𝑥2�̇� = 𝑥/

Thisgives2firstorderdifferentialequations:�̇�2 = 𝑥/

�̇�/ =2 − 2𝑡𝑥/ − 3𝑥2

1 + 𝑡/

Page 12: MATLAB Examples - Differential Equations - The … · MATLAB Examples Hans-Petter Halvorsen Solving Differential Equation using ODE Solvers

function dx = diff_secondorder(t,x)

[m,n] = size(x);dx = zeros(m,n)

dx(1) = x(2);dx(2) = (2-2*t*x(2)-3*x(1))/(1+t^2);

tspan=[0 5];x0=[0; 1];

[t,x]=ode23(@diff_secondorder, tspan, x0);plot(t,x)legend('x1','x2')

tspan=[0 5];x0=[0; 1];

[t,x]=ode23(@diff_secondorder, tspan, x0);plot(t, x(:,2))

Page 13: MATLAB Examples - Differential Equations - The … · MATLAB Examples Hans-Petter Halvorsen Solving Differential Equation using ODE Solvers

Hans-PetterHalvorsen,M.Sc.

UniversityCollegeofSoutheastNorwaywww.usn.no

E-mail:[email protected]:http://home.hit.no/~hansha/