# 11.54 solve

solve { <equation> } via { <variable> }

The `solve` command can be used to solve simple systems of one or more equations numerically. It takes as its arguments a comma-separated list of the equations which are to be solved, and a comma-separated list of the variables which are to be found. The latter should be prefixed by the word `via`, to separate it from the list of equations.

Note that the time taken by the solver dramatically increases with the number of variables which are simultaneously found, whereas the accuracy achieved simultaneously decreases. The following example solves a simple pair of simultaneous equations of two variables:

`pyxplot> `**solve x+y=10, x-y=3 via x,y**

`pyxplot> `**print x**

`6.5`

`pyxplot> `**print y**

`3.5`

No output is returned to the terminal if the numerical solver succeeds, otherwise an error message is displayed. If any of the fitting variables are already defined prior to the `solve` command’s being called, their values are used as initial guesses, otherwise an initial guess of unity for each fitting variable is assumed. Thus, the same `solve` command returns two different values in the following two cases:

`pyxplot> `**x= ***# Undefine x*

`pyxplot> `**solve cos(x)=0 via x**

`pyxplot> `**print x/pi**

`0.5`

`pyxplot> `**x=10**

`pyxplot> `**solve cos(x)=0 via x**

`pyxplot> `**print x/pi**

`3.5`

In cases where any of the variables being solved for are not dimensionless, it is essential that an initial guess with appropriate units be supplied, otherwise the solver will try and fail to solve the system of equations using dimensionless values:

x = unit(m)
y = 5*unit(km)
solve x=y via x

The `solve` command works by minimising the squares of the residuals of all of the equations supplied, and so even when no exact solution can be found, the best compromise is returned. The following example has no solution – a system of three equations with two variables is over-constrained – but Pyxplot nonetheless finds a compromise solution:

`pyxplot> `**solve x+y=10, x-y=3, 2*x+y=16 via x,y**

`pyxplot> `**print x**

`6.4220634`

`pyxplot> `**print y**

`3.4266948`

When complex arithmetic is enabled, the `solve` command allows each of the variables being fitted to take any value in the complex plane, and thus the number of dimensions of the fitting problem is effectively doubled – the real and imaginary components of each variable are solved for separately – as in the following example:

`pyxplot> `**set numerics complex**

`pyxplot> `**solve exp(x)=e*i via x**

`pyxplot> `**print Re(x)**

`-1227.7`

`pyxplot> `**print Im(x)/pi**

`0`