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 view (last 30 days)
Show older comments
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;
0 Comments
Accepted Answer
Birdman
on 4 Apr 2018
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.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!