2
$\begingroup$

I'm relatively new in Mathematica. I already checked this post: Need code for differential equation analysis $\dot{x} = rx \ln \frac{K}{x}$ but it doesn't seems to help me. I apologize in advance if this kind of question was already asked, but I don't find an answer that can help me with the following issue: I'm solving the Gompertz equation:

$$\frac{dN}{dt}=-aN\ln(bN)$$

with initial conditions $N(0)=\frac{1}{2b}$

I want to plot the solution curves of this equation with the following conditions: \begin{aligned}-3\leq t\leq3\text{ et }-3\leq N\leq3.\end{aligned} all in one graph. something like this: enter image description here

after trying using the function DNsolve in mathematica with no success, I decided I would plot directly from the solution of the equation: $$N(t)=\frac{1}{b}\exp({\ln{(1/2)}e^{-at}})$$

I use $a=2$ and $b=2$. I wrote the following code:

ClearAll["Global`*"]

b = 2; 
cValue = Log[1/2]; (*cte d'integration*)
a = 2; 
f[t_] = 1/b*Exp[cValue*Exp[-a*t]]
curve1 = Plot[1/b*Exp[cValue*Exp[-a*t]], {t, -3, 3},
    PlotStyle -> {Thickness[0.0065], Blue},
    PlotRange -> All, PlotLabel -> "Plot pour condition N(0)", 
  AxesLabel -> {"t", "N"}]

(*plots pour plusieurs solutions pour les intervalles -3<t<3 et \
-3<n<3*)
samples1 = table[f[t], {t, -3, 3, 1}]; 
curves = Plot[Evaluate[samples1], {t, -3, 3}, PlotStyle -> {Black}];

Show[curves, curve1]

but I get this: enter image description here

I can plot the curve with the initial condition (where the constant equals $\log(1/2)$) but not the other solution curves in the given interval. Can someone kindly help me to plot the family of curves or indicate me how can I do it with ND solve?

Thanks in advance

$\endgroup$
4
  • $\begingroup$ I'm not quite sure what you are asking for, but I suspect that ParametricNDSolvemight be useful for you. If you just want to see the solution for various values of $a$ and $b$ then that should essentially be what you need. $\endgroup$
    – chuy
    Feb 20 at 18:11
  • $\begingroup$ Note that your code has table and not Table $\endgroup$ Feb 20 at 19:20
  • $\begingroup$ Jeez, that's embarrasing. thanks for pointing it out @CraigCarter. $\endgroup$
    – nutshell_A
    Feb 21 at 0:03
  • $\begingroup$ @chuy, I didn't know that function until now. thanks for the tip! $\endgroup$
    – nutshell_A
    Feb 21 at 0:05

2 Answers 2

2
$\begingroup$

Maybe

cValue = Log[1/2]; (*cte d'integration*)
f[t_] := 1/b*Exp[cValue*Exp[-a*t]]
Plot[Table[
    1/b*Exp[cValue*Exp[-a*t]], {a, -2, 2, 
     1}, {b, {-1/2, -1/3, -1/6, 1/6, 1/3, 1/2}}] // Evaluate, {t, -3, 
   3}, PlotStyle -> {Thickness[0.0065], Blue}, PlotRange -> All, 
  PlotLabel -> "Plot pour condition N(0)", 
  AxesLabel -> {"t", "N"}] // Show

enter image description here

Or

Clear[funs];
funs = Table[
   NDSolveValue[{n'[t] == -a*n[t]*Log[b*n[t]], n[0] == 1/(2 b)}, 
    n[t], {t, -3, 3}], {a, -2, 2, 
    1}, {b, {-1/2, -1/3, -1/6, 1/6, 1/3, 1/2}}];
Plot[funs, {t, -3, 3}]

enter image description here

Or

Clear[sol]; 
sol = 
 ParametricNDSolveValue[{n'[t] == -a*n[t]*Log[b*n[t]], 
   n[0] == 1/(2  b)}, n[t], {t, -3, 3}, {a, b}];
Plot[Table[sol[a, b],
   {a, -2, 2, 1}, {b, {-1/2, -1/3, -1/6, 1/6, 1/3, 1/2}}] // 
  Evaluate, {t, -3, 3}]

enter image description here

$\endgroup$
1
  • $\begingroup$ This was just what I was looking for! thank you so much! now it make sense why the function NDsolve didn't work for solving this ODE in the first place.. If I may bother you with another silly question @cvgmt , I was wondering what is the difference between NDSolveValue and ParametricNDSolveValue. After reading the documentation, it seems to me the behave quite the same way. $\endgroup$
    – nutshell_A
    Feb 20 at 23:59
3
$\begingroup$

This is what I get. Note, your latex and code do not match. I assumed it is the Latex which is correct. But what I get does not exactly match what you show. So I think your ode is still missing something or your parameters are off somehow. Feel free to change this as needed.

f[t_, n_, a_, b_] = -a*n*Log[b/n]
a = 2; b = 2;
p1 = StreamPlot[{1, f[t, n, a, b]}, {t, -2, 2}, {n, -5, 5},
  Frame -> False,
  Axes -> True,
  AspectRatio -> 1/GoldenRatio,
  AxesLabel -> {"t", "N(t)"}, BaseStyle -> 12, 
  StreamPoints -> {{{{0, 1/(2*b)}, Red}, Automatic}}]

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ If I understand correctly, it seems your graph is plotting the function from the right side of the ODE, but not the solution of the ODE itself, right? so, in order to get the right plot I change f by the solution I found, right? $\endgroup$
    – nutshell_A
    Feb 20 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.