Slip 21
Slip 21
Q3.
a.
1.netwon raphson method
def f(x):
return x**5+3*x+1
def g(x):
return 5*x+3
def newtonRaphson(x0,e,N):
print("\n\n***NEWTON RAPHSON METHOD IMPLEMENTATION ***")
step=1
flag=1
condition=True
while condition:
if g(x0) == 0.0:
print("Divide by zero error!")
break
x1 = x0-f(x0)/g(x0)
print("Iteration-%d,x1=%0.6f and f(x1)=%0.6f"%(step,x1,f(x1)))
x0=x1
step=step+1
if step>N:
flag=0
break
condition=abs(f(x1))>e
if flag==1:
print("\nRequired root is: %0.8f" % x1)
else:
print("\nNot convergent.")
x0=input("enter guess: ")
e=input("Tolerable Error: ")
N=input("Maximum step: ")
N=int(N)
newtonRaphson(x0,e,N)
2. Interpolate
from scipy.interpolate import interp1d
# test value
interpolate_x = 3
def newtonRaphson(x0,e,N):
print('\n\n*** NEWTON RAPHSON METHOD IMPLEMENTATION ***')
step = 1
flag = 1
condition = True
while condition:
if g(x0) == 0.0:
print('Divide by zero error!')
break
x1 = x0 - f(x0)/g(x0)
print('Iteration-%d, x1 = %0.6f and f(x1) = %0.6f' % (step, x1, f(x1)))
x0 = x1
step = step + 1
if step > N:
flag = 0
break
if flag==1:
print('\nRequired root is: %0.8f' % x1)
else:
print('\nNot Convergent.')
x0 = float(x0)
e = float(e)
N = int(N)
newtonRaphson(x0,e,N)
step = step + 1
condition = abs(f(x2)) > e
x0 = float(x0)
x1 = float(x1)
e = float(e)