Runge kutta matlab code (method)
Hi guys,
I am having trouble implementing Runge Kutta to solve the following
Lotka-Volterra equations.
dx/dt=x(1-2y) t0=0, x(t0)=1
dy/dt=-y(1-2x) t0=0, x(t0)=2
Below I given what code I have done so far but it only produces an axis with a dot.
My first file has the code below:
function z_out=lotka_volterra_rhs(z)
%create a vector for storing the results;
x=zeros(1,2);
%z(1) is supposed to store the values of x
%z(2) is supposed to store the values of y
x(1)=z(1)*(1-2*z(2));
x(2)=-z(2)*(1-2*z(1));
z_out=x;
my second file has the code below:
t0=0;
t1=40;
Delta=0.005;
t=t0: Delta:t1;
i=1;
y(i)=2;
x(i)=1;
while (x(i)<x)
z_1=lotka_volterra_rhs(y(i),x(i));
z_1x=lotka_volterra_rhs(y(i),x(i));
z_2x=lotka_volterra_rhs((y(i)+Delta*(z_1/(2))),(x(i)+Delta*(z_1x/(2))));
z_2=lotka_volterra_rhs((y(i)+Delta*(z_1/(2))),(x(i)+Delta*(z_1x/(2))));
z_3x=lotka_volterra_rhs((y(i)+Delta*(z_2/(2))),(x(i)+Delta*(z_2x/(2))));
z_3=lotka_volterra_rhs((y(i)+Delta*(z_2/(2))),(x(i)+Delta*(z_2x/(2))));
z_4x=lotka_volterra_rhs((y(i)+Delta*z_3),(x(i)+Del ta*z_3x));
z_4=lotka_volterra_rhs((y(i)+Delta*z_3),(x(i)+Delt a*z_3x));
y(i+1)=(y(i)+Delta*((z_1+2*z_2+2*z_3+z_4)/(6)));
x(i+1)=(x(i)+Delta*((z_1x+2*z_2x+2*z_3x+z_4x)/(6)));
i=i+1;
end;
plot(x,y)
Thank you guys... really appreciate it