3
$\begingroup$

I want to solve, let's say, this differential equation:

y'[x]=y[x]^n

Given any initial condition (which I might want to manipulate too).

I want to plot the graph of this function while manipulating the exponential factor n. I tried something like this:

sol = NDSolve[{y'[x] == y[x]^n, y[0] == 0}, y[x], x]; 
Manipulate[ Plot[Evaluate[y[x] /. sol], {x, -5, 5}], {n, 0, 2}]

But it does not work...

I'm really sorry, but I'm new to Mathematica so it can be a really simple thing. Already grateful for the help!

$\endgroup$
5
$\begingroup$
Manipulate[ Plot[y[x] /. sol[n], {x, 0, 2}, Evaluated -> True],
 {n, 0, 4},
 Initialization-> {sol@h_:= Quiet@NDSolve[{y'@x == y@x^h, y@0 == 1}, y, {x, 0, 2}]}]

Mathematica graphics

$\endgroup$
3
$\begingroup$

Just for fun and using DynamicModule and only looking at integer exponents:

DynamicModule[{p},
 Framed[Column[{
    Dynamic[With[{h = p},
      sol = 
       First@Quiet[
         DSolve[{y'[x] == y[x]^h, y[0] == 1}, y[x], {x, 0, 2}]];
      Plot[y[x] /. sol, {x, 0, 2}, Evaluated -> True, 
       PlotStyle -> Red, 
       Epilog -> Text[Style[y[x] /. sol, White], Center], 
       Frame -> True, FrameStyle -> White, PlotRange -> {0, 30}, 
       ImageSize -> 350]]],
    Row[{Style["Exponent", White, Bold], Spacer[20], 
      SetterBar[Dynamic[p], Range[0, 10]]}]}], RoundingRadius -> 20, 
  Background -> Black]]

enter image description here

$\endgroup$
3
$\begingroup$

You are almost there. There are two things. One is to make the NDSolve syntax correctly. That is to give {x,0,5}instead of simply xin it, since NDSolve is a numeric solution and one needs to fix the limits for the variables.

The second is to put the solution of the equation under the Manipulate statement. Otherwise the solution will "not be aware" of your varying the n value:

Manipulate[
 sol = NDSolve[{y'[x] == y[x]^n, y[0] == 1}, y[x], {x, 0, 5}];
 Plot[Evaluate[y[x] /. sol], {x, 0, 5}],
   {n, 0, 2}]

enter image description here

Have fun!

$\endgroup$
  • $\begingroup$ Thanks for clear answer as always. In case you find the time, your input on this recent post would be very valuable mathematica.stackexchange.com/questions/166671/… Look forward to hearing from you. Thank you in any case. $\endgroup$ – user929304 Feb 26 '18 at 17:06
  • $\begingroup$ @ user929304 Have a look into the question you mentioned above, I gave an answer. $\endgroup$ – Alexei Boulbitch Feb 27 '18 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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