Matlab:二阶R-k程序?哪位高手会编写这个Matlab程序,请帮帮忙!谢谢了。

如题所述

function [x,y]=Runge_kutta2(f,a,b,x0)
%2阶Runge_kutta解微分方程
%调用格式同ode45,
%f为微分方程函数,a b为积分区间,x0初值
%h为步长,默认为0.001
%例子
%odefun=@(t,x)[-10*x(1)*x(3)+x(2);10*x(1)*x(3)-x(2);-10*x(1)*x(3)+x(2)-2*x(3)];
%[t,y]=ode45(odefun,[0 10],[50 0 40]);
%[t1,y1]=Runge_kutta2(odefun,0,10,[50 0 40]);
%subplot(2,1,1),plot(t,y);legend('a-t','b-t','c-t');title('ode45')
%subplot(2,1,2),plot(t1,y1);legend('a-t','b-t','c-t');title('Runge_kutta2')

h=0.001;
xk=a:h:b;
n=(b-a)/h+1;
a=ones(length(x0),1);
y1(:,1)=x0';
for i=1:n
x1=xk(i)*a;
k1=f(x1,y1(:,i));
k2=f(x1+(3/4)*h*a,y1(:,i)+(3/4)*k1*h);
y1(:,i+1)=y1(:,i)+((1/3)*k1+(2/3)*k2)*h;
y(i,:)=y1(:,i)';
end
x=xk;
温馨提示:答案为网友推荐,仅供参考
相似回答