# Midpoint Method for Solving ODEs

by admin in on April 27, 2019

This code uses Midepoint Method to Solve single Ordinary Differential Equations (ODE). This code compare between the exact solution (if exist) and numerical solution, and presents the results on one chart, it shows the absolute Errors also on a new chart.

### Code Outputs:

• Chart presenting Midpoint method compared with Exact solution (if exist).
• Error chart (the absolute error between exact and Midpoint method).
• Printed Maximum absolute error for Midpoint method.

Max. Error = 0.052424

### Input Requirements:

• The differential equation you intended to calculated.
• Initial and Final Integral Terms.
• Initial Conditions.
• Exact solution if exist.
• Number of iterations.

Illustration of the midpoint method assuming that yn equals the exact value y(tn) The midpoint method computes yn+1  so that the red chord is approximately parallel to the tangent line at the midpoint (the green line).

In numerical analysis, a branch of applied mathematics, the midpoint method is a one-step method for numerically solving the differential equation,

.

The explicit midpoint method is given by the formula

the implicit midpoint method by

for n = 0,1,2,…  Here, h is the step size — a small positive number, tn = t0 + n.h and yn  is the computed approximate value of y(tn) The explicit midpoint method is also known as the modified Euler method, the implicit method is the most simple collocation method, and, applied to Hamiltonian dynamics, a symplectic integrator.

The name of the method comes from the fact that in the formula above the function f giving the slope of the solution is evaluated at (note : is ) which is the midpoint between at which the value of is known and at which the value of  needs to be found.

A geometric interpretation may give a better intuitive understanding of the method. In the basic Euler’s method, the tangent of the curve at is computed using . The next value { is found where the tangent intersects the vertical line . However, if the second derivative is only positive between and , or only negative (as in the diagram), the curve will increasingly veer away from the tangent, leading to larger errors as increases. The diagram illustrates that the tangent at the midpoint (upper, green line segment) would most likely give a more accurate approximation of the curve in that interval. This tangent is estimated by using the original Euler’s method to estimate the value of at the midpoint, then computing the slope of the tangent with. Finally, the improved tangent is used to calculate the value of from . This last step is represented by the red chord in the diagram. Note that the red chord is not exactly parallel to the green segment (the true tangent), due to the error in estimating the value of  at the midpoint.

The local error at each step of the midpoint method is of order , giving a global error of order . Thus, while more computationally intensive than Euler’s method, the midpoint method’s error generally decreases faster as .

The methods are examples of a class of higher-order methods known as Runge–Kutta methods.

### Derivation of the Midpoint Method:

Illustration of the midpoint method assuming that yn equals the exact value y(tn) The midpoint method computes yn+1 so that the red chord is approximately parallel to the tangent line at the midpoint (the green line).

In numerical analysis, a branch of applied mathematics, the midpoint method is a one-step method for numerically solving the differential equation,

.

The explicit midpoint method is given by the formula

the implicit midpoint method by

for n = 0,1,2,…  Here, h is the step size — a small positive number, tn = t0 + n.h and yn is the computed approximate value of y(tn) The explicit midpoint method is also known as the modified Euler method, the implicit method is the most simple collocation method, and, applied to Hamiltonian dynamics, a symplectic integrator.

The name of the method comes from the fact that in the formula above the function f giving the slope of the solution is evaluated at t = tn + h/2 (note : t is (tn + tn+1)/2) which is the midpoint between tn at which the value of y(t) is known and tn+1 at which the value of y(t) needs to be found.

A geometric interpretation may give a better intuitive understanding of the method. In the basic Euler’s method, the tangent of the curve at (tn, yn) is computed using f(tn, yn). The next value yn+1 is found where the tangent intersects the vertical line t = tn+1. However, if the second derivative is only positive between tn and tn+1, or only negative (as in the diagram), the curve will increasingly veer away from the tangent, leading to larger errors as h increases. The diagram illustrates that the tangent at the midpoint (upper, green line segment) would most likely give a more accurate approximation of the curve in that interval. This tangent is estimated by using the original Euler’s method to estimate the value of y(t) at the midpoint, then computing the slope of the tangent with f(). Finally, the improved tangent is used to calculate the value of yn+1 from yn. This last step is represented by the red chord in the diagram. Note that the red chord is not exactly parallel to the green segment (the true tangent), due to the error in estimating the value of y(t) at the midpoint.

The local error at each step of the midpoint method is of order O(h^3), giving a global error of order O(h^2). Thus, while more computationally intensive than Euler’s method, the midpoint method’s error generally decreases faster as h–>0.

The methods are examples of a class of higher-order methods known as Runge–Kutta methods.

### Derivation of the Midpoint Method:

Illustration of numerical integration for the equation y’ = y, y(0) =1. Blue: the Euler method, green: the midpoint method, red: the exact solution, y = exp(t) The step size is h = 1.0.

The same illustration for h = 0.25 It is seen that the midpoint method converges faster than the Euler method.

The midpoint method is a refinement of the Euler’s method

and is derived in a similar manner. The key to deriving Euler’s method is the approximate equality

which is obtained from the slope formula

and keeping in mind that y’ = f(t,y) For the midpoint methods, one replaces (3) with the more accurate

when instead of (2) we find

One cannot use this equation to find y(t+h) as one does not know y at t + h/2. The solution is then to use a Taylor series expansion exactly as if using the Euler method to solve for y(t+h/2):

which, when plugged in (4), gives us

and the explicit midpoint method (1e).

The implicit method (1i) is obtained by approximating the value at the half step t + h/2 by the midpoint of the line segment from y(t) to y(t+h)

and thus

Inserting the approximation yn + h.k for y(tn+h)  results in the implicit Runge-Kutta method

which contains the implicit Euler method with step size h/2 as its first part.

Because of the time symmetry of the implicit method, all terms of even degree in h of the local error cancel, so that the local error is automatically of order O(h^3). Replacing the implicit with the explicit Euler method in the determination of k results again in the explicit midpoint method.

### References:

[1] Griffiths,D. V.; Smith, I. M. (1991). Numerical methods for engineers: a programming approach. Boca Raton: CRC Press. p. 218. ISBN 0-8493-8610-1.

[2] Süli, Endre; Mayers, David (2003), An Introduction to Numerical Analysis, Cambridge University Press, ISBN 0-521-00794-1

Share Now!

#### Release Information

• Price
:

\$3.99

• Released
:

April 27, 2019

• Last Updated
:

May 28, 2019