Numerical solution of first-order ordinary differential equations
Here we introduce the second-order explicit and implicit Adams formula and Milne method to solve equations.
Question: For the initial value problem
u ′ = u − t 2 , 0 ≤ t ≤ 1 , u ( 0 ) = 0 u^\prime=ut^2,\ \ \ 0\le t\le1 ,u\left(0 \right)=0u′=u−t2, 0≤t≤1,u(0)=0
Try to use the second-order explicit, implicit Adams formula and Milne method to find the numerical solution, take τ = 0.2, and compare with the exact solution and Euler.
The second-order explicit Adams formula is:
u n + 1 = u n + τ 2 ( 3 f ( t n , u n ) − f ( t n − 1 , u n − 1 ) ) u_{n+1}=u_n+\frac{\tau}{2}\left(3f\left(t_n,u_n\right)-f\left(t_{n-1},u_{n-1}\right)\right) un+1=un+2t( 3 f(tn,un)−f(tn−1,un−1))
The second-order implicit Adams formula is:
u n + 1 = u n + τ 2 ( f ( t n + 1 , u n + 1 ) + f ( t n , u n ) ) u_{n+1}=u_n+\frac{\tau}{2}\left(f\left(t_{n+1},u_{n+1}\right)+f\left(t_n,u_n\right)\right) un+1=un+2t(f(tn+1,un+1)+f(tn,un))
The Milne method formula is:
u n + 2 = u n + τ 3 ( f ( t n + 2 , u n + 2 ) + 4 f ( t n + 1 , u n + 1 ) + f ( t n , u n ) ) u_{n+2}=u_n+\frac{\tau}{3}\left(f\left(t_{n+2},u_{n+2}\right)+4f\left(t_{n+1},u_{n+1}\right)+f\left(t_n,u_n\right)\right) un+2=un+3t(f(tn+2,un+2)+4f _(tn+1,un+1)+f(tn,un))
All codes are as follows
clear all;clc;close all;
fun= inline('u-t^2','t','u');
a = 0; b =1;
u0 = 0;
h = 0.2;
M =floor(b-a)/h ;
S=Milne(fun, a, b, u0, h);%or Adamsout(fun, t0, tn, u0, h) or Adamsin(fun, t0, tn, u0, h)
plot(S(:,1),S(:,2) ,'r*-'); hold on;
exa = dsolve('Du = u-t^2', 'u(0) = 0', 't'); %求出解析解
ezplot(exa, [0,1,-1,0.05]); %画出解析解的图像
legend('数值解','解析解' );
function S = Adamsout(fun, t0, tn, u0, h)%二阶显式的Adams 公式
%fun:微分方程的右表达式
%t0, tn为区间
%u0 为初值
M =floor(tn-t0)/h ; %离散点的个数M+1
T =zeros(1, M+1); U =zeros(1, M+1); %行向量
T(1) = t0;
T(2)=t0+h;
U(1) = u0;
K = feval(fun, T(1) ,U(1));
U(2)=u0+h*K;
for i = 2:M
K0 = feval(fun, T(i-1) ,U(i-1));
K1 = feval(fun, T(i) ,U(i));
U(i+1) = U(i) +h/2*(3*K1-K0);
T(i+1) = T(i) +h;
end
S = [T' U'];
function S = Adamsin(fun, t0, tn, u0, h)% 二阶隐式 Adams 法
%fun:微分方程的右表达式
%t0, tn为区间
%u0 为初值
M =floor(tn-t0)/h ; %离散点的个数M+1
T =zeros(1, M+1); U =zeros(1, M+1); %行向量
T(1) = t0;
U(1) = u0;
for i = 1:M
K1 = feval(fun, T(i) ,U(i));
K2 = feval(fun, T(i)+h ,U(i)+ h*K1);
U(i+1) = U(i) +h/2 *(K1 + K2);
T(i+1) = T(i) +h;
end
S = [T' U'];
function S = Milne(fun, t0, tn, u0, h)%Milne 方法
%fun:微分方程的右表达式
%t0, tn为区间
%u0 为初值
M =floor(tn-t0)/h ; %离散点的个数M+1
T =zeros(1, M+1); U =zeros(1, M+1); %行向量
T(1) = t0;
T(2) = t0+h;
K=Rungekutta(fun,t0,tn,u0,h);
U=(K(:,2))';
for i = 3:(M+1)
K1 = feval(fun, T(i-1) ,U(i-1));
K2= feval(fun, T(i-2) ,U(i-2));
K3 = feval(fun, T(i-1)+h ,U(i));
U(i) = U(i-2) +h/3 *(K2 + 4*K1+K3);
T(i) = T(i-1) +h;
end
S = [T' U'];
Question result
(1) Second-order explicit Adams numerical solution and analytical solution images:
(2) Second-order implicit Adams numerical solution and analytical solution image
(3) Milne method numerical solution and analytical solution image
Maximum error comparison:
Summarize
Summary: From the image analysis, we can clearly find that the second-order implicit Adams method and the Milne method have a better fitting degree than the second-order explicit Adams method. From the figure, we cannot see that the second-order implicit Adams method and the Milne method fit better. Which one is more accurate, so we compared the absolute values of the errors of the two methods. It is not difficult to see from the table that the Milne method has the highest accuracy.