Function reference · Calculus

diff

Differentiates an expression with respect to a variable.

It uses the product, quotient, power and chain rules on algebraic expressions and the built-in functions. Pass an order for higher derivatives.

Notation

diff(expression[, variable][, order])

Write it in an expression: nerdamer('diff(x^2+2*x+1,x)'), or in the Playground.

nerdamer.diff

nerdamer.diff(expression: Expression, variable?: ExpressionInput, order?: number | Expression): Expression

A method on the default import. Arguments can be strings, as in Notation.

import { diff }

import { diff } from 'nerdamer/calculus';
diff(expression: ExpressionInput, variable?: ExpressionInput, order?: number | Expression): Expression

Import only this function, from nerdamer/calculus.

Parameters

expression
Expression to differentiate.
variable
Variable to differentiate with respect to.
order
Optional derivative order.

Examples

A polynomial.

diff(x^2+2*x+1,x)
// → 2+2*x
Open in Playground

The chain rule through a power of a sine.

diff(5*sin(x^2)^2,x)
// → 20*x*cos(x^2)*sin(x^2)
Open in Playground

A variable raised to a variable power.

diff(x^sin(x),x)
// → x^sin(x)*(x^-1*sin(x)+cos(x)*log(x))
Open in Playground

The third derivative.

diff(sin(x),x,3)
// → -cos(x)
Open in Playground

Leave out the variable when the expression has only one.

diff(x^2)
// → 2*x
Open in Playground

A vector is differentiated element by element.

diff([x,x^2])
// → [1, 2*x]
Open in Playground

A value stored under the variable's name isn't substituted, so this is still 2*x.

x:5; diff(x^2,x)
// → 2*x
Open in Playground

Other stored values are substituted as usual.

a:2; diff(a*x^2,x)
// → 4*x
Open in Playground

Good to know

  • Without a variable, diff uses the only variable left after stored values are substituted: after x:5, diff(x^2) gives 0. With more than one variable, give it: diff(x*y) is an error.

← Back to Function reference