2

Not sure about the syntax of the output I am receving. Any help would be appreciated. Here is my code:

import numpy

def g(): #generate random complex values
    return numpy.random.random(1) + numpy.random.random(1) *1j

p = numpy.poly1d(numpy.squeeze([g(),g(),g()]))  # test function p
pprime = numpy.polyder(p) #derivative of p

print 'Our p(x) is {} '. format(p)
print('\n') # new line
print'Our pprime(x) is {} '. format(pprime)  #apply newtons method to p
print('\n') # new line

#apply newtons method to p
def root_newton ( f, df, tolerance = 1.0e-6):
    dx = 2 * tolerance
    x=0
    while dx > tolerance:
        x1 = x - f(x)/df(x)
        dx = abs (x - x1)
        x = x1
    return x
print('Our first root is at {}'.format(root_newton(p,pprime)))
print('\n') # new line

Here's the output:

Our p(x) is                    2
(0.6957 + 0.683j) x + (0.3198 + 0.5655j) x + (0.9578 + 0.1899j) 


Our pprime(x) is  
(1.391 + 1.366j) x + (0.3198 + 0.5655j) 


Our first root is at (0.00925817978737+0.830966156841j)


The correct roots are [-0.64968928-1.01513333j  0.00925818+0.83096616j]

What does the 2 above the second component in my first line outputted mean? I can't find anything similar to my question online. I am guessing it may mean the x component is squared but I'm not sure? This is python 3 by the way.

1 Answer 1

4

The 2 is the exponent on the first x, misaligned because you put text before it on the same line.

If we take your output:

Our p(x) is                    2
(0.6957 + 0.683j) x + (0.3198 + 0.5655j) x + (0.9578 + 0.1899j)

and remove the text you prepended:

                   2
(0.6957 + 0.683j) x + (0.3198 + 0.5655j) x + (0.9578 + 0.1899j)

the intended meaning of the 2 becomes clearer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.