# Midpoint Method – Numerical Differentiation with MATLAB

Midpoint Method is numerical method to solve the first order ordinary differential equation with given initial condition. The midpoint method is also known as 2nd order Runge-Kutta method, which improve the Euler method by adding midpoint in step which is given better accuracy by order one. The Midpoint method is converge faster than Euler method. The local error of Midpoint method is O(h3)

At here, we write the code of Midpoint Method in MATLAB step by step. MATLAB is easy way to solve complicated problems that are not solve by hand or impossible to solve at page. MATLAB is develop for mathematics, therefore MATLAB is the abbreviation of MATrix LABoratory.

The formula of Modified Euler method is $y_{n+1}=y_{n}+hf(t_{n}+\frac{h}{2},y_{n}+\frac{h}{2}f(t_{n},y_{n}))$

At here, we solve the differential equation $\frac{dy}{dt}=y-t^{2}+1$ by using Euler method with the help of MATLAB.


% Midpoint method to solve the ordinary differential equation
% Midpoint method fall in the category of Runge-Kutta method of order 2
clear all;
close all;
clc;

f=inline('y-t^2+1');
x0=input('Enter x0=');
y0=input('Enter y0=');
xn=input('Enter upper limit of interval xn=');
h=input('Enter width (equal space) h=');
n=(xn-x0)/h;

fprintf('--------------------------------------------\n')
fprintf('    x              y            ynew\n');
fprintf('--------------------------------------------\n')

for i=1:n
y1=y0+h*f(x0+h/2,y0+h*f(x0,y0)/2);
fprintf('%f      %f       %f \n',x0,y0,y1)
y0=y1;
x0=x0+h;
end