MATLAB Examples - Differential Equations

2y ago
32 Views
2 Downloads
1,008.13 KB
13 Pages
Last View : 23d ago
Last Download : 3m ago
Upload by : Fiona Harless
Transcription

MATLAB ExamplesSolving Differential Equation using ODE SolversHans-Petter Halvorsen

Solving Differential Equations in MATLABMATLAB have lots of built-in functionality for solving differential equations.MATLAB includes functions that solve ordinary differential equations (ODE) ofthe form:𝑑𝑦 𝑓 𝑑, 𝑦 ,𝑦 𝑑' 𝑦'𝑑𝑑MATLAB can solve these equations numerically.Higher order differential equations must be reformulated into a system of firstorder differential equations.Note! Different notation is used:𝑑𝑦 𝑦 ( 𝑦̇𝑑𝑑Not all differential equations can be solved by the same technique, so MATLABoffers lots of different ODE solvers for solving differential equations, such asode45, ode23, ode113, etc.

Bacteria PopulationIn this task we will simulate a simple model of a bacteriapopulation in a jar.The model is as follows:birth rate 𝑏π‘₯death rate 𝑝π‘₯2Then the total rate of change of bacteria population is:π‘₯Μ‡ 𝑏π‘₯ 𝑝π‘₯ /Set b 1/hour and p 0.5 bacteria-hour Simulate the number of bacteria in the jar after 1 hour,assuming that initially there are 100 bacteria present.

function dx bacteriadiff(t,x)% My Simple Differential Equationb 1;p 0.5;dx b*x - p*x 2;clearclctspan [0 1];x0 100;[t,y] ode45(@bacteriadiff, tspan,x0);plot(t,y)[t,y]

Passing Parameters to the modelGiven the following system (1.order differential equation):π‘₯Μ‡ π‘Žπ‘₯ 𝑏2where π‘Ž ,where 𝑇 is the time constant3In this case we want to pass π‘Ž and 𝑏 as parameters, to make iteasy to be able to change values for these parametersWe set 𝑏 1We set initial condition π‘₯(0) 1 and 𝑇 5.Solve the Equation and Plot the results with MATLAB

function dx mysimplediff(t,x,param)% My Simple Differential EquationBy doing this, it is very easy to changesvalues for the parameters a and b.a param(1);b param(2);Note! We need to use the 5. argument inthe ODE solver function for this. The 4.argument is for special options and isnormally set to β€œ[]”, i.e., no options.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)

Differential EquationUse the ode23 function to solve and plot the results of thefollowing differential equation in the interval [𝑑' , 𝑑 ]:𝑀 ( 1.2 𝑠𝑖𝑛10𝑑 𝑀 0Where:𝑑' 0𝑑 5𝑀 𝑑' 1

Differential EquationWe start by rewriting the differential equation:𝑀 ( 1.2 𝑠𝑖𝑛10𝑑 𝑀Then we can implement it in MATLAB

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.order differential equationUse the ode23/ode45 function to solve and plot the results of thefollowing differential equation in the interval [𝑑' , 𝑑 ]:1 𝑑 / π‘€Μˆ 2𝑑𝑀̇ 3𝑀 2Where; , 𝑑' 0, 𝑑 5, 𝑀 𝑑' 0, 𝑀̇ 𝑑' 1Note! Higher order differential equations must be reformulated into asystem of first order differential equations.Tip 1: Reformulate the differential equation so π‘€Μˆ is alone on the leftside.Tip 2: Set:𝑀 π‘₯2𝑀̇ π‘₯/

2.order differential equationTip1: First we rewrite like this:2 2𝑑𝑀̇ 3π‘€π‘€Μˆ 1 𝑑/Tip2: In order to solve it using the ode functions in MATLAB it hasto be a set with 1.order ode’s. So we set:𝑀 π‘₯2𝑀̇ π‘₯/This gives 2 first order differential equations:π‘₯Μ‡ 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-Petter Halvorsen, M.Sc.University College of Southeast Norwaywww.usn.noE-mail: hans.p.halvorsen@hit.noBlog: http://home.hit.no/ hansha/

Higher order differential equations must be reformulated into a system of first order differential equations. Note! Different notation is used:!"!# "( "Μ‡ Not all differential equations can be solved by the same technique, so MATLAB offers lots of different ODE solvers for solving differenti

Related Documents:

MATLAB have lots of built-in functionality for solving differential equations. MATLAB includes functions that solve ordinary differential equations (ODE) of the form: ( , ), ( 0) 0 MATLAB can solve these equations numerically. Higher order differential equations must be reformulated into a syste

MATLAB tutorial . School of Engineering . Brown University . To prepare for HW1, do sections 1-11.6 – you can do the rest later as needed . 1. What is MATLAB 2. Starting MATLAB 3. Basic MATLAB windows 4. Using the MATLAB command window 5. MATLAB help 6. MATLAB β€˜Live Scripts’ (for algebra, plotting, calculus, and solving differential .

(iii) introductory differential equations. Familiarity with the following topics is especially desirable: From basic differential equations: separable differential equations and separa-tion of variables; and solving linear, constant-coefficient differential equations using characteristic equations.

lutions of first and second order differential equations usually encountered in a differential equations course. We will then look at examples of more Examples of MATLAB solutions of differential equations will also be provided. complicated systems. 1.1 Solving an ODE Simulink is a graphical environment for designing simulations of systems.

19 MATLAB Excel Add-in Hadoop MATLAB Compiler Standalone Application MATLAB deployment targets MATLAB Compiler enables sharing MATLAB programs without integration programming MATLAB Compiler SDK provides implementation and platform flexibility for software developers MATLAB Production Server provides the most efficient development path for secure and scalable web and enterprise applications

MATLAB tutorial . School of Engineering . Brown University . To prepare for HW1, do sections 1-11.6 – you can do the rest later as needed . 1. What is MATLAB 2. Starting MATLAB 3. Basic MATLAB windows 4. Using the MATLAB command window 5. MATLAB help 6. MATLAB β€˜Live Scripts’ (for

3. MATLAB script files 4. MATLAB arrays 5. MATLAB two‐dimensional and three‐dimensional plots 6. MATLAB used‐defined functions I 7. MATLAB relational operators, conditional statements, and selection structures I 8. MATLAB relational operators, conditional statements, and selection structures II 9. MATLAB loops 10. Summary

patience and understanding during the long and comprehensive revision process. We believe you will find it was well worth the wait. Deborah E. Wilson, DrPH, CBSP L. Casey Chosewood, M.D. Director Director Division of Occupational Office of Health and Safety Health and Safety Centers for Disease Control National Institutes of Health and Prevention Bethesda, Maryland Atlanta, Georgia September .