/(x(i)-xp)); %注意区分yh和y
end
end
调用程序
x=[11,12,13];
y=[2.3979,2.4849,2.5649];
xh=11.75;
yh=lagrange...function y=rk4(fun,a,b,y0,n)
h=(b-a)/n;
y(1)=y0;
for k=1:n
x=a+(k-1)*h;
k1=h*feval(fun,x,y(k));...fun,x+h,y(k)+k3);
y(k+1)=y(k)+(k1+2*k2+2*k3+k4)/6;
end
函数文件
function u=frk4(x,y)
u=y-2*x/y;
调用程序
y=rk4...(@frk4,0,1,1,10)
7.2 欧拉法
function y=euler(f,a,b,y0,h)
n=(b-a)/h;
y(1)=y0;
for i=1:n
x(i)=a+(i-1)*h...;
y(i+1)=h*feval(f,x(i),y(i));
end
函数文件
function u=feuler(x,y)
u=x^3-y/x;
调用程序
y=euler(@feuler,1,2,0.4,0.2