MATLAB Answers

improved Euler integration scheme (possible code error) in matlab

1 view (last 30 days)
Anastasia Kyriakou
Anastasia Kyriakou on 20 Feb 2020
Answered: Fabio Freschi on 20 Feb 2020
I have this ex:
dx/dt = −x(1 − y), t0 = 0, x(t0) = 0.5, (3)
dy/dt = y(1 − x), t0 = 0, y(t0) = 2 (4)
These equations are also known as Lotka-Volterra or predator-prey equations modeling evolution of species as a function of time t. In the equations above variable x stands for the number of predators, and y is the number of prey.
Let [0, 40] be the interval of integration.
Please implement improved Euler integration scheme with ∆ = 0.001, ∆ = 0.002, and
∆ = 0.005 (2) for (3) and (4). Plot the values of x(t), y(t) for t ∈ [0, 40] in the xy plane.
I have written this so far but i get an error (line 32) about ''='' . I have also tried to write it as ''=='' but it still does not work
Could someone help me?
function [x_out,y_out,t_out]=improved_euler_CS(Delta_in,T0,T)
% setting up parameters of the integration
t_max=T;
t_min=T0;
Delta=Delta_in;
% defining initial conditions
t=t_min:Delta:t_max;
% reserving space for y
x=zeros(1,size(t,2));
y=zeros(1,size(t,2));
% defining initial conditions
z=zeros(1,2);
z(1)=0.5; % this is the initial condition for x
z(2)=2; % this is the initial condition for y
for i=1:size(t,2)
x(i)=z(1);
y(i)=z(2);
z1=competing_species_rhs(z);
z2=competing_species_rhs(z+Delta*z1);
z=z+Delta*(z1+z2)/2;
end;
% returning the values of x,y, and t back to MATLAB?s environment
t_out=t;
x_out=x;
y_out=y;
where the function competing species rhs is defined as
function z_out=competing_species_rhs(z)
%create a vector for storing the results;
p=zeros(1,2);
%z(1) is supposed to store the values of x
%z(2) is supposed to store the values of y
p(1) = (-z(1)*(1=(z(2))));
p(2) = z(2)*(1=(z(1)));
z_out=p;
end
end

  0 Comments

Sign in to comment.

Answers (1)

Fabio Freschi
Fabio Freschi on 20 Feb 2020
I think it should be "-"
p(1) = (-z(1)*(1-(z(2))));
p(2) = z(2)*(1-(z(1)));

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!