PYTHON中牛顿拉夫森法实现三角函数结果错误
Result error in implementation of trigonometric function by Newton Raphson method in PYTHON
牛顿法三角函数给出错误的结果
def func(x):
return radians(math.sin(x)+log(x)+1)
def derivFunc(x):
return radians((1/x) + math.cos(x))
#sin(x)+log(x)+1 --is the function i want to apply method on
**# 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)
x0 = 0.03
newtonRaphson(x0)
问题是 radians
,它将表达式的一部分乘以 pi/180.0
。删除所有提及的内容,应该没问题。
编辑
下面的现在工作得很好:
import math
def func(x):
return math.sin(x)+math.log10(x)+1
def derivFunc(x):
return (1/x) + math.cos(x)
#sin(x)+log(x)+1 --is the function i want to apply method on
# 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)
x0 = 0.03
newtonRaphson(x0)
牛顿法三角函数给出错误的结果
def func(x):
return radians(math.sin(x)+log(x)+1)
def derivFunc(x):
return radians((1/x) + math.cos(x))
#sin(x)+log(x)+1 --is the function i want to apply method on
**# 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)
x0 = 0.03
newtonRaphson(x0)
问题是 radians
,它将表达式的一部分乘以 pi/180.0
。删除所有提及的内容,应该没问题。
编辑
下面的现在工作得很好:
import math
def func(x):
return math.sin(x)+math.log10(x)+1
def derivFunc(x):
return (1/x) + math.cos(x)
#sin(x)+log(x)+1 --is the function i want to apply method on
# 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)
x0 = 0.03
newtonRaphson(x0)