100% found this document useful (1 vote)
211 views

Matlab Function Ode45

The Matlab function ode45 numerically integrates differential equations. It takes in a function handle representing the differential equation, a time span, initial conditions, and optional settings. Ode45 returns the solution at time points, and can handle events defined by a separate function. Options and behaviors are configured using odeset.

Uploaded by

jack_hero_56
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
211 views

Matlab Function Ode45

The Matlab function ode45 numerically integrates differential equations. It takes in a function handle representing the differential equation, a time span, initial conditions, and optional settings. Ode45 returns the solution at time points, and can handle events defined by a separate function. Options and behaviors are configured using odeset.

Uploaded by

jack_hero_56
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Matlab

Function ode45
Numerical Integration of Differential Equations
Rakinder Kalsi and Katherine Lloyd


The function:

[T,Y,TE,YE,IE] = ode45(odefun,tspan,y0,options)

!"
odefun function handle to () from differential equation = ()
!"
tspan vector of interval of integration [! , ! ].
y0 initial conditions.
options structure created by function odeset.

T Time points the solution is returned for.
Y Solution ().
TE Time points at which events occur.
YE Value of () when events occur.
IE Array index of events in Y.

odeset

options = odeset('name1',value1,'name2',value2,...)

A list of options may be found on the odeset documentation.



Events

[value,isterminal,direction] = events(t,y)

value expression which = 0 when the event occurs.
isterminal takes value 1 (if integration stops at event) or 0 (if integration does not
stop.
direction Value determined by whether solution needs to be becoming more positive
(1), more negative (-1) or either (0) for the event to be recorded.


Links

ode45 Documentation: http://www.mathworks.co.uk/help/matlab/ref/ode45.html

odeset Documentation: http://www.mathworks.co.uk/help/matlab/ref/odeset.html

General Differential Equation Solving Functions:
http://www.mathworks.co.uk/help/matlab/math/ordinary-differential-
equations.html#f1-669698

Example Scripts and Functions:
http://www2.warwick.ac.uk/fac/sci/moac/people/students/2012/katherine_ll
oyd/presentations

The same equation may be passed using either using an anonymous function or a
normal function. The two examples below integrate the same differential equation using
ode45 but pass the function using the two methods.

Example using anonymous function:

% Set up anonymous function. Part on right is f(y) from equation
dy_dt=f(y)
odefun=@(t,y) -t*y/sqrt(2-y^2);

% Set up time interval vector, inital conditions and options


tspan=[0,5];
y0=1;
options = odeset('Stats','on','OutputFcn',@odeplot);

% Run ode45. Returns solution Y and timepoints T


[T,Y] = ode45(odefun,tspan,y0,options);



Example using a normal function:

function f()
% set up time interval vector, initial conditions and options
tspan=[0,5];
y0=1;
options = odeset('Stats','on','OutputFcn',@odeplot);

% Run ode45
[T,Y] = ode45(@g,tspan,y0,options);
end

function dy_dt=g(t,y)
dy_dt= -t*y/sqrt(2-y^2);
end

You might also like