# Finding complex roots numerically using sage

Can sage find complex roots numerically for equations like

cos(cos(cos(cos(x)))) == sin(sin(sin(sin(x))))

if so, how?

4

You can use `scipy.optimize.fsolve`

to find a solution, but you'll need to set up things slightly differently. You'll want to consider your function as a map from `RR^2 -> RR^2`

as opposed to `CC -> CC`

.

```
sage: x, y = var('x,y')
sage: g = cos(cos(cos(cos(x+i*y)))) - sin(sin(sin(sin(x+i*y))))
sage: f(x,y) = (real(g), imag(g))
sage: from scipy.optimize import fsolve
sage: fsolve(lambda v: f(*v), (1.0,1.0))
array([ 0.75688714, 0.610155 ])
sage: f(*_)
(1.87627691162e-13, -4.80504525058e-13)
```

You'll have to be careful with your initial guess as the procedure may not converge.

```
sage: fsolve(lambda v: f(*v), (0.0,0.0))
array([ 1.01756188e+00, 1.17664027e-08])
sage: f(*_)
(0.165849713543, -9.91343156065e-13)
```

SciPy should give a warning the first time this happens, but it's best to use the `full_output`

option to be sure:

```
sage: fsolve(lambda v: f(*v), (0.0,0.0), full_output=1)
(array([ 1.01756188e+00, 1.17664027e-08]), {'qtf': array([ -1.65849759e-01, -2.24024475e-06]), 'nfev': 26, 'fjac': array([[ -1.00000000e+00, 1.35076898e-05],
[ -1.35076898e-05, -1.00000000e+00]]), 'r': array([ 3.14707173e-04, 3.50982324e-09, -2.59845204e-04]), 'fvec': array([ 1.65849714e-01, -9.91343156e-13])}, 5, 'The iteration is not making good progress, as measured by the \n improvement from the last ten iterations.')
```

(Note the "The iteration is not making good progress" comment. See the SciPy documentation for the specifics on the output given by `full_output`

.)

Looking at question 500 will also be useful. It probably wouldn't be too difficult to make a something that does this behind the scenes.

1

Here is one way to find a solution, bypassing the complex nature of the problem:

```
sage: var('z')
z
sage: f = cos(cos(cos(cos(z)))) - sin(sin(sin(sin(z))))
sage: var('a,b', domain=RR)
(a, b)
sage: M = f(z=a+b*i).real()^2+f(z=a+b*i).imag()^2
sage: minimize(M,[0,0],algorithm='powell',disp=0)
(0.756887137348, 0.61015499692)
sage: M(a=_[0],b=_[1])
6.39826537371e-26
```

You can try various starting points and other algorithms (ncg,bfgs) - they give different results.

In Maple things are much simpler:

fsolve(cos(cos(cos(cos(x)))) = sin(sin(sin(sin(x)))),x,complex);

-0.7401150735 - 1.364789582 I

so it is possible...

0

Here is something that doesn't answer your question, but could at least help you narrow it down some - and perhaps some code in it could help.

```
sage: f = cos(cos(cos(cos(x)))) - sin(sin(sin(sin(x))))
sage: complex_plot(f,(-pi,pi),(-pi,pi),plot_points=500)
```

This will show the results of `f`

on this square. The darker the area, the smaller the modulus (color indicates the argument of the image of that point). You can compare with the identity map to see what is what.

```
sage: complex_plot(x,(-pi,pi),(-pi,pi),plot_points=500)
```

0

Sorry - I didn't read the "complex" part! Ignore the answer below.

Even in Maxima this seems to mostly be available for polynomials only, which is also true in Sage. I'd be interested in any answer to this. See my other answer for a poor, but cool-looking, substitute.

+++++

Usually the `find_root`

is pretty useful here - see the example below. However, this particular situation has no (real) roots, I think.

```
sage: f = cos(cos(cos(cos(x)))) == sin(sin(sin(sin(x))))
sage: f.find_root(-100,100)
------------------------------------------
RuntimeError: f appears to have no zero on the interval
```

Plotting it makes this clear, plus the obvious periodicity:

```
sage: plot(f,-100,100)
```

Asked: **
2011-07-02 14:04:50 -0500
**

Seen: **2,355 times**

Last updated: **Jul 03 '11**

"partial_fraction_decomposition" with possibly "complex roots", again

Change Precision of complex_roots()

Question about error using plot

inverse_laplace of a fraction whose denominator has real roots (or complex)

desolve (how can i enable numeric?)

obtaining all numerical roots of a function in an interval

Roots of multivariable polynomials with respect to one variable?

Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.