Solve nonstiff differential equations — medium order method (2024)

Solve nonstiff differential equations — mediumorder method

collapse all in page

Syntax

[t,y] =ode45(odefun,tspan,y0)

[t,y] =ode45(odefun,tspan,y0,options)

[t,y,te,ye,ie]= ode45(odefun,tspan,y0,options)

sol = ode45(___)

Description

example

[t,y] =ode45(odefun,tspan,y0),where tspan = [t0 tf], integrates the system ofdifferential equations y'=f(t,y) from t0 to tf withinitial conditions y0. Each row in the solutionarray y corresponds to a value returned in columnvector t.

All MATLAB® ODE solvers can solve systems of equations ofthe form y'=f(t,y),or problems that involve a mass matrix, M(t,y)y'=f(t,y).The solvers all use similar syntaxes. The ode23s solveronly can solve problems with a mass matrix if the mass matrix is constant. ode15s and ode23t cansolve problems with a mass matrix that is singular, known as differential-algebraicequations (DAEs). Specify the mass matrix using the Mass optionof odeset.

ode45 is a versatile ODE solver and is thefirst solver you should try for most problems. However, if the problemis stiff or requires high accuracy, then there are other ODE solversthat might be better suited to the problem. See Choose an ODE Solver formore information.

example

[t,y] =ode45(odefun,tspan,y0,options) alsouses the integration settings defined by options,which is an argument created using the odeset function.For example, use the AbsTol and RelTol optionsto specify absolute and relative error tolerances, or the Mass optionto provide a mass matrix.

[t,y,te,ye,ie]= ode45(odefun,tspan,y0,options) additionallyfinds where functions of (t,y),called event functions, are zero. In the output, te isthe time of the event, ye is the solution at thetime of the event, and ie is the index of the triggeredevent.

For each event function, specify whether the integration isto terminate at a zero and whether the direction of the zero crossingmatters. Do this by setting the 'Events' propertyto a function, such as myEventFcn or @myEventFcn,and creating a corresponding function: [value,isterminal,direction]= myEventFcn(t,y).For more information, see ODE Event Location.

example

sol = ode45(___) returnsa structure that you can use with deval to evaluatethe solution at any point on the interval [t0 tf].You can use any of the input argument combinations in previous syntaxes.

Examples

collapse all

ODE with Single Solution Component

Open Live Script

Simple ODEs that have a single solution component can be specified as an anonymous function in the call to the solver. The anonymous function must accept two inputs (t,y), even if one of the inputs is not used in the function.

Solve the ODE

y=2t.

Specify a time interval of [0 5] and the initial condition y0 = 0.

tspan = [0 5];y0 = 0;[t,y] = ode45(@(t,y) 2*t, tspan, y0);

Plot the solution.

plot(t,y,'-o')

Solve nonstiff differential equations — mediumorder method (1)

Solve Nonstiff Equation

Open Live Script

The van der Pol equation is a second-order ODE

y1-μ(1-y12)y1+y1=0,

where μ>0 is a scalar parameter. Rewrite this equation as a system of first-order ODEs by making the substitution y1=y2. The resulting system of first-order ODEs is

y1=y2y2=μ(1-y12)y2-y1.

The function file vdp1.m represents the van der Pol equation using μ=1. The variables y1 and y2 are the entries y(1) and y(2) of a two-element vector dydt.

function dydt = vdp1(t,y)%VDP1 Evaluate the van der Pol ODEs for mu = 1%% See also ODE113, ODE23, ODE45.% Jacek Kierzenka and Lawrence F. Shampine% Copyright 1984-2014 The MathWorks, Inc.dydt = [y(2); (1-y(1)^2)*y(2)-y(1)];

Solve the ODE using the ode45 function on the time interval [0 20] with initial values [2 0]. The resulting output is a column vector of time points t and a solution array y. Each row in y corresponds to a time returned in the corresponding row of t. The first column of y corresponds to y1, and the second column corresponds to y2.

[t,y] = ode45(@vdp1,[0 20],[2; 0]);

