Summation, equation solving and calculus
This page covers adding up or multiplying a run of terms, finding where an equation holds, differentiating an expression either as a formula or at a single point, and working out definite integrals. Reach for it when a problem needs Σ or Π notation, when you can’t easily rearrange an equation by hand, or when you want to check a derivative or an area under a curve from your coursework.
All of these functions take a variable name as one of their arguments, such as the k in
sum(k^2, k, 1, 10). That name only exists inside the call, so it never changes a value of the
same name elsewhere in your note.
Finite sums and products
sum(k, k, 1, 100) gives 5,050sum(k^2, k, 1, 10) gives 385summation(2*k - 1, k, 1, 10) gives 100product(k, k, 1, 5) gives 120The arguments are the expression, the variable, the first value and the last value. Both ends are
included, so sum(k, k, 1, 100) adds 1 + 2 + … + 100. summation is another name for sum. The
third line shows that the first ten odd numbers add up to 10², and the last is 5 factorial.
Sums work with money and units, which suits regular savings:
# £1,200 paid in at the start of each year for 5 years, at 5%sum(£1200 * 1.05^k, k, 1, 5) gives £6,962.30A product is useful for chains of probabilities. The chance that at least two people in a group of 23 share a birthday is one minus the chance that all 23 birthdays are different:
p = 1 - product((365 - k)/365, k, 0, 22)(p as %) to 1 dp gives 50.7%Some details:
- The first and last values must be whole numbers.
- If the last value is below the first, there are no terms: the sum is 0 and the product is 1.
- A single sum or product can have at most 10,000 terms.
- Sums can be nested, as in
sum(sum(j, j, 1, n), n, 1, 4), which is20. Nested calls share one overall work limit. - To add up values you already have, use a list instead:
sum([4, 8, 15]). See Lists and statistics.
Find one numerical root
solve finds a value of the variable that makes an equation true, searching between two limits
that you give:
solve(x^2 = 2, x, 0, 2) gives 1.4142135624solve(x^2 - 4, x, -3, 0) gives -2solve(cos(x) = x, x, 0, 1) gives 0.7390851332# How long does a stone take to fall 44.1 m?solve(4.9*t^2 = 44.1, t, 0, 10) gives 3# Candles sell for £12, cost £4 each to make, plus £2,000 of fixed costssolve(12*n = 2000 + 4*n, n, 0, 1000) gives 250The arguments are the equation, the variable, the lower limit and the upper limit. You can give an
equation with =, or an expression on its own, which solve treats as expression = 0.
Choosing the limits
solve needs the two sides of the equation to swap which one is bigger somewhere between your
limits. In other words, left - right must be positive at one limit and negative at the other
(or exactly zero at a limit). It then narrows the range down until it finds the crossing point.
This has a few consequences:
- If there are two roots between the limits, the sign may not change overall.
solve(x^2 - 4, x, -3, 3)fails, becausex^2 - 4is positive at both ends. Use limits that surround one root, such as0and3. - A root where the curve only touches zero, such as
x^2 = 0, can’t be found this way, because the sign never changes. - The expression has to be defined across the whole range.
solve(sqrt(x) = 2, x, -1, 10)fails becausesqrtof a negative number is an error. Start the range at0instead. - A jump across zero, as in
1/xnear0, isn’t a root, sosolvegives an error there rather than an answer. - The lower limit must be smaller than the upper limit, and both must be plain numbers.
solve returns one root, never a list of all of them. It works with plain numbers only, so leave
units out of the equation.
Rounding and reuse
The answer is a close numerical approximation and can be out in the last displayed digit. Add
to 4 dp (or any number of places) to the same line for a tidy figure, or name the result first
if you want to use it again:
solve(sqrt(x) = 2, x, 0, 10) gives 4.0000000001solve(sqrt(x) = 2, x, 0, 10) to 4 dp gives 4side = solve(sqrt(x) = 2, x, 0, 10) gives 4.0000000001side * 3 to 2 dp gives 12You can wrap solve in a function of your own to reuse it with different values:
root(y) = solve(x^2 = y, x, 0, 10)root(9) gives 3For several linear equations in several unknowns, use solve_system from
Vectors and matrices.
Symbolic differentiation
With two arguments, differentiate (or derivative) gives the derivative as a formula:
differentiate(sin(x), x) gives cos(x)derivative(x^3, x) gives (3*(x^(3-1)))derivative(x * sin(x), x) gives (sin(x)+(x*cos(x)))derivative(exp(2*x), x) gives (exp((2*x))*2)The result isn’t simplified, and every step is bracketed: (3*(x^(3-1))) is 3x². Decimals in
your expression come back as fractions, so 19.6 appears as (98/5).
The result is text, not something Varlig can calculate with. To use it, type the formula into a function of your own:
slope(x) = 3*x^2slope(2) gives 12What symbolic differentiation handles:
+,-,*,/and powers, using the sum, product, quotient, power and chain rules- the functions
sin,cos,tan,sinh,cosh,exp,lnandsqrt - other names, which are treated as constants and stay as names in the result
A name doesn’t need a value in your note, so you can differentiate a formula with symbols in it:
derivative(a*x^2, x) gives (a*(2*(x^(2-1))))Other functions, such as abs, log or a function you defined yourself, give an error. For those,
use the numerical form below. The formula also doesn’t carry over the original expression’s
restrictions: the derivative of sqrt(x) is only valid where sqrt(x) is, even though
(1/(2*sqrt(x))) looks like a formula of its own.
Numerical differentiation
With a third argument, derivative gives the slope at that point as a number:
derivative(x^3, x, 2) gives 12differentiate(sin(x), x, 0) gives 1derivative(sqrt(x), x, 4) gives 0.25height(t) = 20*t - 4.9*t^2derivative(height(t), t, 1) gives 10.2This form works with functions you’ve defined in the note, like height above, which the
symbolic form can’t. Because it works out a number, every other name in the expression needs a
value. The same goes for sum, product, solve and integrate:
derivative(a*x^2, x, 2) gives Unsupported: Unknown name: aa = 3derivative(a*x^2, x, 2) gives 12The answer is an estimate from values close by on either side of the point. When the true slope is
a tidy number, Varlig stores that number, so comparing it exactly works. Irrational slopes, such
as the slope of exp(x), keep all their digits, so compare those within a tolerance:
derivative(x^3, x, 2) == 12 gives trueabs(derivative(exp(x), x, 1) - e) < 1e-6 gives trueThe expression needs to be smooth, with no corners or gaps, in the area around the point.
derivative(abs(x), x, 0) is an error because abs has a corner at zero, and
derivative(sqrt(x), x, 0) is an error because sqrt isn’t defined to the left of zero.
Definite integration
integrate(x^2, x, 0, 3) gives 9integrate(sin(x), x, 0, pi) gives 2integrate(x^2, x, 3, 0) gives -9integrate(4/(1 + x^2), x, 0, 1) gives 3.1415926536# Distance fallen in 3 s: integrate the speed, 9.8t m/sintegrate(9.8*t, t, 0, 3) gives 44.1The arguments are the expression, the variable, the lower limit and the upper limit, the same
order as sum. The answer is the signed area under the curve between the limits. Swapping the
limits flips the sign, and equal limits give zero. You can integrate functions defined in your
note, such as integrate(power(t), t, 0, 4).
integrate works numerically, so a few things are out of reach:
- There is no indefinite integration: you always get a number, never a formula.
- Both limits must be finite numbers; there’s no way to integrate out to infinity.
- The expression must be defined across the whole range.
integrate(1/sqrt(x), x, 0, 1)is an error, because1/sqrt(0)divides by zero, even though the area itself is finite. - Expressions with jumps, spikes or very fast wiggles can give inaccurate answers or an error.
- Units aren’t accepted, so leave them in a comment, as in the last example.
Algorithms and convergence
You don’t need these details for everyday use, but they help when a result looks wrong or a line gives an error.
solve uses bisection: it halves the range again and again, keeping the half where the sign
changes. It stops when the value of left - right is within 10⁻¹⁰ of zero and the range is narrow
enough, and gives up with an error after 128 halvings.
Symbolic derivative applies the standard rules step by step without simplifying. The
result can be at most 8,192 characters long. This isn’t a full computer algebra system.
Numerical derivative compares central differences over shrinking steps and checks the slope
from each side separately. If those don’t agree, or a value near the point is undefined, you get
an error rather than a guess.
integrate uses adaptive 7- and 15-point Gauss–Kronrod rules. It compares the two estimates
on each piece of the range and splits the piece with the largest estimated error, until the total
estimated error is below 10⁻⁹ × max(1, |answer|). It allows up to 2,048 splits, 20 levels of
splitting and a shared budget of 100,000 steps. The sample points aren’t evenly spaced, so a fast
oscillation such as cos(128*pi*x) over 0 to 1 correctly integrates to about zero rather
than being mistaken for a constant. The error estimate is still a heuristic, so treat answers for
expressions with singularities or discontinuities with care.
The full rules are in the advanced mathematics reference.
Putting it together
Here is a check on a physics homework problem: a ball thrown straight up at 19.6 m/s, taking gravity as 9.8 m/s²:
# Height in metres after t secondsheight(t) = 19.6*t - 4.9*t^2velocity(t) = 19.6 - 9.8*t# Lands when the height is back to zero (skip t = 0)landing = solve(height(t) = 0, t, 1, 10) gives 4# Speed after 1 second, from the slope of the heightderivative(height(t), t, 1) gives 9.8# Highest point, where the velocity is zeropeak = solve(velocity(t) = 0, t, 0, landing) gives 2height(peak) gives 19.6# Check: integrating the velocity up to the peak gives the same heightintegrate(velocity(t), t, 0, peak) gives 19.6The ball lands after 4 seconds and reaches 19.6 m at 2 seconds. The lower limit of 1 in the
landing line keeps solve away from the other root at t = 0. The last line confirms the
height a second way, by integrating the velocity.