0

I am new to Python and I apologise if this a silly question. I am attempting to answer the following question:

A polynomial, (. ), with one variable, , has a finite number of nonzero terms, e.g. () = 2 + + for a degree two polynomial. A root of the polynomial is a solution to the equation = 0, i.e., an such that () = 0. Newton-Raphson theorem implies that if a value, call it , is an approximation to a root of a polynomial, then − ()/’(), where ’ is the first derivative of , is a better approximation. Write a function named NR_Root that accepts as input:

  • A tuple, coefficients, with the coefficients of the polynomial, e.g. (a,b,c)starting from that of the highest order term. The polynomial can be of order two, three, or higher.
  • An initial guess, guess
  • Tolerance, epsilon, for the absolute value of the error away from zero

The definition of the function should be:

def NR_Root(coefficients, guess, epsilon):

The function returns the value of the root.

One way of attempting it is with the following:

def nraphson(fn, x, tol = 0.0001, maxiter =1000):
    for i in range(maxiter):
        xnew = x - fn[0](x)/fn(x)
        if abs(xnew-x) < tol: break
        return xnew, i
        y = [lambda x: a*x**2+b*x+c, lamda x: 2*a*x+b]
  File "<ipython-input-4-5be43bc0c364>", line 6
    y = [lambda x: a*x**2+b*x+c, lamda x: 2*a*x+b]
                                       ^
SyntaxError: invalid syntax

However, I get an invalid syntax and currently cannot resolve this issue. Any guidance on how to proceed would be great.

Thank you.

TMBailey
  • 557
  • 3
  • 14
  • Are you spelling `lambda` right? – TMBailey Nov 27 '21 at 17:53
  • Thank you for spotting this, I've changed the spelling to the correct one – Johnny Azar Nov 27 '21 at 19:11
  • The below is my most recent approach in attempting to solve the aforementioned problem: a=1 b=1 c=1 def p(x): return a*x**2+b*x+c def pprime(x): return 2*a*x+b def NR_Root(coefficients, guess, epsilon): returns: sqrt(x) guess = 1 for val in range(1, 1000): nextGuess = guess-p(guess)/pprime(guess) – Johnny Azar Nov 27 '21 at 19:45
  • If you've got a solution that works, you can post an answer to your own question, in case anybody else wants to see how to do Newton-Raphson. But note that `ax` is not the same as `a*x` and so forth... – TMBailey Nov 27 '21 at 19:47
  • Thanks for letting me know. When I finalize my workings, I will 'Answer my Question' instead of replying. The results are the following (unable to attach the image): 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0.0 0.0 0.0 0.0 999 999 999 999 However, I am not sure if semantically this is correct. One of my doubts is that I have erroneously proceeded with the following: a=1, b=1, c=1 – Johnny Azar Nov 27 '21 at 19:58
  • I submitted some edits to improve the formatting of the original question, but they are probably not visible yet as they are waiting for someone to approve them. Anyway, to check if your function returns a sensible result for this sample problem you can find the roots using the quadratic formula... – TMBailey Nov 27 '21 at 20:06

0 Answers0