MathWorks - Mobile View
  • Sign In to Your MathWorks AccountSign In to Your MathWorks Account
  • Access your MathWorks Account
    • My Account
    • My Community Profile
    • Link License
    • Sign Out
  • Products
  • Solutions
  • Academia
  • Support
  • Community
  • Events
  • Get MATLAB
MathWorks
  • Products
  • Solutions
  • Academia
  • Support
  • Community
  • Events
  • Get MATLAB
  • Sign In to Your MathWorks AccountSign In to Your MathWorks Account
  • Access your MathWorks Account
    • My Account
    • My Community Profile
    • Link License
    • Sign Out

Videos and Webinars

  • MathWorks
  • Videos
  • Videos Home
  • Search
  • Videos Home
  • Search
  • Contact sales
  • Trial software
  Register to watch video
  • Description
  • Full Transcript
  • Code and Resources

Solving ODEs in MATLAB, 6: ODE45

From the series: Solving ODEs in MATLAB

Cleve Moler, MathWorks

ODE45 is usually the function of choice among the ODE solvers. It compares methods of orders four and five to estimate error and determine step size. ODE45 is so accurate that its default behavior is to use its interpolant to provide results at intermediate points.

Related MATLAB code files can be downloaded from MATLAB Central

 

The most frequently used ODE solver in MATLAB and Simulink is ODE45. It is based on method published by British mathematicians JR Dormand and PJ Prince in 1980. The basic method is order five. The error correction uses a companion order four method.

The slope of tn is, first same as last left over from the previous successful step. Then there are five more slopes from function values at 1/5 h, 3/10h, 4/5h, 8/9h and then at tn plus 1. These six slopes, linear combinations of them, are used to produce yn plus 1.

The function is evaluated at tn plus 1 and yn plus 1 to get a seventh slope. And then linear combinations of these are used to produce the error estimate.

Again, if the error estimate is less than the specified accuracy requirements the step is successful. And then that error estimate is used to get the next step size. If the error is too big, the step is unsuccessful and that error estimate is used to get the step size to do the step over again.

If we want to see the actual coefficients that are used, you can go into the code for ODE45. There's a table with the coefficients. Or you go to the Wikipedia page for the Dormand-Prince Method and there is the same coefficients.

As an aside, here is an interesting fact about higher order Runge-Kutta methods. Classical Runge-Kutta required four function evaluations per step to get order four. Dormand-Prince requires six function evaluations per step to get order five. You can't get order five with just five function evaluations. And then, if we were to try and achieve higher order, it would take even more function evaluations per step.

Let's use ODE45 to compute e to the t. y prime is equal to y. We can ask for output by supplying an argument called tspan. Zero and steps of 0.1 to 1. If we supply that as the input argument to solve this differential equation and get the output at those points, we get that back as the output. And now here's the approximations to the solution to that differential equation at those points.

If we plot it, here's the solution at those points. And to see how accurate it is, we see that we're actually getting this result to nine digits. ODE45 is very accurate.

Let's look at step size choice on our problem with near singularity, is a quarter. y0 is close to 16. The differential equation is y prime is 2(a-t) y squared. We let ODE45 choose its own step size by indicating we just want to integrate from 0 to 1. We capture the output in t and y and plot it.

Now, here, there's a lot of points here, but this is misleading because ODE45, by default, is using the refine option. It's only actually evaluating the function at every fourth one of these points and then using the interpolant to fill in in between. So we actually need a little different plot here.

This plot shows a little better what's going on. The big dots are the points that ODE45 chose to evaluate the differential equation. And the little dots are filled in with the interpolant. So the big dots are every fourth point. And the refine option says that the big dots are too far apart and we need to fill it in with the interpolant. And so this is the continuous interpolant in action.

The big dots are more closely concentrated as we have to go around the curve. And then, as we get farther away from the singularity the step size increases. So this shows the high accuracy of ODE45 and the automatic step size choice in action.

Here's an exercise. Compare ODE23 and ODE45 by using each of them to compute pi. The integral 4 over 1 plus t squared from 0 to 1 is pi. You can express that as a differential equation, use each of the routines to integrate that differential equation and see how close they get to computing pi.

Download Code and Files

Download the code in this video

Related Products

  • MATLAB

Learn More

Explore more resources from Cleve Moler
Related Information
Learn differential equations

Feedback

Featured Product

MATLAB

  • Request Trial
  • Get Pricing

Up Next:

A problem is said to be stiff if the solution being sought varies slowly, but there are nearby solutions that vary rapidly, so the numerical method must take small steps to obtain satisfactory results. The flame model demonstrates stiffness.
7:14
7: Stiffness, ODE23s, ODE15s
View full series (12 Videos)

Related Videos:

5:34
Solving ODEs in MATLAB, 9: The MATLAB ODE Suite
15:21
Solving ODEs in MATLAB, 1: Euler, ODE1
10:24
Solving ODEs in MATLAB, 12: Lorenz Attractor and Chaos
9:51
Solving ODEs in MATLAB, 10: Tumbling Box
14:16
Solving ODEs in MATLAB, 11: Predator-Prey Equations

View more related videos

MathWorks - Domain Selector

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: .

Select web site

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

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

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)
  • 中国
    • 简体中文Chinese
    • English
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

  • Contact sales
  • Trial software

Explore Products

  • MATLAB
  • Simulink
  • Student Software
  • Hardware Support
  • File Exchange

Try or Buy

  • Downloads
  • Trial Software
  • Contact Sales
  • Pricing and Licensing
  • How to Buy

Learn to Use

  • Documentation
  • Tutorials
  • Examples
  • Videos and Webinars
  • Training

Get Support

  • Installation Help
  • Answers
  • Consulting
  • License Center
  • Contact Support

About MathWorks

  • Careers
  • Newsroom
  • Social Mission
  • Contact Sales
  • About MathWorks

MathWorks

Accelerating the pace of engineering and science

MathWorks is the leading developer of mathematical computing software for engineers and scientists.

Discover…

  • Select a Web Site United States
  • Patents
  • Trademarks
  • Privacy Policy
  • Preventing Piracy
  • Application Status

© 1994-2021 The MathWorks, Inc.

  • Facebook
  • Twitter
  • Instagram
  • YouTube
  • LinkedIn
  • RSS

Join the conversation

This website uses cookies to improve your user experience, personalize content and ads, and analyze website traffic.  By continuing to use this website, you consent to our use of cookies.  Please see our Privacy Policy to learn more about cookies and how to change your settings.