# Need code for differential equation analysis $\dot{x} = rx \ln \frac{K}{x}$ [closed]

I'm analyzing the Gompertz population model: ${\dot{x} = rx \ln \frac{K}{x}}$.

Can anyone kindly write the mathematica code for this equation so I can vary the parameters so I can intepret what's going on with the parameters $r$ and $K$?

Initial code was

 Manipulate[ (sol = NDSolve[{x'[t] == r*x[t]*log[K/x[t]], x[0] == 0.001}, x[t], {t, 0, 1000}]; Plot[Evaluate[x[t] /. sol], {t, 0, 1000}, PlotRange -> {0, 10}]), {{r, 0.01}, 0, 0.05}, {{K, 5}, 0, 10}]

• I'm voting to close this question as off-topic because there is no well-focused question in this post; the OP is simply asking for somebody to act as a free coding service. Commented Feb 1, 2018 at 4:57
• You are making no effort on your own. You don't even bother to give necessary information such as the initial condition on x[t] or the range of the parameters k and r. Commented Feb 1, 2018 at 5:12
• ln isn't a function in Mathematica. It's Log. Do you need NDSolve? You can solve something as simple as this analytically. And (hopefully) finally rx[t] =!= r*x[t]. Please. Read the docs. Oh and you're using Manipulate wrong here. Commented Feb 1, 2018 at 5:17
• I mean you can make Mathematica solve this analytically. In any case, yes, I can see a very simple one that you should know from reading the docs. When I fix this the code works. Mathematica reads log(a) to be log*a. You want to use Log as a function. Past this I'm going to ask you to figure it out on your own. I think you can do it. Commented Feb 1, 2018 at 5:23
• Is log a function in Mathematica? Look it up. Log certainly is, though. Commented Feb 1, 2018 at 5:29

Try this.

Clear[x]
x = DSolveValue[{x'[t] == r x[t] Log[k/x[t]], x[0] == .001}, x, t];
Manipulate[
Plot[Evaluate[x[t] /. {r -> rr, k -> kk}], {t, 0, 1}],
{{rr, 1, "r"}, 1, 11, .1, Appearance -> "Labeled"},
{{kk, .1, "k"}, .05, 2., .01, Appearance -> "Labeled"}]


• The OP actually did give the IC in a comment (x[0] == .001). For performance it might be worth tossing the standard Evaluate in the first argument of Plot. I'm imagining someone will naively come and copy this some day for something more complicated and their Plot will take forever. Commented Feb 1, 2018 at 5:38
• @b3m2a1. Shouldn't have to dig such info out a comment. But you make two good points and I have modified my code accordingly. Commented Feb 1, 2018 at 5:50

Here it is quite clear that the equation in question is easily transformed into a trivial equation

y'[z]==q-y[z]


by the replacements: y=Log[x], z=r t and q=Log[k].

This equation can be then either solved, or its solution could be taken from the nearest textbook on differential equations.

However, I will make these transformations on the screen step-by-step just for a pleasure. There, the initial equation is:

eq1 = x'[t] == r*x[t]*Log[k/x[t]]


@Itsnhantransitive Please note that the capital K is reserved in Mma, and it is generally recommended not to use capital letters for private variables. Therefore, here stays small k.

Let us now make the following replacement: x[t]->Exp[y[z]]:

eq2 = eq1 /. x -> (Exp[y[r*#]] &) /. t ->z/r


Let us cancel the common factor, then represent Log[E^-y[z] k] as -y[z]+Log[k] and finally replace Log[k]->q:

eq3 = Map[Divide[#, E^y[z]*r] &, eq2] /.
Log[E^-a_*b_] :> -a + Log[b] /. Log[k] -> q

(* Derivative[1][y][z] == q - y[z] *)


Done. Now one can solve this equation:

sl = DSolve[eq3, y, z]

(*  {{y -> Function[{z}, q + E^-z C[1]]}}  *)


and substitute the replacements back:

x[t] == Exp[sl[[1, 1, 2, 2]] /. {z -> r*t, q -> Log[k]}]

(*  x[t] == E^(E^(-r t) C[1]) k  *)


Done. One can, further, analyze the result, but this is another story.

Have fun!