-2

I am learning scientific computing with python. In the exercise, I am supposed to generate a polynomial by using its roots with this formula:

enter image description here

Here is my implementation:

def poly(x,roots):            #Pass real and/or complex roots

    x = symbols(x)
    f = 1            
    for r in roots:
        f = f*(x - r)

    return expand(f)

When I test it:

from sympy import expand
poly('x',[(-1/2), 5, (21/5), (-7/2) + (1/2)*sqrt(73), (-7/2) - (1/2)*sqrt(73)])

I get:

x**5 - 1.7*x**4 - 50.5*x**3 + 177.5*x**2 - 24.8999999999999*x - 63.0

But I should get:

10*x**5 - 17.0*x**4 - 505.0*x**3 + 1775.0*x**2 - 248.999999999999*x - 630.0

Hence, everything is off by a factor of 10. If I set f = 10, it works, but I don't see why I should do that. Am I making an obvious mistake? Thank you!

Johnathan
  • 1,877
  • 4
  • 23
  • 29
  • 2
    The formula is not showing up. Can you fix this please? – Eli Sadoff Nov 17 '16 at 01:43
  • what does symbols() do? – matias elgart Nov 17 '16 at 01:44
  • 1
    You are constructing polynomial with specific roots. If you multiply it's coeffs to any number, the roots will remains the same. – mingaleg Nov 17 '16 at 01:44
  • @EliSadoff Oups! thank you for mentioning it. :) – Johnathan Nov 17 '16 at 01:49
  • @matiaselgart Hi! It's supposed to make Python treat 'x' as an algebraic variable. That's how I was shown, but is there a better way? – Johnathan Nov 17 '16 at 01:52
  • 3
    @Jonathan you shouldn't be getting 10*x**5, the above answer is correct and you can verify this by plugging in all the roots and seeing that for each `x`, `p(x)=0`. While this will also be true with `10 * p(x)`, there's no reason to do this. – Eli Sadoff Nov 17 '16 at 01:54
  • 1
    @EliSadoff OH! I see now: I don't know why I didn't realize this obvious mistake. The authors chose those coefficients, but I have the form with the most reduced coefficients. Thank you!!! – Johnathan Nov 17 '16 at 02:00
  • @EliSadoff Also, if you post your answer, I'll chose it. – Johnathan Nov 17 '16 at 02:01
  • 1
    No worries. I'm a scientific computing guy, so I like helping out with this stuff. – Eli Sadoff Nov 17 '16 at 02:01

2 Answers2

1

While 10x**5 + ... is correct, that is 10 * p(x), which isn't really what is needed. The answer you are getting is fine right now as well and you can test that as for each r in roots, p(r) is 0.

Eli Sadoff
  • 7,173
  • 6
  • 33
  • 61
  • I tested each root. As the book warns me, due to floating-point rounding errors, I get very small numbers close to zero. If please possible, do you have a preferred technique to correct this? – Johnathan Nov 17 '16 at 02:21
  • 1
    @Johnathan You'll generally get floating point errors. I generally set a tolerance of `1e-12` and `abs(p(r)) < 1e-12`, you should be fine. Read more about it [here](http://stackoverflow.com/questions/588004/is-floating-point-math-broken?rq=1). – Eli Sadoff Nov 17 '16 at 02:22
0

x**5 is correct.

As you can see in the formula, there's no coefficient for it.

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176