求解常微分方程:
{ d y d x = x 3 − y x , y ( 1 ) = 2 5 . \begin{cases} \dfrac{dy}{dx}=x^3-\frac{y}{x},\\ y(1)=\frac{2}{5}. \end{cases} ⎩⎨⎧dxdy=x3−xy,y(1)=52.
该方程的精确解为:
y = 1 5 x 4 + 1 5 x . y=\frac{1}{5}x^4+\frac{1}{5x}. y=51x4+5x1.
y n + 1 = y n + h 2 ( f ( x n + 1 , y n + 1 ) + f ( x n , y n ) ) . y_{n+1}=y_n+\frac{h}{2}(f(x_{n+1},y_{n+1})+f(x_n,y_n)). yn+1=yn+2h(f(xn+1,yn+1)+f(xn,yn)).
对于这个问题可以被写为:
( 1 + h 2 x n + 1 ) y n + 1 = ( 1 − h 2 x n ) y n + h 2 ( x n + 1 3 + x n 3 ) . (1+\frac{h}{2x_{n+1}})y_{n+1}=(1-\frac{h}{2x_n})y_n+\frac{h}{2}(x_{n+1}^3+x_n^3). (1+2xn+1h)yn+1=(1−2xnh)yn+2h(xn+13+xn3).
{ y n + 1 ∗ = y n + h f ( x n , y n ) , y ( n + 1 ) = y n + h 2 ( f ( x n , y n ) + f ( x n + 1 , y n + 1 ∗ ) ) . \begin{cases} y_{n+1}^{*}=y_n+hf(x_n,y_n),\\ y(n+1)=y_n+\frac{h}{2}(f(x_n,y_n)+f(x_{n+1},y_{n+1}^{*})). \end{cases} {yn+1∗=yn+hf(xn,yn),y(n+1)=yn+2h(f(xn,yn)+f(xn+1,yn+1∗)).
误差阶( l ∞ l_\infty l∞) | Adams | 改进的欧拉法 |
---|---|---|
h=0.1 | 2.000274 | 1.999845 |
h=0.05 | 2.000068 | 1.999961 |
h=0.001 | 2.000003 | 1.999998 |
h=0.0005 | 2.000001 | 2.000000 |
%建立error_order.m文件
clear all,close all,clc
[error1,error2]=shili(0.005);
e1=[norm(error1,inf),norm(error2,inf)];
[error1,error2]=shili(0.0025);
e2=[norm(error1,inf),norm(error2,inf)];
format long
error_order_linf=log2(e1./e2)
%建立shili.m文件
%clear all,close all,clc
function [error1,error2]=shili(h)
f=@(x,y)x^3-y/x;
%h=0.1;
x=[1:h:2];
N=size(x,2)-1;
%% Adams
y1=[2/5,zeros(1,N)];
for n=1:N
mid=(1-h/(2*x(n)))*y1(n)+h/2*(x(n+1)^3+x(n)^3);
y1(n+1)=mid/(1+h/(2*x(n+1)));
end
%% Improved Euler method
y2=[2/5,zeros(1,N)];
for n=1:N
y2(n+1)=y2(n)+h*f(x(n),y2(n));
y2(n+1)=y2(n)+h/2*(f(x(n),y2(n))+f(x(n+1),y2(n+1)));
end
y=1/5*x.^4+1./(5*x); % Exact solution
error1=y-y1;
error2=y-y2;
end