I need to solve this PDE $$\partial_tf(t,x)+\partial_xf(t,x)+k\partial_{xx}f(t,x)-xf(t,x)=0 $$

with $k\in\mathbb{R}$ and final condition $f(T,x)=1$ with $0<t<T$.

My problem is how to solve numerically this PDE according to this:

$\begin{cases} k=k_1, \quad \partial_{xx}f(t,x) \ge 0 \\k=k_2, \quad \partial_{xx}f(t,x)<0 \end{cases}$

with $k_1,k_2\in\mathbb{R}$.

I supposed WhenEvent were to be used, therefore I set

T = 1
pde = D[f[t, T, x], {t, 1}] + D[f[t, T, x], {x, 1}] + k*D[f[t, T, x], {x, 2}] - 
x*f[t, T, x] == 0
cond = f[T, T, x] == 0

and with NDSolve

sol = NDSolve[{pde, cond, 
WhenEvent[D[f[t, T, x], {x, 2}] >= 0, k -> 0.1], 
WhenEvent[D[f[t, T, x], {x, 2}] < 0, k -> 0.05]}, 
f[t, T, x], {t, 0, T}, {x, 0, 0.5}]

but i got this error

NDSolve::ivone: Boundary values may only be specified for one independent variable. Initial values may only be specified at one value of the other independent variable.

ADDED: I pointed out that $f$ is a function only in $t$ and $x$, T is inserted in the code because it tries to replicate a bond-finance setting. Moreover, removing from the code the WhenEvent part I got a solution similar to this one: https://mathematica.stackexchange.com/questions/78186/pde-solved-with-ndsolve-how-can-i-plot-the-partial-derivatives-of-the-solution

How can I fix this problem?

  • $\begingroup$ Is the function f[t, y, x] or f[t, x] or f[y, x]? And you still miss 2 boundary conditions. (A rule of thumb for the necessary number of i.c. or b.c. is that it should be usually equal to the highest order of the corresponding derivative.) $\endgroup$
    – xzczd
    Mar 26, 2015 at 2:43
  • $\begingroup$ @xyzcd Your point is right, I mixed two different ways to formulate my problem. I've fixed the question. Thank you $\endgroup$
    – Marco
    Mar 26, 2015 at 10:22
  • $\begingroup$ Well, I still suggest you to add the missing b.c.s, though NDSolve gives an answer currently, it's not that clear what boundary is added. $\endgroup$
    – xzczd
    Mar 26, 2015 at 11:05
  • $\begingroup$ Yes, I have another bc, it is $\lim_{x \to +\infty}f(x,t)=0$, but I don't know how to add it. I tried with high values of x but I got errors. Moreover, in finance the standard condition used is the only one I wrote in the question. If can be useful, here there is a video that speaks about it: it's quite long, the part about this point is at 24:41 youtube.com/watch?v=sDmjVCx8Edw $\endgroup$
    – Marco
    Mar 26, 2015 at 11:16
  • $\begingroup$ I tried with an ODE case, modifying the initial problem (but still inserting the "if" case on the highest order derivative) and adding the right number of conditions: in that case, I got an error too. So my point is: suppose the problem is correctly specified, how can i implement this kind of "if"? $\endgroup$
    – Marco
    Mar 26, 2015 at 11:20

1 Answer 1


In my opinion, WhenEvent is still a wild beast in Mathematica, actually the only example for handling PDE with WhenEvent in the document only sets a "StopIntegration" event. Indeed, WhenEvent is able to do more, but quite tricky, see this post for example.

For your problem, I think using Piecewise is a possible solution:

T = 1;
pde = D[f[t, x], {t, 1}] + D[f[t, x], {x, 1}] + 
      Piecewise[{{0.1, D[f[t, x], {x, 2}] >= 0}}, 0.05] D[f[t, x], {x, 2}] - 
      x f[t, x] == 0;
cond = {f[T, x] == 0, f[t, 10] == 0, Derivative[0, 1][f][t, 10] == 0};

sol = NDSolveValue[{pde, cond}, f[t, x], {t, 0, T}, {x, 0, 10}]

Plot3D[sol, {t, 0, T}, {x, 0, 10}, PlotRange -> All]

Notice I used f[t, 10] == 0 and Derivative[0, 1][f][t, 10] == 0 to approximate the b.c.s. (Not sure if it's proper. ) The resulting plot isn't that interesting under the parameters you gave, so I'd like to omit it here. (More difficult parameters seem to lead to other problems though. )

  • 1
    $\begingroup$ +1. I agree Piecewise seems the way to go. I think the "when" in WhenEvent suggests it reacts to events in time, and actions that change a variable have to reinitialize the variable at each point in the spatial grid. That means you might have to know what the spatial grid actually is. (This remark pertains to the method of lines; I haven't looked into WhenEvent with FEM.) $\endgroup$
    – Michael E2
    Mar 26, 2015 at 12:12
  • $\begingroup$ without selecting proper coefficient is not so interesting but i changed them and it solves exactly what i was searching for. in fact, i was trying to solve a princing pde for a bond for example, so t mean time, and x the interest rate (so the plotting solution should be for $0<x<0.50$) while the piecewise condition for the volatility: changing it according to the concavity means uncertain volatility and the model is normally called UMV (developed by Avellaneda, Levy, Paras in 1995). thank you very much for your help $\endgroup$
    – Marco
    Mar 26, 2015 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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