I am trying to plot this function dxdt=N0*sin(omega*t)*x*(1-x/K) to get a 3-D plot but my code does not work,where is the error?
1 view (last 30 days)
Show older comments
Avan Al-Saffar
on 1 Aug 2014
Commented: Avan Al-Saffar
on 8 Aug 2014
function RunOsciliationsky3D
N0all= 1:1:10;
N=length(N0all);
omegaall= 1:1:10;
M=length(omegaall);
Pmax=zeros(1,N);
Pmean=zeros(1,N);
Pall=[Pmax(i),Pmean(j)];
x=length(Pall);
for i=1:N
for j =1:N
[t,x]=ode45(@osciliation,[0 100],0.1,[],N0all(i),10,omegaall(j));
Pall(i,j)=x;
end
end
[N0x,omegay]=meshgrid(N0all,omegaall);
h=mesh(N0x,omegay,Pall);
1;
Note: x axis = Noall
y axis =omegaall
z axis = Pall, which is a matrix containing the maximum and mean values of x.
0 Comments
Accepted Answer
Roger Stafford
on 2 Aug 2014
It is not necessary to call on 'ode45' to solve this particular differential equation. By ordinary methods of calculus, integration of both sides of
dx/(x*(1-x/K)) = N0*sin(omega*t)*dt
will give you
x/(K-x) = C*exp(-N0/omega*cos(omega*t))
where C is a constant whose value depends on the given initial condition. All you have to do then is put in those initial conditions for x and t to solve for C, and then solve the equation for x to obtain x as an explicit function of t involving the parameters N0 and omega. With this explicit formula you should be able to do whatever plotting you have in mind.
3 Comments
Roger Stafford
on 7 Aug 2014
I will assume you have followed my reasoning via calculus to the equation
x/(K-x) = C*exp(-N0/omega*cos(omega*t))
where C is a constant parameter whose value depends on the particular initial conditions you have with x. According to your ode45 call, the value of x is to be 0.1 when t = 0. If so, that suffices to determine what C must be:
0.1/(K-0.1) = C*exp(-N0/omega*cos(omega*0))
= C*exp(-N0/omega)
Therefore
C = 0.1/(K-0.1)*exp(N0/omega)
which gives the equation
x/(K-x) = 0.1/(K-0.1)*exp(N0/omega*(1-cos(omega*t)))
Solving this for x gives:
x = 0.1*K*exp(N0/omega*(1-cos(omega*t))) / ...
(K-0.1*(1-exp(N0/omega*(1-cos(omega*t)))))
= 0.1*K/((K-0.1)*exp(-N0/omega*(1-cos(omega*t)))+0.1)
This last equation is your explicit formula for x as a function of t, derived entirely without the use of ode45. You can do your plotting directly from this formula.
More Answers (0)
See Also
Categories
Find more on Calculus 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!