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 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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started