solve
Solves an equation for one variable and returns every solution it finds.
Notation
solve(equation, variable[, onInterval])
Write it in an expression: nerdamer('solve(x^2-4,x)'), or in the Playground.
nerdamer.solve
nerdamer.solve(equation: Equation | ExpressionInput, variable?: ExpressionInput, onInterval?: Vector | FunctionSolverOptions): SolutionSet
A method on the default import. Arguments can be strings, as in Notation.
import { solve }
import { solve } from 'nerdamer/solve';
solve(equation: Equation | ExpressionInput, variable?: ExpressionInput, onInterval?: Vector | FunctionSolverOptions): SolutionSet
Import only this function, from nerdamer/solve.
Parameters
equation- An equation such as
x^2=4, or an expression, which is solved for zero. variable- The variable to solve for.
onInterval- Optional. A window
[lower, upper]for the numerical search; the default is[-10, 10]. Exact roots, such as polynomial roots, are returned wherever they are.
Returns
A SolutionSet, written {...}, holding each distinct solution. It is empty, {}, when there is none.
Examples
Other symbols stay symbolic, so this gives the quadratic formula.
solve(a*x^2+b*x+c,x)
// → {(1/2)*a^-1*(-b+(b^2-4*a*c)^(1/2)), (1/2)*a^-1*(-b-(b^2-4*a*c)^(1/2))}Open in PlaygroundA periodic equation returns the whole family of solutions. _n stands for any integer.
solve(sin(x),x)
// → {_n*pi}Open in PlaygroundStore the SolutionSet, then pick one member by giving _n a value inside evaluate. : assigns; = makes an equation.
sol: solve(sin(x),x); evaluate(sol,{_n=>0})
// → {0}Open in PlaygroundAn unknown in an exponent is solved with logarithms.
solve((a+b)^x=y,x)
// → {log(y)*log(a+b)^-1}Open in Playgroundx=0 is left out, because the denominator vanishes there.
solve(0=(x^2-2)/(e^x-1),x)
// → {2^(1/2), -2^(1/2)}Open in PlaygroundRoots that fail the original equation are dropped. Squaring both sides gives 3 and 8, and only 8 works.
solve(x-sqrt(x+1)=5,x)
// → {8}Open in PlaygroundRoots outside the domain of a logarithm are dropped too.
solve(log(x-1)+log(x+3)+log(3)=log(15),x)
// → {2}Open in PlaygroundWith no closed form, roots are found numerically: about 0.309906932380691, and the exact root 4.
solve(x=2^x/4,x)
// → {0.30990693238069053544, 4}Open in Playgroundsin(x)=x/3 has three roots. With a window, only the one between 1 and 5 is searched for.
solve(sin(x)=x/3,x,[1,5])
// → {2.2788626600758283127}Open in PlaygroundThe window only limits the numerical search: polynomial roots are found exactly, so all three come back.
solve(x^3-x,x,[0.5,2])
// → {0, -1, 1}Open in PlaygroundSolve the relativistic kinetic-energy equation for the velocity v.
solve(E_k=m*c^2*((1-v^2/c^2)^(-1/2)-1),v)
// → {-c^2*(c^-2*(1-c^4*m^2*((E_k^2+2*c^2*m*E_k+c^4*m^2)^-1)))^(1/2), c^2*(c^-2*(1-c^4*m^2*((E_k^2+2*c^2*m*E_k+c^4*m^2)^-1)))^(1/2)}Open in PlaygroundGood to know
- Numerical roots are approximate, to the working precision.
- A comparison such as
x^2<4is an error;solvetakes equations, written with=. - The window must be two finite numbers in ascending order, such as
[1,5]; anything else is an error. _nis reserved:_n:0is an error. Give it a value insideevaluate, as above, where the value is local to that call and= denotes an equation, not an assignment.- In JavaScript, substitute an integer into a family member:
nerdamer.solve('sin(x)', 'x').elements[0].subst('_n', 2)gives2*pi. - To solve several equations at once, use
solveeqs.
JavaScript examples
The method takes the same arguments as strings.
nerdamer.solve('x^2-4', 'x').text()
// → {2, -2}Read the solutions one by one from elements.
nerdamer.solve('x^2-4', 'x').elements.map(root => root.text())
// → ["2","-2"]Pick a member of a periodic family.
nerdamer.solve('sin(x)', 'x').elements[0].subst('_n', 2).text()
// → 2*piIn JavaScript, pass the window as a parsed vector.
nerdamer.solve('sin(x)=x/3', 'x', nerdamer('[1,5]')).text()
// → {2.2788626600758283127}