0
$\begingroup$

How to get the solution from DSolve in such way that there is no need to copy the result.

When for example solving:

f[x_, y_] := a y + x^2
 DSolve[{D[y[x], x] == f[x, y[x]], y[0] == c}, y[x], x]

I get the solution:

{{y[x] -> (-2 + 2 E^(a x) + a^3 c E^(a x) - 2 a x - a^2 x^2)/a^3}}

How can I make a function of this let's say sol[x] so I can use it in such context:

Manipulate[
 Plot[sol[x], {x, 0, 5}], {a, -5, 5}, {c, -5, 5}]

EDIT: not interested in:

sol[x_, a_, c_] = 
 y[x] /. DSolve[{D[y[x], x] == f[x, y[x]], y[0] == c}, y[x], x] 
$\endgroup$
  • 1
    $\begingroup$ You mean DSolveValue? $\endgroup$ – xzczd Jul 31 '14 at 7:30
  • $\begingroup$ Not really, as Manipulate does not work with it as well. I can always stay with sol[x_,a_,c_] version. Thanks anyway. $\endgroup$ – Misery Jul 31 '14 at 7:45
  • 1
    $\begingroup$ Well, then have you considered DSolveValue[……] (* start a new line *) Manipulate[Plot[%, {x, 0, 5}], {a, -5, 5}, {c, -5, 5}]? $\endgroup$ – xzczd Jul 31 '14 at 8:36
  • $\begingroup$ @xzczd Why do not you give this as an answer? $\endgroup$ – Alexei Boulbitch Jul 31 '14 at 9:16
  • $\begingroup$ @AlexeiBoulbitch Because I'm not sure if this is what OP wants , % is surely not a function :) $\endgroup$ – xzczd Jul 31 '14 at 10:37
3
$\begingroup$

Just to add a bit to the xzczd answer given in a form of a comment above. In earlier Mma versions (that might be your case) it can be done as follows:

    f[x_, y_] := a y + x^2
ss = DSolve[{D[y[x], x] == f[x, y[x]], y[0] == c}, y, x][[1, 1]]

yielding this:

(*  y -> Function[{x}, (-2 + 2 E^(a x) + a^3 c E^(a x) - 2 a x - a^2 x^2)/
  a^3]    *)

Then you can Manipulate/Plot it using the following construct:

    Manipulate[
 Plot[Evaluate[y[x] /. ss /. {a -> a1, c -> c1}], {x, 0, 5}], {a1, -5,
   5}, {c1, -5, 5}]

Have fun!

$\endgroup$

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.