How to use Euler's method to solve the logistic grown model?

47 Ansichten (letzte 30 Tage)
Erin W
Erin W am 20 Sep. 2016
Kommentiert: Erin W am 23 Sep. 2016
Hi all,
I need help solving the logistic growth model (an ODE) using Euler's Method in MATLAB.
The function is: (dy/dx) = r*y*(1-(y/K)) where r is the growth rate and K is the carrying capacity.
I have solved this out by hand but I am having a difficult time implementing it as a function.
I'm meant to write a function with two inputs (a vector time, t, and an initial y, y0) and this function is meant to output a vector of solutions to the ode for each time t and plot the results.
I know there is mean to be some h as a time step to evaluate a new tangent line at the function and the smaller that step is, the more accurate the answer is, but I'm having a hard time writing the code.
Can anyone help?
  8 Kommentare
Erin W
Erin W am 22 Sep. 2016
"We will write a function in MATLAB to solve this equation numerically using Euler’s method.
Open a new file in MATLAB and declare a function odeEuler. There are 2 inputs to this function: a vector t of evenly spaced points over the time interval of integration, [0, 10] and an initial value y0. Note that your time step will be determined by the spacing in t. There is one output: the function should return a vector of the approximate solution to the logistic equation at the time points t."
Star Strider
Star Strider am 22 Sep. 2016
I should have been clearer. Sorry.
What algorithm for the Euler method were you told to use, or was discussed in class?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

James Tursa
James Tursa am 22 Sep. 2016
One step of Euler's Method is simply this:
(value at new time) = (value at old time) + (derivative at old time) * time_step
So to put this in a loop, the outline of your program would be as follows assuming y is a scalar:
t = your time vector
y0 = your initial y value
dt = the time step (you write code here to calculate this from the t vector based on your instructions)
y = zeros(size(t)); % allocate a vector to hold the output values, same size as t
y(1) = y0; % the initial value
for k=2:numel(t) % loop to find all the subsequent values y(2) through y(end)
y(k) = y(k-1) + (you write code for derivative dy/dt here) * dt; % The Euler step
end
So, complete the code as indicated above, and then add code to wrap all of this inside a function per the instructions and you will be done. Your function will return the y vector. (Note that t and y0 will be coming in as function arguments, not set directly in the function itself)
  5 Kommentare
James Tursa
James Tursa am 22 Sep. 2016
Bearbeitet: James Tursa am 22 Sep. 2016
Problem with this line:
dt = 0:t:10; % time step
dt is the distance between the current time and the next time. Since your assignment states that these distances are constant ("... evenly space points ..."), you can simply calculate one single scalar dt value and use that at each iteration. So ask yourself, if you are given a vector of times t that is evenly spaced, how would you calculate the distance between any two of these values that are next to each other? Put that calculation on the rhs of this line. (Hint: this is not some complex formula ... it is really really simple!)
And a problem with this line:
y(i) = y(i-1) + ((K*y0)/(y0+(K-y0)*e^(-r*t)))*dt;
Your derivative code uses the y0 value. This is not what your derivative equation looks like in your original post. If you look above, the derivative equation has y on the rhs, not y0. In other words, you should be using the current value of y, not the initial value of y. The current value of y for the rhs is simply y(i-1). So replace y0 with y(i-1) on the rhs.
Another problem, and it may be a bit confusing to you because of nomenclature, is the t that you are using on the rhs. Again, the derivative equation written above implicitly assumes that this t is the current time value for the rhs, not the entire t vector coming into the function. So the confusing part is that t is used differently ... in the function argument t is a vector of times, but in the derivative equation t is a single time value (from the vector t). So, bottom line is the time value for the derivative equation for the rhs is simply going to be t(i-1) since that is the current time of the rhs stuff. So replace the t on the rhs of this line with t(i-1).
And yes, just plot(t,y) will work as long as t and y are the names of the variables holding the data in the current workspace.
Erin W
Erin W am 23 Sep. 2016
I got it now! Thank you so much! :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Mathematics finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by