sábado, 25 de mayo de 2019

Adams-Moulton Method

https://www.mathworks.com/matlabcentral/fileexchange/63034-adams-bashforth-moulton-method
Numerical Methods (single step and multi step) for solving First Order Ordinary Differential Equations. 
Methods included: 
1. Euler's Method 
2. Heun's Method 
3. Fourth Order Runge Kutta methods 
4. Adams-Bashforth Method 
5. Adams-Moulton Method 
These methods are commonly used for solving IVP, a first order Initial Value Problem (IVP) is defined as a first order differential equation together with specified initial condition at t=t₀: 
y' = f(t,y) ; t0 ≤ t ≤ b with y(t₀) = y₀
There exist several methods for finding solutions of differential equations.


%% Adams-Bashworth-Moulton Predictor, corrector and other methods
%% initialization
%%
syms x y
% Range of x
xspan=[2,3];
% Step size
h=0.1
% Initial values
y0=3, x0=2
y(1)=y0;x(1)=x0;
% No. of steps
steps=(xspan(2)-xspan(1))/h;
%% Define f(x,y)
%%
f= @(x,y)x*(y-x);
%% Exact Solution
%%
[Xe,Yexact]=ode45(f,xspan,y0);
Xe=Xe(1:4:end);
Yexact=Yexact(1:4:end);
Exact_Sol=vpa([Xe Yexact],5)
%% Euler's Method
%%
for j=2:steps+1
x(j,1)=x(j-1)+h;
y(j,1)=y(j-1,1)+h*f(x(j-1,1),y(j-1,1));
end
euler=vpa([x y],5)
% Mean Square Error
mse = vpa(norm(Yexact-y,2),5)
%% Heun's Method
%%
for j=2:steps+1
x(j,1)=x(j-1)+h;
k1(j-1,1)=h*f( x(j-1), y(j-1) );
k2(j-1,1)=h*f( x(j-1)+h, y(j-1)+k1(j-1));
y(j,1)=y(j-1)+0.5*(k1(j-1)+k2(j-1));
end
heuns=vpa([x y],5)
% Mean Square Error
mse = vpa(norm(Yexact-y,2),5)
%% RK 4th order method
%%
for j=2:steps+1
x(j,1)=x(j-1)+h;
k1(j-1,1)=h*f( x(j-1), y(j-1) );
k2(j-1,1)=h*f( x(j-1)+h/2, y(j-1)+0.5*k1(j-1) );
k3(j-1,1)=h*f( x(j-1)+h/2, y(j-1)+0.5*k2(j-1) );
k4(j-1,1)=h*f( x(j-1)+h, y(j-1)+k3(j-1) );
y(j,1)=y(j-1)+(1/6)*(k1(j-1)+2*k2(j-1)+2*k3(j-1)+k4(j-1));
end
rk4=vpa([x y],5)
% Mean Square Error
mse = vpa(norm(Yexact-y,2),5)
%% Adam-Bashworth predictor
%%
for k=5:steps+1
x(k,1)=x(k-1)+h;
y(k,1)=y(k-1) +(h/24)*( -9*f(x(k-4),y(k-4)) +37*f(x(k-3),y(k-3))...
-59*f(x(k-2),y(k-2)) +55*f(x(k-1),y(k-1)));
end
p=y;
Adam_Bashworth=vpa([x p],5)
% Mean Square Error
mse = vpa(norm(Yexact-y,2),5)
%% Adam-Moulton corrector
%%
for k=5:steps+1
x(k,1)=x(k-1)+h;
y(k,1)=y(k-1) +(h/24)*( f(x(k-3),y(k-3)) -5*f(x(k-2),y(k-2))...
+19*f(x(k-1),y(k-1)) +9*f(x(k),p(k)));
end
Adam_Moulton=vpa([x y],5)
% Mean Square Error
mse = vpa(norm(Yexact-y,2),5)
%%
% Created by- Bhartendu Thakur, Machine Learning & Computing


Adams-Bashworth-Moulton Predictor, corrector and other methods

initialization

