Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

using python latest version #---------------------------------------------------

ID: 3734746 • Letter: U

Question

using python latest version

#-------------------------------------------------------------

def NewtonsMethod(F,FP,x0,epsilon) :

#-------------------------------------------------------------

provides missing code that implements the Newton-Raphson Method

      algorithm used in the C source code shown below. Pass lambda functions

      actual parameters for F() and FP() in lieu of call-back function pointers

      that Python does not support! Try epsilon = 10-3 or smaller.

//------------------------------------------------------------

// C function that returns the root of F() "close to" x0 using Newton-Raphson Method

//------------------------------------------------------------

double NewtonsMethod(double (*F)(double x),double (*FP)(double x),

                     double x0,double epsilon)

//------------------------------------------------------------

{

   double x = x0

   do

   {

      x = x - F(x)/FP(x);

   } while ( fabs(F(x)) >= epsilon );

   return( x );

}

Explanation / Answer

There is no call-back function pointers in python

You can individually define the function and run it in loop

def func( x ):

    return x - x * x + 2 # return some function

# Derivative of the above function

# which is 1 - 2*x

def derivFunc( x ):

    return 1 - 2 * x

# Function to find the root

def newtonRaphson( x ):

    h = func(x) / derivFunc(x)

    while abs(h) >= 0.0001:

        h = func(x)/derivFunc(x)

         

        # x(i+1) = x(i) - f(x) / f'(x)

        x = x - h

     print("The value of the root is : ", "%.4f"% x)