# Nonlinear stationary equations A stationary equation may hold a term that is nonlinear in the unknown: `sp.sinh(u)`, `u**3`, `sp.exp(u)`. Such an equation is solved by Newton's method, and it can have several solutions. This page says what that means for you: where the method starts decides which solution you get. ```python import math import numpy as np import sympy as sp import pdemagic as pm from pdemagic import curves x, y = sp.symbols("x y", real=True) ``` ## Newton's method In an electrolyte the charges that screen a potential respond to it exponentially, which gives the Poisson–Boltzmann equation: ```python c = sp.Function("c")(x, y) charge = 8 * sp.exp(-(x**2 + y**2)) electrolyte = pm.Problem( sp.Eq(-pm.laplacian(c) + sp.sinh(c), charge), domain=pm.FreeSpace(x, y) ) double_layer = pm.solve(electrolyte) assert double_layer.plan.method.kind == "fourier_newton" assert double_layer.diagnostics["newton_steps"] < 10 # Where the potential is large the screening is stronger than a linear # equation would have it. debye = pm.solve( pm.Problem(sp.Eq(-pm.laplacian(c) + c, charge), domain=pm.FreeSpace(x, y)) ) assert double_layer[c].max() < 0.9 * debye[c].max() ``` You state the equation as any other. The planner sees that it is nonlinear and chooses Newton's method: a sequence of linear equations, each solved by the method of the domain, that closes in on a solution. The diagnostic `newton_steps` counts them, and `plan.method.tolerance` and `plan.method.max_steps` set when it stops. The nonlinear term may be any function of the unknown itself and of the position. Derivatives of the unknown stand in the linear part, which may be any of the linear equations that the domain takes. ## Several solutions, and the starting value A nonlinear equation can have several solutions. Newton's method finds one near its starting value, which is zero unless you give another, as `guess`. The problem of Bratu, $v'' + e^v = 0$ on a string that is held at both ends, has two: ```python v = sp.Function("v")(x) string = pm.Box({x: (0, 1)}) bratu = sp.Eq(v.diff(x, 2) + sp.exp(v), 0) lower = pm.solve(pm.Problem(bratu, domain=string, boundary=sp.Eq(v, 0))) upper = pm.solve( pm.Problem(bratu, domain=string, boundary=sp.Eq(v, 0), guess={v: 16 * x * (1 - x)}) ) assert lower.plan.method.kind == "collocation_newton" assert abs(lower(0.5) - 0.1405392) < 1e-6 assert abs(upper(0.5) - 4.0914672) < 1e-6 ``` The plan says in its last note that the solution is the one that Newton's method reaches from its start. pdemagic does not look for the others: a starting value near each one is yours to give. The same works on a disk, a ring, a ball, a shell and a cylinder with boundary conditions. ## In free space The starting value is a formula, a number or an [array](data.md). The equation $-u'' + u - u^3 = 0$ is solved by zero, and by a wave that stands alone: ```python s = sp.Function("s")(x) alone = pm.Problem( sp.Eq(-sp.Derivative(s, x, 2) + s - s**3, 0), domain=pm.FreeSpace(x), guess={s: 1.2 * sp.exp(-(x**2) / 2)}, ) plan = pm.plan(alone) # The solution reaches further than the starting value. plan.grid.lower, plan.grid.upper, plan.grid.shape = [-12.0], [12.0], [192] soliton = pm.solve(alone, plan) (points,) = soliton.mesh() assert abs(soliton[s] - np.sqrt(2) / np.cosh(points)).max() < 1e-8 ``` The planner puts the box around the starting value. A solution that reaches further shows in the diagnostic `source_boundary_ratio`, and the box can be widened in the plan, as here. Along free directions the equation must settle far away to a Poisson or a screened Poisson equation, as one with [coefficients that vary](varying.md) must. ## On a shape On a [shape in the plane](regions.md) the equation $\nabla^2 u + e^u = 0$ with zero on the rim of the unit disk has two solutions as well, both of the form $\ln\bigl(8b / (1 + b\,r^2)^2\bigr)$. From zero, Newton's method reaches the lower one, with $b = 3 - 2\sqrt 2$. A `guess` near the other one leads there: ```python u = sp.Function("u")(x, y) disk = pm.Region(x, y, curves.Circle((0, 0), 1)) swelling = sp.Eq(pm.laplacian(u) + sp.exp(u), 0) low = pm.solve(pm.Problem(swelling, domain=disk, boundary=sp.Eq(u, 0))) high = pm.solve( pm.Problem( swelling, domain=disk, boundary=sp.Eq(u, 0), guess={u: 4 * (1 - x**2 - y**2)}, ) ) assert low.plan.method.kind == "element_newton" assert abs(low(0, 0) - math.log(8 * (3 - 2 * math.sqrt(2)))) < 1e-9 assert abs(high(0, 0) - math.log(8 * (3 + 2 * math.sqrt(2)))) < 1e-8 ``` The nonlinear term may depend on the position as well, and the rest of the equation may be any of the linear ones that a region takes. ## When Newton's method does not arrive Not every start leads to a solution, and not every equation has one. The string above has none once the nonlinear term is five times as strong. pdemagic then raises `pm.ConvergenceError` and returns nothing: ```python too_strong = pm.Problem( sp.Eq(v.diff(x, 2) + 5 * sp.exp(v), 0), domain=string, boundary=sp.Eq(v, 0) ) try: pm.solve(too_strong) except pm.ConvergenceError as error: message = str(error) assert message.endswith("Give a starting value that is closer to the solution") ``` A better starting value is the first thing to try. The method page says when Newton's method fails, and what else to know. ## What does not work yet - A derivative of the unknown inside a nonlinear term, as in $u\,u_x$. - Between walls, an equation whose linear part does not fix the solution by itself: with the slope given on every face it needs a linear term that holds the unknown. - On boxes and round domains with boundary conditions, an array in the nonlinear term or as the starting value: there both are formulas. - Following a solution as a parameter changes. ## See also - [Nonlinear equations in time](nonlinear-time.md): the other kind, which is stepped. - Reference: [what is solved where](../ref/equations.md), and the [error classes](../ref/errors.md). - Gallery: [ions around a rod](../gallery/ions-around-a-rod.md), [the two states of Bratu](../gallery/two-states-of-bratu.md). - Methods: [nonlinear stationary equations](../methods/newton.md), [between walls](../methods/iteration-between-walls.md#nonlinear-equations), [on a shape](../methods/element-newton.md). - Recipe: [pick the solution that Newton's method finds](../howto/pick-the-solution-newton-finds.md).