# Integrate a solution Use this when you need the integral of an unknown over its domain: the heat that a plate holds, the total of a substance, a mean value. ## Steps 1. Call `solution.integral()`. It returns the integral of the unknown over the domain, as a number. 2. For a problem in time, read the result as an array with one integral for each recorded time. 3. With several unknowns, name the one you mean: `solution.integral(a)`. ## Example A rod that is hot in the middle and kept cold at both ends. Its heat leaves through the ends: ```python import numpy as np import sympy as sp import pdemagic as pm x, t = sp.symbols("x t", real=True) T = sp.Function("T")(x, t) rod = pm.Problem( sp.Eq(T.diff(t), T.diff(x, 2)), domain=pm.Box({x: (0, 1)}), time=(t, 0, 0.2), initial={T: sp.sin(sp.pi * x)}, boundary=sp.Eq(T, 0), ) cooling = pm.solve(rod) cooling.plot() heat = cooling.integral() assert heat.shape == cooling.times.shape # one integral for each time # The integral of sin(πx) is 2/π, and it decays at the rate π². exact = 2 / np.pi * np.exp(-(np.pi**2) * cooling.times) assert abs(heat - exact).max() < 1e-12 # What is left of the heat at the end. left = heat[-1] / heat[0] assert abs(left - np.exp(-0.2 * np.pi**2)) < 1e-12 ``` ## Notes - The integral is taken by the quadrature of the points of the grid, and for a resolved solution it is exact up to rounding. No values are interpolated. - The coordinates of the domain bring their measure: on a disk the integral is over its area, in a ball and a cylinder over the volume. On a shape in the plane it is over the shape, up to its boundary. - Where a direction is free, the integral is over the box of the grid, not over all of space. For a solution that has not faded at the faces of the box it depends on the box. - Only an unknown itself is integrated. For the integral of its square or of its energy, take the arrays: see [Get the arrays and the grid](get-the-arrays-and-the-grid.md). - For an eigenproblem the result holds one number for each state. - [Solutions](../topics/solutions.md) explains what a solution offers, and the page about [the methods](../methods/index.md#values-between-the-points-of-a-grid) says how values and integrals are taken.