# Solving nonlinear PDE with Method Of Lines

I'm trying to master the method of lines for solving nonlinear PDEs. Currently, I've faced the PDE, that I'm not able to solve.

Firstly, I have started with this equation:

$$\partial_t U(t,x) =\partial^2_x U(t,x) + 25\cdot \cos^2 (U(t,x))\cdot\partial_x U(t,x)$$

With initial condition:

$$U(0,x) = \sin(x)$$

And boundary conditions:

$$U(t,0) = 0, U(t,\pi) = 0$$

And I can successfully solve it by the following code:

(*initialize grid params*)\[Rho]max = \[Pi]; tmax = 1;
n = 100; dh = \[Rho]max/(n + 1);

(*list of time functions and derivatives*)
U[t_] = Join[{0}, Table[u[i][t], {i, 1, n}], {0}];
Uhh[t_] = ListCorrelate[{1, -2, 1}/dh^2, U[t]];
Uh[t_] = ListCorrelate[{-3, 4, -1}/(2*dh), U[t]];

(*PDE*)
D[U[t], t][[2 ;; -2]] ==
Uhh[t] + 25*(Cos[U[t]][[2 ;; -2]])^2*Uh[t]];

(*IC*)
initU = Thread[U[0][[2 ;; -2]] == Table[Sin[dh*i], {i, 1, n}]];

(*call solver*)
lines = NDSolveValue[{eqU, initU}, {U[t][[2 ;; -2]]}, {t, 0, tmax}];

(*plot the result*)
Utab = Table[i dh, {i, 0, n + 1}][[2 ;; -2]];
ParametricPlot3D[Evaluate@Thread[{Utab, t, lines[[1]]}], {t, 0, 0.05},
PlotRange -> All, AxesLabel -> {"z", "t", "U"},
BoxRatios -> {2, 2, 1}, ImageSize -> Large,
LabelStyle -> {Black, Bold, Medium}]


The same plot can be produced by NDSolve:

nsol = NDSolve[{D[y[x, t], t] ==
D[D[y[x, t], x], x] + 25*(Cos[y[x, t]])^2*D[y[x, t], x], y[x, 0] == Sin[x], y[0, t] == 0, y[\[Pi], t] == 0}, y[x, t], {x, 0, 1}, {t, 0, 0.05}]
Plot3D[nsol[[1, 1, 2]], {x, 0, \[Pi]}, {t, 0, 0.05}]


After that I've decided to make problem more complex, I changed the equation this way:

$$U(t,x) \partial_t U(t,x) =\partial^2_x U(t,x) + 25\cdot \cos^2 (U(t,x))\cdot\partial_x U(t,x)$$

i.e add $$U(t,x)$$ to the LHS of the equation.

Thereafter, I couldn't solve this equation by code written above.

Execution is stopped by error:

NDSolveValue::ndsz: At t == 0.000026446140667429057, step size is effectively zero; singularity or stiff system suspected.


Can somebody give me a piece of advice how to solve this problem? Is it just property of such equation (presence of singularity) that not allow me to use method of lines here? How can I avoid the error, if it is possible at all?

• The PDE becomes singular where y[x, t] vanishes. Dec 10, 2019 at 22:58
• @bbgodfrey Thanks for your response! Maybe, you can tell me how can I avoid this singularity? Dec 11, 2019 at 9:53
• Since you're trying to understand method of lines, it's better to use some well-known problem for testing. Constructing PDE casually can easily lead to unsolvable problem, and your 2nd example is the case. Dec 15, 2019 at 1:57
• @Oiale Thank you for accepting my answer and for bounties. Dec 24, 2019 at 7:19

The modified equation, boundary conditions (BCs) and the initial condition (IC) are not consistent. To see this, let's consider the equation at $$x=0$$ (or at $$x=\pi$$): $$0 = \left[ \partial_x^2 U(t,x) + a^2 \partial_x U(t,x) \right]_{x = 0}, \quad (1)$$ because $$U(t,0) = 0$$ and $$\cos(U(t,0)) = 1$$ (I use $$a^2$$ instead of 25). Eq.(1) provides a relation between the 1st and 2nd derivatives at $$x = 0$$. This relation should be satisfied for any $$t$$, including $$t = 0$$. Surely, function $$\sin(x)$$ in the IC does not satisfy Eq.(1).
One can modify the BCs and IC to make the problem consistent. For example, $$U(t,0) = \delta, \quad U(t,\pi) = \delta, \quad U(0,x) = \sin(x) +\delta,$$ where $$\delta > 0$$. Also, taking smaller $$a$$ might help. For example, taking $$\delta = 0.01$$ and $$a = 2$$ works fine.
(Minor comment: There is a misprint in your NDSolve command, {x,0,1} should be changed to {x,0,Pi}`).