Why do i keep on receiving the same error? Please show me how to fix the code!

I tried to work on the code based on the predator-prey with chaos case study but I still get error after countless times of fixing it.

2 Comments

% Parameter values
a = 1.2;
b = 0.6;
c = 0.8;
d = 0.3;
% Initial conditions and time span
y0 = [2; 1];
tspan = [10, 40];
h = 0.0625;
% Solve the equations using Euler's method
[tp_euler, yp_euler] = eulersys(@predprey, tspan, y0, h, a, b, c, d);
% Solve the equations using RK4 method
[tp_rk4, yp_rk4] = rk4sys(@predprey, tspan, y0, h, a, b, c, d);
% Plot the results
figure;
subplot(2, 2, 1);
plot(tp_euler, yp_euler(:, 1), 'b', tp_euler, yp_euler(:, 2), 'r');
title('Euler time plot');
xlabel('Time');
ylabel('Population');
legend('Prey', 'Predator');
subplot(2, 2, 2);
plot(yp_euler(:, 1), yp_euler(:, 2));
title('Euler phase plane plot');
xlabel('Prey');
ylabel('Predator');
subplot(2, 2, 3);
plot(tp_rk4, yp_rk4(:, 1), 'b', tp_rk4, yp_rk4(:, 2), 'r');
title('RK4 time plot');
xlabel('Time');
ylabel('Population');
legend('Prey', 'Predator');
subplot(2, 2, 4);
plot(yp_rk4(:, 1), yp_rk4(:, 2));
title('RK4 phase plane plot');
xlabel('Prey');
ylabel('Predator');
% Predator-Prey Models and Chaos
% Function for Lotka-Volterra equations
function yp = predprey(t, y, a, b, c, d)
yp = [a * y(1) - b * y(1) * y(2); -c * y(2) + d * y(1) * y(2)];
end
% Function for Euler's method
function [tp, yp] = eulersys(dydt, tspan, y0, h, varargin)
t = tspan(1):h:tspan(2);
N = length(t);
y = zeros(N, length(y0));
y(1, :) = y0;
for i = 1:N-1
y(i+1, :) = y(i, :) + h * feval(dydt, t(i), y(i, :), varargin{:});
end
tp = t';
yp = y;
end
% Function for fourth-order Runge-Kutta method
function [tp, yp] = rk4sys(dydt, tspan, y0, h, varargin)
ti = tspan(1);
tf = tspan(2);
t = ti:h:tf;
N = length(t);
y = zeros(N, length(y0));
y(1, :) = y0;
for i = 1:N-1
tt = t(i);
hh = t(i+1) - t(i);
k1 = h * feval(dydt, tt, y(i, :), varargin{:})';
ymid = y(i, :) + k1./2;
k2 = h * feval(dydt, tt + hh/2, ymid, varargin{:})';
ymid = y(i, :) + k2./2;
k3 = h * feval(dydt, tt + hh/2, ymid, varargin{:})';
yend = y(i, :) + k3;
k4 = h * feval(dydt, tt + hh, yend, varargin{:})';
phi = (k1 + 2*(k2 + k3) + k4) / 6;
y(i+1, :) = y(i, :) + phi';
end
tp = t';
yp = y;
end

Sign in to comment.

 Accepted Answer

Your function "predprey" must return a row vector, not a column vector:
Use
function yp = predprey(t, y, a, b, c, d)
yp = [a * y(1) - b * y(1) * y(2), -c * y(2) + d * y(1) * y(2)];
end
instead of
function yp = predprey(t, y, a, b, c, d)
yp = [a * y(1) - b * y(1) * y(2); -c * y(2) + d * y(1) * y(2)];
end

5 Comments

I tried with that code but still error occur:
predator_prey_script
Unable to perform assignment because the size of the left side is 1-by-2 and the size of the right side
is 2-by-2.
Error in eulersys (line 7)
y(i+1, :) = y(i, :) + h * feval(dydt, t(i), y(i, :)', varargin{:});
Error in predator_prey_script (line 10)
[tp_euler, yp_euler] = eulersys(@predprey, tspan, y0, h, a, b, c, d);
I haven't looked at your code, but if you are getting a 2x2 on the RHS then that may be the result of adding a 1x2 row vector to a 2x1 column vector. Double check that all of your vectors are either row vectors or column vectors for consistency.
You can't get this error message because I tested the code and it worked after the change.
Although Euler and Runge-Kutta gave different results - but that's another issue.
If this Answer solves your original question, then could you please click the "Accept this answer" link to award the answerer with "reputation points" for their efforts in helping you? They'd appreciate it. Thanks in advance. 🙂 Note: you can only accept one answer (so pick the best one) but you can click the "Vote" icon for as many Answers as you want. Voting for an answer will also award reputation points.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB 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!