syms x y
% Range of x
xspan=[2,3];
% Step size
h=0.1
h = 0.1000
% Initial values
y0=3, x0=2
y0 = 3
x0 = 2
y(1)=y0;x(1)=x0;
% No. of steps
steps=(xspan(2)-xspan(1))/h;

Define f(x,y)

f= @(x,y)x*(y-x);

Exact Solution

[Xe,Yexact]=ode45(f,xspan,y0);
Xe=Xe(1:4:end);
Yexact=Yexact(1:4:end);
Exact_Sol=vpa([Xe Yexact],5)
Exact_Sol = 
2.02.12.22.32.42.52.62.72.82.93.03.03.21643.47263.78144.16114.63665.24326.03067.06968.461810.354

Euler's Method

for j=2:steps+1
x(j,1)=x(j-1)+h;
y(j,1)=y(j-1,1)+h*f(x(j-1,1),y(j-1,1));
end
euler=vpa([x y],5)
euler = 
2.02.12.22.32.42.52.62.72.82.93.03.03.23.4313.70184.02424.41414.89265.48866.24167.20528.4537
% Mean Square Error
mse = vpa(norm(Yexact-y,2),5)
mse = 2.5235

Heun's Method

for j=2:steps+1
x(j,1)=x(j-1)+h;
k1(j-1,1)=h*f( x(j-1), y(j-1) );
k2(j-1,1)=h*f( x(j-1)+h, y(j-1)+k1(j-1));
y(j,1)=y(j-1)+0.5*(k1(j-1)+k2(j-1));
end
heuns=vpa([x y],5)
heuns = 
2.02.12.22.32.42.52.62.72.82.93.03.03.21553.47013.77654.15224.62165.21865.99097.00638.361410.195
% Mean Square Error
mse = vpa(norm(Yexact-y,2),5)
mse = 0.20447

RK 4th order method

for j=2:steps+1
x(j,1)=x(j-1)+h;
k1(j-1,1)=h*f( x(j-1), y(j-1) );
k2(j-1,1)=h*f( x(j-1)+h/2, y(j-1)+0.5*k1(j-1) );
k3(j-1,1)=h*f( x(j-1)+h/2, y(j-1)+0.5*k2(j-1) );
k4(j-1,1)=h*f( x(j-1)+h, y(j-1)+k3(j-1) );
y(j,1)=y(j-1)+(1/6)*(k1(j-1)+2*k2(j-1)+2*k3(j-1)+k4(j-1));
end
rk4=vpa([x y],5)
rk4 = 
2.02.12.22.32.42.52.62.72.82.93.03.03.21643.47263.78144.16114.63655.24316.03057.06948.461510.353
% Mean Square Error
mse = vpa(norm(Yexact-y,2),5)
mse = 0.00066539

Adam-Bashworth predictor

for k=5:steps+1
x(k,1)=x(k-1)+h;
y(k,1)=y(k-1) +(h/24)*( -9*f(x(k-4),y(k-4)) +37*f(x(k-3),y(k-3))...
-59*f(x(k-2),y(k-2)) +55*f(x(k-1),y(k-1)));
end
p=y;
Adam_Bashworth=vpa([x p],5)
Adam_Bashworth = 
2.02.12.22.32.42.52.62.72.82.93.03.03.21643.47263.78144.16054.63485.23956.02387.05768.441310.319
% Mean Square Error
mse = vpa(norm(Yexact-y,2),5)
mse = 0.042467

Adam-Moulton corrector

for k=5:steps+1
x(k,1)=x(k-1)+h;
y(k,1)=y(k-1) +(h/24)*( f(x(k-3),y(k-3)) -5*f(x(k-2),y(k-2))...
+19*f(x(k-1),y(k-1)) +9*f(x(k),p(k)));
end
Adam_Moulton=vpa([x y],5)
Adam_Moulton = 
2.02.12.22.32.42.52.62.72.82.93.03.03.21643.47263.78144.16114.63655.24286.02977.06758.457510.345
% Mean Square Error
mse = vpa(norm(Yexact-y,2),5)
mse = 0.0097564
% Created by- Bhartendu Thakur, Machine Learning & Computing

No hay comentarios:

Publicar un comentario