1
$\begingroup$

I am in the following situation: I have a complicated ODE for the function f[x] that has no anlytical solution and an integral that depends on f[x]: how can I plot the integral?

As a MWE consider the following situation:

f'[x] = Sqrt[f[x]]
Integrate[f[x]^2,x]

EDIT This is what I get with the following attempt:

NDSolve[{f'[x] == Sqrt[f[x]], f[0] == 1}, f, {x, 0, 10}]
Plot[Integrate[f[x]^2, x], {x, 0, 10}]

enter image description here

$\endgroup$
  • $\begingroup$ can you not solve the ODE numerically and obtain f(x) that way? Then you can integrate it. $\endgroup$ – Nasser Jan 16 at 10:40
  • $\begingroup$ I can solve the ODE numerically, but I don't know how to use the result to plot the integral. $\endgroup$ – mattiav27 Jan 16 at 10:41
  • $\begingroup$ result of NDSolve is a function. Which is f[x]. Then plug it into the integrand there and integrate it. You'll get a result which you can plot. $\endgroup$ – Nasser Jan 16 at 10:43
  • $\begingroup$ @Nasser see my edit $\endgroup$ – mattiav27 Jan 16 at 10:55
  • 1
    $\begingroup$ This is to calculate both results:DSolve[f'[x] == Sqrt[f[x]] && f [0] == 1, f, x] Integrate[f[x]^2 /. %, x] Plot[ %, {x, 0, 10}] $\endgroup$ – Boson Jan 16 at 14:24
3
$\begingroup$

Does this do what you want?

sol = First@NDSolve[{f'[x] == Sqrt[f[x]], f[0] == 1}, f, {x, 0, 10}];
Plot[Evaluate[Integrate[f[x] /. sol, x]], {x, 0, 10}]

Mathematica graphics

$\endgroup$
  • $\begingroup$ Yes it does! thank you $\endgroup$ – mattiav27 Jan 16 at 11:08
  • $\begingroup$ ˋDSolveˋ will find 2 solutions in this case (cf. comments above) - how can this be achieved with ˋNDSolveˋ? (+1 of course) $\endgroup$ – gwr Jan 17 at 7:15
1
$\begingroup$

You might also simply introduce another differential equation to obtain the antiderivative for f[x]:

sol = First @ NDSolve[
    {
       f'[x] == Sqrt @ f[x],
       g'[x] == f[x]^2, (* so g[x] is the antiderivative of f[x]^2 *)
        f[0] == 1,
        g[0] == 0
    }
    , { f, g }
    , { x, 0, 10 }
];
Plot[ g[x] /. sol, { x, 0, 10 } ]

Plot of the antiderivative

$\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.