# Systems of equations A problem can have several unknowns, with as many equations: the fields of Maxwell's equations, substances that react with each other, a plate and its bending moment. This page says which systems are solved where, and how the unknowns of a solution are told apart. ```python import numpy as np import sympy as sp import pdemagic as pm from pdemagic import curves x, y, t = sp.symbols("x y t", real=True) D = sp.Derivative ``` ## Several unknowns, as many equations The equations are given one after the other, and `initial=` holds a state for each unknown. Maxwell's equations in vacuum, for a wave in the plane whose electric field points out of it: ```python E = sp.Function("E")(x, y, t) Hx = sp.Function("Hx")(x, y, t) Hy = sp.Function("Hy")(x, y, t) light = pm.Problem( sp.Eq(D(E, t), D(Hy, x) - D(Hx, y)), sp.Eq(D(Hx, t), -D(E, y)), sp.Eq(D(Hy, t), D(E, x)), domain=pm.FreeSpace(x, y), time=(t, 0.0, 4.0), initial={ E: sp.exp(-(x**2 + (y - 1) ** 2)) - sp.exp(-(x**2 + (y + 1) ** 2)), Hx: 0, Hy: 0, }, ) flash = pm.solve(light) assert flash.unknowns == ("E", "Hx", "Hy") assert flash[Hx].shape == flash[E].shape flash.plot(E) ``` Two pulses of opposite sign spread as rings, and cancel along the line between them. The solution holds every unknown: `flash[E]`, `flash[Hx]`. Where one unknown is meant, it is named, as in `flash.plot(E)`. Without a name the first one is drawn. pdemagic recognised that the three equations describe waves that travel with the speed 1, and chose the box so that it holds everything they reach by the end of the time span. Such a system is linear with constant coefficients. Its equations can be of first or second order in time, also mixed, and need not be solved for their derivatives in time. It is propagated exactly, like one [linear equation in time](time.md). In free space the equations must describe waves. On a domain that is periodic in every direction any such system is solved, stationary ones included. ## Nonlinear systems On a domain that is periodic in every direction the equations of a system may be nonlinear. Two substances that diffuse and react with each other, the Gray–Scott model, form patterns: ```python tile = pm.Space(x, y, periodic={x: (0, 1), y: (0, 1)}) a = sp.Function("a")(x, y, t) b = sp.Function("b")(x, y, t) seed = sp.exp(-((x - 0.5) ** 2 + (y - 0.5) ** 2) / 0.01) reaction = pm.Problem( sp.Eq(D(a, t), 2e-5 * pm.laplacian(a) - a * b**2 + 0.037 * (1 - a)), sp.Eq(D(b, t), 1e-5 * pm.laplacian(b) + a * b**2 - 0.097 * b), domain=tile, time=(t, 0.0, 1000.0), initial={a: 1 - seed / 2, b: seed / 2}, ) pattern_plan = pm.plan(reaction) pattern_plan.times.count = 3 pattern_plan.method.tolerance = 1e-4 pattern = pm.solve(reaction, pattern_plan) assert pattern.plan.method.kind == "exponential_runge_kutta" assert 0.3 < pattern[b][-1].max() < 0.5 pattern.plot(b) ``` The diffusion is exact, and the reaction is stepped, as for one [nonlinear equation in time](nonlinear-time.md), here with a looser tolerance than the planner's. The gallery lets [the pattern](../gallery/patterns-from-a-reaction.md) grow further. An equation of second order in time may be nonlinear as well. The sine-Gordon equation, `sp.Eq(D(u, t, 2), D(u, x, 2) - sp.sin(u))`, is solved as a system for `u` and its rate of change. ## Constraints An unknown without a derivative in time is fixed by an equation without one, at every time. A quantum particle in the potential of its own charge, for example: the Schrödinger equation with a potential that obeys Poisson's equation. ```python ring = pm.Space(x, periodic={x: (0, 2 * sp.pi)}) psi = sp.Function("psi")(x, t) phi = sp.Function("phi")(x, t) self_consistent = pm.Problem( sp.Eq(sp.I * D(psi, t), -D(psi, x, 2) / 2 + phi * psi), sp.Eq(D(phi, x, 2), sp.Abs(psi) ** 2), domain=ring, time=(t, 0.0, 2.0), initial={psi: 1 + sp.cos(x) / 4}, ) bound = pm.solve(self_consistent) assert set(bound.unknowns) == {"psi", "phi"} assert bound[phi].shape == bound[psi].shape ``` The potential needs no initial value: it follows from the state. On a periodic domain a uniform charge has no potential, so the mean of the density is left out, as for Poisson's equation alone. ## Between walls On a [box with walls](boxes.md) and on a [round domain](round.md) a system in time has an equation of first or second order in time for each unknown, and each unknown has its own condition on every face: a list of equations stands where one stood, and each holds one unknown. A reservoir holds `p` at 1 on both walls, and `q` feeds on it and cannot pass them: ```python rod = pm.Box({x: (0, 1)}) p = sp.Function("p")(x, t) q = sp.Function("q")(x, t) fed = pm.Problem( sp.Eq(D(p, t), D(p, x, 2) / 10 - p * q), sp.Eq(D(q, t), D(q, x, 2) / 20 + p * q - q / 2), domain=rod, time=(t, 0, 20), initial={p: 1, q: sp.sin(sp.pi * x) ** 2 / 2}, boundary=[sp.Eq(p, 1), sp.Eq(pm.normal_derivative(q), 0)], ) eaten = pm.solve(fed) # In the middle q has eaten most of p. The walls still hold it at 1. assert np.allclose(eaten([0.0, 1.0], unknown=p), 1) assert eaten(0.5, unknown=p)[-1] < 0.3 print(eaten.plan.notes[1]) ``` ```text Boundary conditions: for `p`: the value at x = 0, the value at x = 1; for `q`: the derivative at x = 0, the derivative at x = 1. ``` Each unknown is expanded in the modes of its own operator with its own conditions, in which its diffusion or its wave equation is exact. See [a pulse between walls](../gallery/pulse-between-walls.md), [a string on a bed that hardens](../gallery/string-on-a-bed-that-hardens.md), and, for a constraint as above between walls, [a cloud of ions between two plates](../gallery/cloud-of-ions.md). ## On a shape On a [shape in the plane](regions.md) each unknown has its own condition on every piece of the boundary, as between walls. A stationary system has an equation of second order for each unknown. Each equation holds the second derivatives of one unknown, and the others with their values and first derivatives, or in nonlinear terms. That makes an equation of fourth order solvable. A plate that rests on its edge under an even load obeys $\nabla^4 w = 1$, with the deflection $w$ and the bending moment $\nabla^2 w$ zero on the edge: ```python w = sp.Function("w")(x, y) m = sp.Function("m")(x, y) disk = pm.Region(x, y, curves.Circle((0, 0), 1)) plate = pm.Problem( sp.Eq(pm.laplacian(w), m), sp.Eq(pm.laplacian(m), 1), domain=disk, boundary=[sp.Eq(w, 0), sp.Eq(m, 0)], ) bent = pm.solve(plate) # On a disk the deflection is (r⁴ − 4r² + 3)/64. assert abs(bent(0, 0, unknown=w) - 3 / 64) < 1e-10 ``` The system for all unknowns is solved at once, directly, and with nonlinear terms by Newton's method, from the starting values in `guess`. On a box, a plate is one equation with two conditions on each face: see [Beams and plates](plates.md). A system in time is solved as between walls, in the modes of the shape, with constraints as well: see [a cloud of ions in a dish](../gallery/cloud-of-ions-in-a-dish.md). A substance that is turned into another one, $p + q \to 2q$, on a dish: ```python p = sp.Function("p")(x, y, t) q = sp.Function("q")(x, y, t) turning = pm.Problem( sp.Eq(p.diff(t), pm.laplacian(p) / 10 - p * q), sp.Eq(q.diff(t), pm.laplacian(q) / 20 + p * q), domain=pm.Region(x, y, curves.Ellipse((0, 0), (1.5, 1))), time=(t, 0, 6), initial={p: 1, q: sp.exp(-2 * ((x + 0.8) ** 2 + y**2)) / 2}, boundary=[sp.Eq(pm.normal_derivative(p), 0), sp.Eq(pm.normal_derivative(q), 0)], ) turning_plan = pm.plan(turning) turning_plan.method.tolerance = 1e-6 turned = pm.solve(turning, turning_plan) # What p loses, q gains, and nothing passes the rim. both = turned.integral(p) + turned.integral(q) assert np.allclose(both, both[0], rtol=1e-6) turned.plot(q, time=3) ``` ## What is not solved yet Between walls and on round domains: stationary systems. There and on a shape: conditions that hold several unknowns. The [known limits](../dev/limits.md) list them all. ## See also - [Solutions](solutions.md), for the unknowns of a solution, and [Plotting](plotting.md). - Reference: [what is solved where](../ref/equations.md). - Gallery: [a flash of light](../gallery/flash-of-light.md), [patterns from a reaction](../gallery/patterns-from-a-reaction.md), [a reaction in a dish](../gallery/reaction-in-a-dish.md) and [a kink and an antikink](../gallery/kink-and-antikink.md). - Methods: [systems of linear equations](../methods/coupled.md), [nonlinear equations in time](../methods/exponential.md), [systems between walls](../methods/systems-between-walls.md), [systems on a shape](../methods/element-systems.md) and [shapes in the plane, in time](../methods/elements-in-time.md).