Plot the solutions for y1 and y2 against t.

plot(t,y(:,1),'-o',t,y(:,2),'-o')title('Solution of van der Pol Equation (\mu = 1) with ODE45');xlabel('Time t');ylabel('Solution y');legend('y_1','y_2')

Solve nonstiff differential equations — mediumorder method (2)

Pass Extra Parameters to ODE Function

Open Live Script

ode45 works only with functions that use two input arguments, t and y. However, you can pass extra parameters by defining them outside the function and passing them in when you specify the function handle.

Solve the ODE

y=ABty.

Rewriting the equation as a first-order system yields

y1=y2y2=ABty1.

odefcn, a local function included at the end of this example, represents this system of equations as a function that accepts four input arguments: t, y, A, and B.

function dydt = odefcn(t,y,A,B) dydt = zeros(2,1); dydt(1) = y(2); dydt(2) = (A/B)*t.*y(1);end

Solve the ODE using ode45. Specify the function handle so that it passes the predefined values for A and B to odefcn.

A = 1;B = 2;tspan = [0 5];y0 = [0 0.01];[t,y] = ode45(@(t,y) odefcn(t,y,A,B), tspan, y0);

Plot the results.

plot(t,y(:,1),'-o',t,y(:,2),'-.')

Solve nonstiff differential equations — mediumorder method (3)

function dydt = odefcn(t,y,A,B) dydt = zeros(2,1); dydt(1) = y(2); dydt(2) = (A/B)*t.*y(1);end

Solve ODE with Multiple Initial Conditions

Open Live Script

For simple ODE systems with one equation, you can specify y0 as a vector containing multiple initial conditions. This technique creates a system of independent equations through scalar expansion, one for each initial value, and ode45 solves the system to produce results for each initial value.

Create an anonymous function to represent the equation f(t,y)=-2y+2cos(t)sin(2t). The function must accept two inputs for t and y.

yprime = @(t,y) -2*y + 2*cos(t).*sin(2*t);

Create a vector of different initial conditions in the range [-5,5].

y0 = -5:5; 

Solve the equation for each initial condition over the time interval [0,3] using ode45.

tspan = [0 3];[t,y] = ode45(yprime,tspan,y0);

Plot the results.

plot(t,y)grid onxlabel('t')ylabel('y')title('Solutions of y'' = -2y + 2 cos(t) sin(2t), y(0) = -5,-4,...,4,5','interpreter','latex')

This technique is useful for solving simple ODEs with several initial conditions. However, the technique also has some tradeoffs:

  • You cannot solve systems of equations with multiple initial conditions. The technique only works when solving one equation with multiple initial conditions.

  • The time step chosen by the solver at each step is based on the equation in the system that needs to take the smallest step. This means the solver can take small steps to satisfy the equation for one initial condition, but the other equations, if solved on their own, would use different step sizes. Despite this, solving for multiple initial conditions at the same time is generally faster than solving the equations separately using a for-loop.

For more information on this technique, see Solve System of ODEs with Multiple Initial Conditions.

ODE with Time-Dependent Terms

Open Script

Consider the following ODE with time-dependent parameters

Solve nonstiff differential equations — mediumorder method (5)

The initial condition is Solve nonstiff differential equations — mediumorder method (6). The function f(t) is defined by the n-by-1 vector f evaluated at times ft. The function g(t) is defined by the m-by-1 vector g evaluated at times gt.

Create the vectors f and g.

ft = linspace(0,5,25);f = ft.^2 - ft - 3;gt = linspace(1,6,25);g = 3*sin(gt-0.25);

Write a function named myode that interpolates f and g to obtain the value of the time-dependent terms at the specified time. Save the function in your current folder to run the rest of the example.

The myode function accepts extra input arguments to evaluate the ODE at each time step, but ode45 only uses the first two input arguments t and y.

function dydt = myode(t,y,ft,f,gt,g)f = interp1(ft,f,t); % Interpolate the data set (ft,f) at time tg = interp1(gt,g,t); % Interpolate the data set (gt,g) at time tdydt = -f.*y + g; % Evaluate ODE at time t

