MATLAB Answers

code for eulers method help

1 view (last 30 days)
Michelle
Michelle on 5 Oct 2020
Commented: Walter Roberson on 15 Nov 2020
The derivative equation is y'=r(1-(y/L))*y-((p*y^2)/(q+y^2)) where L can be 5.4,8.1,16.3
Heres the code I have so far but it is not giving me a value.
p=1.2;
q=1;
r=0.65;
L1=5.4;
L2=8.1;
L3=16.3;
h=0.01; %step size
t=0:h:60; %0 days to 60 days
y=zeros(size(t));
y(1)=100; %inital amount of 100
n=numel(y);
for i=1:n-1
f=@(y)r(1-(y/L))*y-((p*y^2)/(q+y^2));
y(i+1)=y(i)+h*f(y)
end
  2 Comments
Walter Roberson
Walter Roberson on 15 Nov 2020
Michelle, if you feel that the question is unclear, then as you are the person who posted the question, you should be the one who clarifies it to make it more clear.

Sign in to comment.

Answers (1)

James Tursa
James Tursa on 5 Oct 2020
This needs to be outside of and prior to your loop, and r needs a multiply operator:
f=@(y)r*(1-(y/L))*y-((p*y^2)/(q+y^2));
This needs to have y indexed when passed into f:
y(i+1)=y(i)+h*f(y(i));
Finally, you should define the L you are going to use prior to creating the f function handle. E.g.,
L = L1;
f=@(y)r*(1-(y/L))*y-((p*y^2)/(q+y^2));
You can change L manually and run your code three times, or perhaps you can put all of the L selection stuff in a loop around your other code. E.g.,
for L=[L1,L2,L3]
f = @(y)r*(1-(y/L))*y-((p*y^2)/(q+y^2));
% your other code
end

Community Treasure Hunt

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

Start Hunting!