Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

communitycreator
midpoint
python

What is the midpoint method in ordinary differential equations?

Zain Ali Babar

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The midpoint method is a type of second order Runge-Kutta method. It is used to solve ordinary differential equations with a given initial condition.

This method uses a tangent to approximate the next point y(i+1)y(i+1) given the initial point y(i)y(i). This technique was named the midpoint method because it uses the tangent at the midpoint and then checks where the tangent cuts the vertical lines. The diagram below is an illustration of the midpoint method:

Illustration of how the midpoint method estimates the next term

The tangent in black is drawn at the midpoint. The red secant is parallel to the tangent above and cuts the vertical line at (x(i+1),(y(i+1))(x(i+1), (y(i+1)). The image shows that the estimate, y(i+1)y(i+1), which is marked in red, is very near to the actual value of y(i+1)y(i+1), which lies on the curve and is marked in black.

Formula:

y(i+1)=yi+k2(h)y_{(i+1)}=y_{i}+ k_{2}(h)

k1=f(xi,yi)k_{1}=f(x_{i},y_{i})

k2=f(xi+h2,yi+k1h2)k_{2}=f(x_{i}+\frac{h}{2}, y_{i}+k_{1}\frac{h}{2})

  • k1k1 is the overestimate and k2k2 is the underestimate.
  • hh is the step-size.
  • xix_{i} and yiy_{i} are the initial conditions.

Code

The code below demonstrates the use of the midpoint method for a function.

import math
def func(x,y):
func = (3*math.exp(-x))-(0.4*y) #Example of ordinary differential equation
return func
def midpoint():
##Initial Conditions
x = 0 #Independent variable
y = 5
##Integration Limit
xi=0
xf=3
h=1.5 #stepsize
original_ans=[5,4.37,2.76]
print("Iteration",0)
print("x:", x)
print("estimated y:",y)
print("original y:",original_ans[0])
print("-------")
i=1
n=(xf-xi)/h #number of iterations
while i <= n: #This loop will run until the number of iterations are completed
k1 = func(x,y) #K1
k2 = func(x+(h/2),y+(k1*h/2)) #K2
y = y+(k2*h) #Midpoint formula to update y
x = x+h
print("Iteration",i)
print("x:", x)
print("estimated y:",y)
print("original y:",original_ans[i])
print("--------------------")
i=i+1
def main():
midpoint()
main()
Code demonstrates the midpoint function

The code above predicts the value of y(i+1)y(i+1). The number of iterations is found by dividing the interval by the step-size. For each iteration, the original value is printed with which to compare the estimate.

RELATED TAGS

communitycreator
midpoint
python

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring