1
$\begingroup$

The function f[x, v, t] is the following numerical solution:

NDSolve[{
   D[f[x,v, t], t] == v*D[f[x,v, t], x] + D[f[x,v, t], v, v], 
   f[x,v, 0] == Exp[(-v^2 - x^2)/(2*0.001^2)]/(0.001*Sqrt[2 Pi]), 
   f[x,-100, t] == 0, f[x,100, t] == 0, f[100,v, t] == 0
  }, 
 f,
 {v, -100.0, 100.0}, {t, 0.0, 50.0}, {x, -100.0, 100.0}] 

How do I obtain $$q(x,t)=\int_{-100}^{100} f(x,v,t)dv$$

I wish to plot this function in the $x,t$ plane.

$\endgroup$
0

3 Answers 3

2
$\begingroup$

Since your code, doesn't work, I wrote my own simplified version:

g[x_,t_] = NDSolveValue[{
  D[f[x, t], t] == D[f[x, t], {x, 2}]
  , f[x, 0] == Sin[x]
  , f[0, t] == 0, f[π, t] == 0
 }
 , f[x, t]
 , {x, 0, 1}, {t, 0, 5}];

Note that I am using NDSolveValue to spit out the function automatically. You can then directly integrate the function:

integratedG[t_] = Integrate[g[x, t], {x, 0, 1}];

and the result is another interpolating function that you can then plot. The reason that this works is that behind the scenes, an InterpolatingFunction consists of a bunch of polynomials, which are obviously easy to analytically integrate, and Mathematica does that automatically.

Alternatively, if you don't need the function itself, you can build this directly into the call to NDSolveValue:

integratedG[t_] = NDSolveValue[{
   D[f[x, t], t] == D[f[x, t], {x, 2}]
   , f[x, 0] == Sin[x]
   , f[0, t] == 0
   , f[π, t] == 0}
  , Integrate[f[x0, t], {x0, 0, 1}]
  , {x, 0, 1}, {t, 0, 5}];

To wit:

enter image description here

or

enter image description here

$\endgroup$
1
  • $\begingroup$ Thx! Indeed I have a typo in the equation above, will correct it. $\endgroup$ Commented Feb 9, 2017 at 17:15
3
$\begingroup$

When working with solutions that come out of NDSolve I would always suggest to understand the resulting InterpolatingFunctions as a representation of a function in the mathematical sense and handle it as a value, not a function in a programmatic sense. To make clear what I am trying to say see this slightly modified version of marchs nice answer:

fsol = NDSolveValue[{
    D[f[x, t], t] == D[f[x, t], {x, 2}]
    , f[x, 0] == Sin[x]
    , f[0, t] == 0, f[π, t] == 0
  }
  , f
  , {x, 0, 1}, {t, 0, 5}];

This directly gets the result as a function, (no arguments involved!).

Integrating that with respect to one of its arguments is of course simple, what we expect to get back is another "mathematical" function:

Integrate[fsol[x, t], {x, 0, 1}]

what we get back is again an InterpolatingFunction, but unfortunately this time we have an argument attached. Does that match the idea of a mathematical function? I don't think so, so lets extract the real function:

fIntegrated = Block[{t}, Head@Integrate[fsol[x, t], {x, 0, 1}]]

the Block is not stricktly necessary, but it saves us from any problems which could occure if t would happen to have a definition. The advantages of that approach becomes even clearer when you need a derivative:

fDerivative = Derivative[1,0][fsol]
$\endgroup$
0
$\begingroup$

Credit goes to @m_goldberg for his idea.

The PDE in question cannot be handle directly by NDSolve. So we need to specify a Method. For this, I used MethodOfLines.

sol = NDSolve[{D[f[x, v, t], t] ==v*D[f[x, v, t], x] + D[f[x, v, t], v, v], 
   f[x, v, 0] == Exp[(-v^2 - x^2)/(2*0.001^2)]/(0.001*Sqrt[2 Pi]), 
   f[x, -100, t] == 0, f[x, 100, t] == 0, f[100, v, t] == 0}, 
  f, {v, -100.0, 100.0}, {t, 0.0, 50.0}, {x, -100.0, 100.0}, 
  Method -> {"MethodOfLines","SpatialDiscretization" -> {"TensorProductGrid", 
      "MinPoints" -> {30, 30}}, "TemporalVariable" -> t}];

Now extracting the interpolating function for external use,

{fF} = sol[[1, All, 2]];
Plot3D[fF[x, v, 1], {x, -100.0, 100.0}, {v, -100.0, 100.0}, PlotRange -> Full]

enter image description here

Integrating the interpolating function,

Block[{x, t}, q[x_, t_] = Integrate[fF[x, v, t], {v, -100, 100}];]
Plot3D[q[x, t], {x, -100.0, 100.0}, {t, 0, 50}, PlotPoints -> 30]

enter image description here

The above plot doesn't make any sense to me.

$\endgroup$
2
  • $\begingroup$ That seems like a good solution, however - is there a way to increase the precision of the numerics? For very simple examples (like a 2d diffusion equation) I get results which make no sense (as you point out) , e.g., negative values for f etc.. Is there a better way instead of "method of lines" for example? $\endgroup$ Commented Feb 10, 2017 at 13:56
  • $\begingroup$ "NDSolve::eerr: Warning: scaled local spatial error estimate of 50.50404534329911` at t = 1.` in the direction of independent variable x is much greater than the prescribed error tolerance. Grid spacing with 25 points may be too large to achieve the desired accuracy or precision. A singularity may have formed or a smaller grid spacing can be specified using the MaxStepSize or MinPoints method options." $\endgroup$ Commented Feb 10, 2017 at 14:02

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