Solving equations with SageMath
Exact solutions : solve
Let us start with a simple example: solving for :
Note that the equation is formed with the operator ==
.
The equation itself can be stored in a Python variable:
The solutions are returned as a list:
Each element of the solution list is itself an equation, albeit a trivial one. This can be seen by using the print
function instead of the default LaTeX display:
The access to the value of the solution is via the rhs()
method:
A numerical approximation, n
being a shortcut for numerical_approx
:
A new equation involving the above solution:
Asking Sage whether this equation always holds:
A system with various unknowns
Since x
is the only predefined symbolic variable in Sage, we need to declare the other symbolic variables denoting the unknowns, here y
:
Then we may form the system:
Here again the solutions are returned as a list; but now, each item of the list is itself a list, containing the values for x
and y
:
Approximate solution: find_root
Let us consider the following transcendental equation:
solve
returns a non-trivial equation equivalent to its input, meaning it could not find a solution:
We use then find_root
to get an approximate solution in a given interval, here :
Actually find_root
always return a single solution, even if there are more than one in the prescribed interval. In the current case, there is a second solution, as we can see graphically:
We can get it by forcing the search in the interval :
Approximate solution to a system: mpmath.findroot
For solving a system, we use the mpmath toolbox: