23.3.2 Finding approximate solutions of equations involving one variable
The fsolve
or nSolve
command can solve equations or systems of
equations. This section will discuss solving equations; systems will
be discussed in the next section.
The cfsolve
command is the complex version of
fsolve, with the same arguments. The only difference is that
cfsolve gives numeric solutions over the complex numbers,
even if Xcas is not in complex mode (see
Section 2.5.5). fsolve will return complex roots,
but only in complex mode.
fsolve solves numeric equations of the form:
Unlike solve (Section 9.3.6) or proot
(Section 23.3.4), it is not limited to polynomial equations.
-
fsolve takes one mandatory argument and three
optional arguments:
-
eqn, an equation involving one variable.
- Optionally, var, the variable (by default
x).
- Optionally, init, an initial approximation or range.
- Optionally, method, the name of an iterative method to be
used by the GSL solver. The possible values are:
-
bisection_solver.
-
This algorithm of dichotomy is the simplest but also generally the
slowest. It encloses the zero of a function on an interval. Each
iteration cuts the interval into two parts, the middle
point value is calculated. The function sign at this point gives you the
half-interval on which the next iteration will be performed.
- brent_solver.
-
The Brent method interpolates of f at three points, finds the
intersection of the interpolation with the x axis, computes the sign
of f at this point and chooses the interval where the sign changes.
It is generally faster than bisection.
- falsepos_solver.
-
The “false position” algorithm is an iterative algorithm based on
linear interpolation: it computes the value of f at the intersection
of the line (a,f(a)), (b,f(b)) with the x axis. This value gives
us the part of the interval containing the root, and on which a new
iteration is performed. The convergence is linear but generally
faster than bisection.
- newton_solver.
-
This is the classic Newton method. The algorithm
starts at an initial value x0, then finds the intersection
x1 of the tangent at x0 to the graph of f, with the x axis,
the next iteration is done with x1 instead of x0. The xi
sequence is defined by
If the Newton method converges, then the convergence is quadratic for
roots of multiplicity 1.
- secant_solver.
-
The secant method is a simplified version of Newton’s method. The
computation of x1 is done using Newton’s method, but then the
computation of f′(xn), n>1 is done approximately. This method is
used when the computation of the derivative is expensive:
The convergence for roots of multiplicity 1 is of order
(1+√5)/2 ≈ 1.62… .
- steffenson_solver.
-
The Steffenson method is generally the fastest method. It combines
Newton’s method with a “delta-two” Aitken acceleration: with
Newton’s method, you get the sequence xi and the convergence
acceleration gives the Steffenson sequence
Ri=xi− | (xi+1−xi)2 |
|
(xi+2−2 xi+1+xi) |
| .
|
- fsolve(eqn ⟨,var,init,method ⟩)
returns an approximate solution to the equation eqn.
Examples
Input in real mode:
Input in complex mode:
|
| ⎡
⎣ | −0.5−0.866025403784i,−0.5+0.866025403784i,1.0 | ⎤
⎦ |
| | | | | | | | | | |
|
Input in any mode:
|
| ⎡
⎣ | −0.5−0.866025403784 i,−0.5+0.866025403784 i,1.0 | ⎤
⎦ |
| | | | | | | | | | |
|
|
| ⎡
⎣ | 1.57079632679−1.31695789692 i,1.57079632679+1.31695789692 i | ⎤
⎦ |
| | | | | | | | | | |
|
fsolve((cos(x))=x,x,-1..1,brent_solver) |