1 view (last 30 days)

I'm basically setting a Runge-Kutta of order 4 to solve a sort of 'pred-prey' model. So, my three programs are:

1.

function [t,y]=RK(fun,tspan,y0,n,A,b,c)

h=(tspan(2)-tspan(1))/n;

y(:,1)=y0;

t(1)=tspan(1);

s=length(b);

for j=1:n

$ k(:,1)=feval(fun,t(j),y(:,j));

for i=2:s

k(:,i)=feval(fun,t(j)+c(i)*h,y(:,j)+h*k(:,1:i-1)*A(i,1:i-1)');

end

end

2.

function dy=pred_prey(t,y)

function s=cos(x)

k=1;

a=2/3;

d=4/3;

r(t)=int(s(x^2),x,0,t);

mu(t)=13/20-(3/5)*exp(-(3/x));

dy(1)=(y(1)+k)*r(t)-a*y(1)*y(2);

dy(2)=-mu(t)*y(2)+d*y(1)*y(2);

dy=dy';

3.

function []=driver_pred_prey()

close all

tspan=[0 100];

n=1000;

fun='pred_prey';

y0=[0 -1]';

A=[0 0 0 0; 1/2 0 0 0 ; 0 1/2 0 0; 0 0 1 0];

b=[1/6 1/3 1/3 1/6]';

c=[0 1/2 1/2 1]';

$$ [t,y]=RK('pred_prey', tspan, y0, n, A, b, c);

figure (1)

plot(t, y(1,:), 'k-'), hold

plot(t, y(2,:), 'k-.')

legend('resources',ppl')

figure (2)

plot(y(1,:),y(2,:))

title('orbits)

but I really do not understand what errors there are at $ and $$

Jan
on 24 Jul 2019

Index exceeds the number of array elements (1).

Error in RK (line 8)

k(:,1)=feval(fun,t(j),y(:,j));

After the line:

t(1)=tspan(1);

t is a scalar.

If you run this code:

for J=1:n

k(:,1)=feval(fun,t(j),y(:,j));

another error is expected: The for loop uses an uppercase J as counter, but inside the loop there is a lower case j. Because it is not defined, j is the imaginary unit and cannot be used as index.

If you fix this to for j = 1:n, there is the problem, that t is a scalar and you cannot access t(2).

Maybe you want this instead:

t = linspace(tspan(1), tspan(2), n);

Jan
on 24 Jul 2019

Again: Please post a complete copy of the error message. The details matter.

In which line does the error occur? There is an index in position 2, which exceeds the limits. This should help you to find and to fix the problem.

With some guessing: The code tries to access y(:,j), but this has not been defined anywhere before. The Runge Kutta code calculates the k 's only, but does not create the values for the actual trajectory.

You find many working Runge-Kutta-codes in the net.

Opportunities for recent engineering grads.

Apply Today
## 6 Comments

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/473036-i-do-not-understand-why-my-code-still-gives-me-errors#comment_727476

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/473036-i-do-not-understand-why-my-code-still-gives-me-errors#comment_727476

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/473036-i-do-not-understand-why-my-code-still-gives-me-errors#comment_727515

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/473036-i-do-not-understand-why-my-code-still-gives-me-errors#comment_727515

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/473036-i-do-not-understand-why-my-code-still-gives-me-errors#comment_727517

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/473036-i-do-not-understand-why-my-code-still-gives-me-errors#comment_727517

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/473036-i-do-not-understand-why-my-code-still-gives-me-errors#comment_727566

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/473036-i-do-not-understand-why-my-code-still-gives-me-errors#comment_727566

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/473036-i-do-not-understand-why-my-code-still-gives-me-errors#comment_727903

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/473036-i-do-not-understand-why-my-code-still-gives-me-errors#comment_727903

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/473036-i-do-not-understand-why-my-code-still-gives-me-errors#comment_727948

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/473036-i-do-not-understand-why-my-code-still-gives-me-errors#comment_727948

Sign in to comment.