Home > Documents > MATLAB Examples - Differential Equations - The … · MATLAB Examples Hans-Petter Halvorsen Solving...

# MATLAB Examples - Differential Equations - The … · MATLAB Examples Hans-Petter Halvorsen Solving...

Date post: 29-Jun-2018
Category:
Upload: duongthuan
View: 285 times
Download: 12 times
Share this document with a friend
13
MATLAB Examples Hans-Petter Halvorsen Solving Differential Equation using ODE Solvers
Transcript

MATLABExamples

Hans-PetterHalvorsen

SolvingDifferentialEquationusingODESolvers

SolvingDifferentialEquationsinMATLABMATLABhavelotsofbuilt-infunctionalityforsolvingdifferentialequations.MATLABincludesfunctionsthatsolveordinarydifferentialequations(ODE)oftheform:

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

MATLABcansolvetheseequationsnumerically.Higherorderdifferentialequationsmustbereformulatedintoasystemoffirstorderdifferentialequations.Note! Differentnotationisused:

𝑑𝑦𝑑𝑡 = 𝑦( = �̇�

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

BacteriaPopulationInthistaskwewillsimulateasimplemodelofabacteriapopulationinajar.Themodelisasfollows:

birthrate=𝑏𝑥deathrate=𝑝𝑥2

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

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

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]

PassingParameterstothemodel

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

where𝑎 = − 23,where𝑇 isthetimeconstant

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

SolvetheEquationandPlottheresultswithMATLAB

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.

DifferentialEquation

Usetheode23functiontosolveandplottheresultsofthefollowingdifferentialequationintheinterval[𝑡', 𝑡<]:

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

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

DifferentialEquation

Westartbyrewritingthedifferentialequation:

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

ThenwecanimplementitinMATLAB

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)

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

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

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

𝑤 = 𝑥2�̇� = 𝑥/

2.orderdifferentialequationTip1:Firstwerewritelikethis:

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

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

𝑤 = 𝑥2�̇� = 𝑥/

Thisgives2firstorderdifferentialequations:�̇�2 = 𝑥/

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

1 + 𝑡/

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

Hans-PetterHalvorsen,M.Sc.

UniversityCollegeofSoutheastNorwaywww.usn.no

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

Recommended