On Jul 31, 7:08 pm, New2Sympy <anartz.li...@gmail.com> wrote:
> Hi All,
>
> I am trying to get the Real root as shown below and keep getting a
> NameError. Any suggestions?
>
[cut]
> NameError: global name 'conjugate' is not defined
>

When you ask for a numerical solution, the first thing that nsolve
does is evaluate your function and then create a "lambdified" version
of it to pass to the solver. When you evaluate your function it looks
like this:

>>> 1/(.001+a)**3-6/(.9-a)**3
(0.001 + a)**(-3) - 6/(0.9 - a)**3
>>> _.evalf()
3.0e-6*conjugate(a)/(1.0e-6 + 0.001*a + 0.001*conjugate(a) +
a*conjugate(a))**3 + 14.58*conjugate(a)/(0.81 - 0.9*a - 0.9*conjugate
(a) + a*conjugate(a))**3 + 1.0e-9/(1.0e-6 + 0.001*a + 0.001*conjugate
(a) + a*conjugate(a))**3 - 4.374/(0.81 - 0.9*a - 0.9*conjugate(a) +
a*conjugate(a))**3 + 0.003*conjugate(a)**2/(1.0e-6 + 0.001*a +
0.001*conjugate(a) + a*conjugate(a))**3 - 16.2*conjugate(a)**2/(0.81 -
0.9*a - 0.9*conjugate(a) + a*conjugate(a))**3 + conjugate(a)**3/
(1.0e-6 + 0.001*a + 0.001*conjugate(a) + a*conjugate(a))**3 +
6.0*conjugate(a)**3/(0.81 - 0.9*a - 0.9*conjugate(a) + a*conjugate(a))
**3

All those conjugates are there because sympy (I imagine) doesn't know
whether a will be real or imaginary, so it's covering all bases. The
problem is, in order to evaluate this, you need sympy but the only
thing that nsolve passes along to the solver is mpmath's module. The
solution is to pass sympy, too. I also like to watch the iteration
process so I set the verbose flag to true:

>>> nsolve(1/(.001+a)**3-6/(.9-a)**3,a,.3,modules="sympy",verbose=True)
x:     0.315560066156274077187
error: 0.25
x:     0.318099998922522143874
error: 0.234439933843725911711
x:     0.31882324267983762464
error: 0.00253993276624806668673
x:     0.318830099704768381578
error: 0.000723243757315480766644
x:     0.318830113872912139068
error: 0.00000685702493075693761643
x:     0.318830113873185857794
error: 1.4168143757489826177e-8
x:     0.318830113873185911755
error: 2.7371872575728692042e-13
x:     0.318830113873185898265
error: 5.39615044542560845509e-17
x:     0.318830113873185905365
error: 1.34903761135640211377e-17
x:     0.31883011387318590714
error: 7.10020074079996919847e-18
x:     0.318830113873185905587
error: 1.77506342008979314806e-18
mpf('0.31883011387318591')

And there you go!

Notes:

1) If you want to use the sure-fire bisect method, don't forget to
send in two values for the starting point:

>>> nsolve(1/(.001+a)**3-6/(.9-a)**3,a,(.1,.5),modules="sympy",method="bisect")
mpf('0.3188301138731859')

2) For nasty functions like this an alternative approach would be to
find zeros of the numerator instead of the whole expression, or solve
the problem symbolically and substitute in the values when you are
done:

>>> num,den = (1/(.001+a)**3-6/(.9-a)**3).as_numer_denom()
>>> nsolve(num,a,.3) # no need for sympy now
mpf('0.31883011387318584')
>>>

or

>>> var('c1 c2');solve(1/(c1+a)**3-6/(c2-a)**3,a)

Hmmm...it hangs. (I filed this as issue 1571). But we can use the
numerator tric to make things easier for the solver:

>>> num = (1/(c1+a)**3-6/(c2-a)**3).as_numer_denom()[0]
>>> ans=solv(num,a)
>>> [t.subs({c1:.001,c2:.9}).evalf(n=4) for t in ans]
[3.188e-1, 3.216e-2 - 5.706e-1*I, 3.216e-2 + 5.706e-1*I]

Best regards, and welcome to sympy!
/c
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to sympy@googlegroups.com
To unsubscribe from this group, send email to sympy+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sympy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to