MathWorks - Mobile View
  • MathWorks 㚢㚫㚦㒳㒆への㚵㚤㒳㚤㒳MathWorks 㚢㚫㚦㒳㒆への㚵㚤㒳㚤㒳
  • Access your MathWorks Account
    • マイ アカウント
    • コミュニティのプロファイル
    • ライセンスを関連付ける
    • サインアウト
  • 製品
  • ソリューション
  • アカデミア
  • サポート
  • コミュニティ
  • イベント
  • MATLAB を入手する
MathWorks
  • 製品
  • ソリューション
  • アカデミア
  • サポート
  • コミュニティ
  • イベント
  • MATLAB を入手する
  • MathWorks 㚢㚫㚦㒳㒆への㚵㚤㒳㚤㒳MathWorks 㚢㚫㚦㒳㒆への㚵㚤㒳㚤㒳
  • Access your MathWorks Account
    • マイ アカウント
    • コミュニティのプロファイル
    • ライセンスを関連付ける
    • サインアウト

ビデオ・Webセミナー

  • MathWorks
  • ビデオ
  • ビデオ ホーム
  • 検索
  • ビデオ ホーム
  • 検索
  • 営業へのお問い合わせ
  • 評価版
  Register to watch video
  • Description
  • Full Transcript
  • Code and Resources

Solving ODEs in MATLAB, 2: Midpoint Method, ODE2

From the series: Solving ODEs in MATLAB

Cleve Moler, MathWorks

ODE2 implements a midpoint method with two function evaluations per step. This method is twice as accurate as Euler's method. A nonlinear equation defining the sine function provides an example. An exercise involves implementing a related trapezoid method.

Related MATLAB code files can be downloaded from MATLAB Central

 

The cost of a numerical method for solving ordinary differential equations is measured by the number of times it evaluates the function f per step. Euler's method evaluates f once per step. Here's a new method that evaluates it twice per step. If f is evaluated once at the beginning of the step to give a slope s1, and then s1 is used to take Euler's step halfway across the interval, the function is evaluated in the middle of the interval to give the slope s2. And then s2 is used to take the step. For obvious reasons, this is called the midpoint method.

Here's ode2. It implements the midpoint method, evaluates the function twice per step. The structure is the same as ode1. Same arguments, same for loop, but now we have s1 at the beginning of the step, s2 in the middle of the step, and then the step is actually taken with s2.

Here's an example involving a trig function. Dy dt is the square root of 1, minus y squared. Starting at the origin on the interval from 0 to pi over 2. Now, because I've called it a trig example, you might just-- this is a separable equation-- do the integral, or you can just guess at the-- guess that the answer is sine t. Because the derivative of sine t is the cosine of t, and that's the square root of 1 minus y squared.

Let's set it up. F is the anonymous function square root of 1 minus y squared. T0 is 0. I'm going to take h to be pi over 32. And tfinal is pi over 2. And y0 is 0. And here's my call to ode2, with these five arguments, and it produces this output.

Now I want to plot it. Let's get t to go along with it. There is the t values as a column-- vector-- and let's plot. And do some annotation on the plot. Here's our plot. So there's the graph of our, there's the graph of sine t, the points generated by ode2.

Now I can't help but go look at these answers. This is supposed to be the values of sine t. This should be getting to 1 at pi over 2. We've got 0.997. That gives you a rough idea of what kind of accuracy we're getting out of this crude numerical method.

Let's take a look at an animation of the midpoint method. The differential equation is y prime is 2y, starting at t0 equals 0 with a step size of 1, going up to 3, and starting with y0 equals 10, and using ode2. Here is the animation. Here's t0 and y0. Evaluate the function at y0. 2 times y0 is 20, step halfway across the interval with that slope, that gets us to 20. Evaluate the function there, the slope is 40, so we take a step with slope 40 all the way across the interval to get up to 50.

That's the first step. Now we'll rescale the plot window. Here we are at 50. Evaluate the function there. The slope is 100, step halfway with that slope, get to the middle of the interval, evaluate the function there. The slope is 200, so we take a step with slope 200 to get up to 250. That's the second step. Rescale the plot window. Evaluate the function there. The slope is 500. Take that step halfway across the interval, evaluate the slope there. The slope is 1,000, so we take a step with slope of 1,000 to get up to 1,250 as our final value.

Since this is a rapidly increasing function of y, the values we generate here with the midpoint method are far larger than the values generated with the Euler method that we saw with ode1.

Here's an exercise. Modify ode2, creating ode2t, which implements the companion method, the trapezoid method. Evaluate the function at the beginning of the interval to get s1. Use s1 to go all the way across the interval. Evaluate the function at the right-hand endpoint of the interval to get s2. And then, use the average of s1 and s2 to take the step. That's the trapezoid method.

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:

ODE4 implements the classic Runge-Kutta method, the most widely used numerical method for ODEs over the past 100 years. Its major shortcoming is the lack of an error estimate. A simple model of the growth of a flame is an example that is used.
9:37
3: Classical Runge-Kutta, ODE4
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
9:51
Solving ODEs in MATLAB, 10: Tumbling Box
14:16
Solving ODEs in MATLAB, 11: Predator-Prey Equations
10:24
Solving ODEs in MATLAB, 12: Lorenz Attractor and Chaos

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

  • 営業へのお問い合わせ
  • 評価版

製品を見る

  • MATLAB
  • Simulink
  • 学生向けソフトウェア
  • ハードウェア サポート
  • File Exchange
試す、購入する
  • ダウンロード
  • 評価版ソフトウェア
  • 営業へのお問い合わせ
  • 価格とライセンス
使い方を学ぶ
  • ドキュメンテーション
  • チュートリアル
  • MATLAB の例
  • ビデオ・Webセミナー
  • トレーニング

サポートを受ける

  • インストールのヘルプ
  • MATLAB Answers
  • 技術コンサルティング
  • ライセンスセンター
  • サポートへのお問い合わせ

MathWorks について

  • 採用情報
  • ニュースルーム
  • 社会貢献
  • 営業へのお問い合わせ
  • MathWorks について

MathWorks

Accelerating the pace of engineering and science

MathWorksはエンジニアや研究者向け数値解析ソフトウェアのリーディングカンパニーです。

ディスカバー…

  • Select a Web Site United States
  • 特許
  • 商標
  • プライバシー ポリシー
  • 違法コピー防止
  • アプリケーション ステータス

© 1994-2021 The MathWorks, Inc.

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

MATLAB を語ろう