Question: Write a python program to find a real root of the equation, where equation is taken as input from the user, using the Newton-Raphson method.Write

 Write a python program to find a real root of the

Write a python program to find a real root of the equation, where equation is taken as input from the user, using the Newton-Raphson method.Write user-defined python functions to evaluate f(x) and f'(x) at the current approximation. The iterations of the method should stop when either the approximation is accurate within 10-5, or the number of iterations exceed 100, whichever happens first.

Example program is attached above where hardcoded equation is taken . I want user to input any type of equations.

here is the written form of code.

import math def fval(x): y = 4*x + math.sin(x) - math.exp(x) #Evaluating f(x) return y def dval(x): y = 4 + math.cos(x) - math.exp(x) #Evaluating f'(x) return y N=int(input("Enter the number of interation")) #Maximum no of iterations TOL = float(input("enter the error tolerance")) #Error tolerance print("Enter the initial approximation x0: ") x0 = float(input()) xn = x0 #Processing section for k in range(1,N+1): xp = xn fxp = fval(xp) dfxp = dval(xp) #xk = xk+1 = f(xk+1)/f'(xk+1) xn = xp - fxp/dfxp err = abs(xn-xp) #Error = | xk - xk-1 | print("After "+str(k)+" iterations,the approximate root = "+str(xn)) print("f(x) = "+str(fxp)) print("Error = "+str(err)) if err

import math def fval(x): y = 4*x + math.sin(x) return y math.exp(x) #Evaluating f(x) def dval(x): y = 4 + math.cos(x) - math.exp(x) #Evaluating f'(x) return y N=int(input("Enter the number of interation")) #Maximum no of iterations TOL = float(input("enter the error tolerance")) #Error tolerance print("Enter the initial approximation xo: ") x0 = float(input() x = x0 #Processing section for k in range(1,N+1): xp = xn fxp fval(xp) dfxp dval(xp) #xk = xk+1 = f(xk+1)/f'(xk+1) x = xp fxp/dfxp err = abs(xn-xp) #Error = | xk - xk-1 | print("After "+str(k)+" iterations, the approximate root = "+str(xn)) print("f(x) "+str(fxp)) print("Error "+str(err)) if err

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!