Solve the equation over the time interval [1 5] using ode45. Specify the function using a function handle so that ode45 uses only the first two input arguments of myode. Also, loosen the error thresholds using odeset.

tspan = [1 5];ic = 1;opts = odeset('RelTol',1e-2,'AbsTol',1e-4);[t,y] = ode45(@(t,y) myode(t,y,ft,f,gt,g), tspan, ic, opts);

Plot the solution, y, as a function of the time points, t.

plot(t,y)

Solve nonstiff differential equations — mediumorder method (7)

Evaluate and Extend Solution Structure

Open Live Script

The van der Pol equation is a second order ODE

y1-μ(1-y12)y1+y1=0.

Solve the van der Pol equation with μ=1 using ode45. The function vdp1.m ships with MATLAB® and encodes the equations. Specify a single output to return a structure containing information about the solution, such as the solver and evaluation points.

tspan = [0 20];y0 = [2 0];sol = ode45(@vdp1,tspan,y0)
sol = struct with fields: solver: 'ode45' extdata: [1x1 struct] x: [0 1.0048e-04 6.0285e-04 0.0031 0.0157 0.0785 0.2844 0.5407 0.8788 1.4032 1.8905 2.3778 2.7795 3.1285 3.4093 3.6657 3.9275 4.2944 4.9013 5.3506 5.7998 6.2075 6.5387 6.7519 6.9652 7.2247 7.5719 8.1226 8.6122 9.1017 9.5054 ... ] (1x60 double) y: [2x60 double] stats: [1x1 struct] idata: [1x1 struct]

Use linspace to generate 250 points in the interval [0 20]. Evaluate the solution at these points using deval.

x = linspace(0,20,250);y = deval(sol,x);

Plot the first component of the solution.

plot(x,y(1,:))

Solve nonstiff differential equations — mediumorder method (8)

Extend the solution to tf=35 using odextend and add the result to the original plot.

sol_new = odextend(sol,@vdp1,35);x = linspace(20,35,350);y = deval(sol_new,x);hold onplot(x,y(1,:),'r')

Solve nonstiff differential equations — mediumorder method (9)

Input Arguments

collapse all

Output Arguments

collapse all

Algorithms

ode45 is based on an explicit Runge-Kutta(4,5) formula, the Dormand-Prince pair. It is a single-step solver– in computing y(tn),it needs only the solution at the immediately preceding time point, y(tn-1) [1], [2].

References

[1] Dormand, J. R. and P. J. Prince, “Afamily of embedded Runge-Kutta formulae,” J. Comp.Appl. Math., Vol. 6, 1980, pp. 19–26.

[2] Shampine, L. F. and M. W. Reichelt, “TheMATLAB ODE Suite,” SIAM Journal on ScientificComputing, Vol. 18, 1997, pp. 1–22.

Extended Capabilities

Version History

Introduced before R2006a

See Also

ode23 | ode78 | ode89 | ode113 | ode15s | odeset | odeget | deval | odextend

Topics

  • Choose an ODE Solver
  • Summary of ODE Options
  • Solve Nonstiff ODEs
  • Anonymous Functions
  • Troubleshoot Common ODE Problems

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Solve nonstiff differential equations — mediumorder method (10)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

Solve nonstiff differential equations — medium
order method (2024)
Top Articles
Latest Posts
Article information

Author: Terence Hammes MD

Last Updated:

Views: 5518

Rating: 4.9 / 5 (49 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Terence Hammes MD

Birthday: 1992-04-11

Address: Suite 408 9446 Mercy Mews, West Roxie, CT 04904

Phone: +50312511349175

Job: Product Consulting Liaison

Hobby: Jogging, Motor sports, Nordic skating, Jigsaw puzzles, Bird watching, Nordic skating, Sculpting

Introduction: My name is Terence Hammes MD, I am a inexpensive, energetic, jolly, faithful, cheerful, proud, rich person who loves writing and wants to share my knowledge and understanding with you.