📄 nm_ch_0501.m
字号:
function [xx,yy,exact] = nm_ch_0501(a,h)
% This is the function for Numerical analysis Chapter 5.1 to solve the ODE
% equations with the Runge-Kutta Method
% [xx,yy] = nm_ch_0501(a,h)
% a is the coef of dy/dx
% h is the length of step
% xx is x, yy is y, exact is exact solution
func = inline(' 1 + (y - x) .* a ');
%
% func =
% Inline function:
% func(a,x,y) = 1 + (y - x) .* a
% y' = a*y - a* x + 1 , 0<x<1,
% y(0)=1;
% -50 < a <50 .
% the exact solution is
% y(x) = e^(a*x) +x ;
if ((h>=1) | (h<=0))
error(' the step :h , must be in interval (0,1) 1');
% break;
return;
end
x = 0 :h :1 ;
y = x;
k = length(x);
y(1) =1;
% y(1) is the first element of y ,and in fact is equal to the inital
% value
for n=1:k-1
k1 = func(a,x(n),y(n));
k2 = func(a,x(n)+0.5*h,y(n)+0.5*h*k1);
k3 = func(a,x(n)+0.5*h,y(n)+0.5*h*k2);
k4 = func(a,x(n)+h,y(n)+h*k3);
y(n+1) = y(n) + h*(k1+2.0*k2+2.0*k3+k4)/6.0;
% xn = xn+h; x(k+1)=xn; y(k+1)=y(n);
end
xx=x;
yy=y;
exact=exp(a*x) +x;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -