# Solution dependent variable -how to use WhenEvent

I'm having trouble using WhenEvent function in two-dimensional functions:

My goal is to change the value of a diffusion coefficient $$\alpha[x, t]$$ as a function of the pressure field $$P[x,t]$$ acording the conditions: $$\alpha = 0.3$$ if $$P[x,t]<0.5$$ and $$\alpha = 0.7$$ if $$P[x,t]>0.5$$ only at the location where the presure surpasses the citical value of $$P[x,t]=0.5$$.

Based on the discussion made in this question, the code I'm using is:

ifun = NDSolve[{D[P[x, t], t] - α[x, t] D[P[x, t], {x, 2}] == 0,
P[x, 0] == 0, P[0, t] == UnitStep[t - t0],
P[L, t] == 0, α[x, 0] == 0.3,
WhenEvent[P[x, t] > 0.5,α[x, t] -> 0.7]},
{P[x, t], α[x, t]}, {t, 0, Tend}, {x, 0, L},
DiscreteVariables -> α]


where:

Tend = 10; L = 10; t0=1;


and the error message I get is:

NDSolve::disto: -- Message text not found -- (α[x,t])


Thanks a lot and a Happy new year:) Ofek.

• NDSolve needs the parametervalues t0,L,Tend – Ulrich Neumann Jan 3 at 11:19
• Sure, my mistake, those values are now added to my question, thanks. – Ofek Peretz Jan 3 at 11:26

Using WhenEvent as you propose gives nonlinear pde which cann't be solved by NDSolve!

Workaround(without WhenEvent):

L = 10;
Tend = 10;
t0 = 1;

solP = NDSolveValue[{D[P[x, t],t] - (0.3 + 0.4 Boole[P[x, t] > 0.5]) D[P[x, t],{x, 2}] == 0,P[x, 0] == 0, P[0, t] == UnitStep[t - t0], P[L, t] == 0},P  , {t, 0, Tend}, {x, 0, L} ]

Plot3D[solP[x, t], {x, 0, L}, {t, 0, Tend}, AxesLabel -> {x, t, P}]


• That's great! A lot more simple from what I've expected. I will give it a try, Thanks! – Ofek Peretz Jan 3 at 11:34