I am trying to plot this function dpdt = N0*sin(omega*t)*p*(1-p/K); but I got an error, Please could anyonre help me to solve this error?
1 view (last 30 days)
Show older comments
The code that I am using to plot the previous logistic growth model is :
function RunLogistic
[t,x]=ode45(@logistic42,[0 200],0.1,[],10,10,1);
plot(t,x,'-')
save dodo.mat
1;
I tried to change the solver because may be the error belong to stiff and non-stiff problem but it does not work.
Note: I need to plot the result with the same value for omega,tspan,K and N0.
2 Comments
Geoff Hayes
on 19 Aug 2014
Avan - what is the error that you are observing? Is logistic42
dpdt = N0*sin(omega*t)*p*(1-p/K);
or something else? Please attach all relevant code.
Answers (1)
Aykut Satici
on 19 Aug 2014
The built-in ODE solvers in MATLAB are all variable-step methods. This means they have an adaptive method for selecting how big of a time step they are going to take at a given point along the integration routine, see, for instance
Essentially, if they detect the solution changing more and more rapidly, they are going to take smaller and smaller steps.
One way to circumvent this, is to use fixed-step solvers. I have attached a few such solvers in this answer. These solvers do not change the step they take at each point in time.
Now, mathematically, if the right hand side of the differential equation is "smooth enough" all of these fixed steps solvers will converge to the real solution (which can be shown to exist and to be unique) as the time step goes to zero. Therefore, you may need to make the time step small to get it to converge, which would mean it may take a longer time for the integration to complete.
In your case, however, this is not really the case. I have implemented it for you, and it works just fine. Extract the file "ode5.m" from the zip file I have attached to this email and execute the following commands:
t = 0:0.01:50;
f = @(t,p,K,N0,omega) N0*sin(omega*t)*p*(1-p/K)
p = ode5(f,t,0.1,10,10,1);
plot(t,p)
Note that the syntax for "ode5.m" is similar but a little bit different than that of "ode45". You should check out "help ode5" before using it.
See Also
Categories
Find more on Ordinary Differential Equations 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!