#Program to find root by Newton-Raphson method for y=x-tanh(2x):
from math import*
def f(x):
y=x-tanh(2*x)
return y
def f1(x):
y1=1-(2/(cosh(2*x)*cosh(2*x)))
return y1
x=input("Enter the initial guess,x0=")
x=float(x)
s=0
while abs(f(x))>0.000001 and s<500:
h=float(f(x))/float(f1(x))
x=x-h
s=s+1
print ("The solution for the given function is:",x,"\nNo. of iterations required:",s)
from math import*
def f(x):
y=x-tanh(2*x)
return y
def f1(x):
y1=1-(2/(cosh(2*x)*cosh(2*x)))
return y1
x=input("Enter the initial guess,x0=")
x=float(x)
s=0
while abs(f(x))>0.000001 and s<500:
h=float(f(x))/float(f1(x))
x=x-h
s=s+1
print ("The solution for the given function is:",x,"\nNo. of iterations required:",s)
Comments
Post a Comment