Newton-Raphson method application to a quadratic formulae

152 Views Asked by At

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.

0

There are 0 best solutions below