How to apply runge kutta method for system of equations that are coupled ODE's? like x1'= -3*x2 and x2'=(1/3)*x1? I tried the code below but get an error on function define saying too may input arguments.

1 次查看(过去 30 天)
y=zeros(1,length(x));
z=zeros(1,length(x));
y(1)=0;
z(1)=0;
Fx1= @(x2)(3)*x2;
Gx2=@(x1)(1/3)*x1;
for i=2:(length(x)-1)
k1=Fx1(x(i),y(i));
g1=Gx2(x(i),y(i));
k2=Fx1(x(i)+0.5*h,y(i)+0.5*h*k1);
g2=Gx2(x(i)+0.5*h,y(i)+0.5*h*g1);
k3=Fx1((x(i)+0.5*h),(y(i)+0.5*h*k2));
g3=Gx2((x(i)+0.5*h),(y(i)+0.5*h*g2));
k4=Fx1((x(i)+h),(y(i)+k3*h));
g4=Gx2((x(i)+h),(y(i)+g3*h));
y(i+1)=y(i)+(1/6)*(k1+2*k2+2*k3+k4)*h;
z(i+1)=y(i)+(1/6)*(g1+2*g2+2*g3+g4)*h;

采纳的回答

Birdman
Birdman 2018-4-4
In all of your for loop, even though your Fx1 and Gx2 functions are defined for one input argument, you try to pass two input arguments. You need to either change your code in for loop, or change your function definitions before for loop.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by