用Python绘制图表
在下面的代码中,我用Python实现了Newton方法用Python绘制图表,python,diagram,Python,Diagram,在下面的代码中,我用Python实现了Newton方法 import math def Newton(f, dfdx, x, eps): f_value = f(x) iteration_counter = 0 while abs(f_value) > eps and iteration_counter < 100: try: x = x - float(f_value)/dfdx(x) except Z
import math
def Newton(f, dfdx, x, eps):
f_value = f(x)
iteration_counter = 0
while abs(f_value) > eps and iteration_counter < 100:
try:
x = x - float(f_value)/dfdx(x)
except ZeroDivisionError:
print ("Error! - derivative zero for x = ", x)
sys.exit(1) # Abort with error
f_value = f(x)
iteration_counter += 1
# Here, either a solution is found, or too many iterations
if abs(f_value) > eps:
iteration_counter = -1
return x, iteration_counter
def f(x):
return (math.cos(x)-math.sin(x))
def dfdx(x):
return (-math.sin(x)-math.cos(x))
solution, no_iterations = Newton(f, dfdx, x=1, eps=1.0e-14)
if no_iterations > 0: # Solution found
print ("Number of function calls: %d" % (1 + 2*no_iterations))
print ("A solution is: %f" % (solution))
else:
print ("Solution not found!")
最后:
import numpy as np
import matplotlib.pyplot as plt
data = np.array(list(Newton(f,dfdx, 1, 10e-14)))
plt.plot(data[:,0], data[:,1])
plt.yscale('log')
plt.show()
这将生成图形:
Newton
函数不应同时屈服和返回import math
import sys
import numpy as np
import matplotlib.pyplot as plt
def newton(f, dfdx, x, eps):
f_value = f(x)
iteration_counter = 0
while abs(f_value) > eps and iteration_counter < 100:
try:
x = x - float(f_value)/dfdx(x)
yield iteration_counter, x, abs(f(x))
except ZeroDivisionError:
print ("Error! - derivative zero for x = ", x)
sys.exit(1) # Abort with error
f_value = f(x)
iteration_counter += 1
def f(x):
return x ** 2 - 1.34
def dfdx(x):
return 2 * x
data = np.array(list(newton(f, dfdx, 10, 10e-14)))
# plt.plot(data[:, 0], data[:, 1]) # x-axis: iteration, y-axis: x values
plt.plot(data[:, 0], data[:, 2]) # x-axis: iteration, y-axis: f(x) values
plt.yscale('log')
plt.show()
导入数学
导入系统
将numpy作为np导入
将matplotlib.pyplot作为plt导入
def牛顿(f、dfdx、x、eps):
f_值=f(x)
迭代计数器=0
当abs(f_值)>eps和迭代_计数器<100时:
尝试:
x=x-浮点(f_值)/dfdx(x)
收益率计数器,x,abs(f(x))
除零误差外:
打印(“错误!-x=,x的导数为零)
系统退出(1)#带错误中止
f_值=f(x)
迭代计数器+=1
def f(x):
返回x**2-1.34
def dfdx(x):
返回2*x
数据=np.数组(列表(牛顿(f,dfdx,10,10e-14)))
#plt.绘图(数据[:,0],数据[:,1])#x轴:迭代,y轴:x值
plt.绘图(数据[:,0],数据[:,2])#x轴:迭代,y轴:f(x)值
plt.yscale('log')
plt.show()
谢谢你的帖子,我需要使用我在问题tho中的功能!你的还为我工作吗?谢谢你的帖子,你的函数收敛得非常快。这就是为什么。在data=np.array(list(newton(f,dfdx,10,10e-14))中,10代表x的初始值是什么。因此没有明确说明x的初始值应该是什么,所以我假设我只选择一个?
import numpy as np
import matplotlib.pyplot as plt
data = np.array(list(Newton(f,dfdx, 1, 10e-14)))
plt.plot(data[:,0], data[:,1])
plt.yscale('log')
plt.show()
import math
import sys
import numpy as np
import matplotlib.pyplot as plt
def newton(f, dfdx, x, eps):
f_value = f(x)
iteration_counter = 0
while abs(f_value) > eps and iteration_counter < 100:
try:
x = x - float(f_value)/dfdx(x)
yield iteration_counter, x, abs(f(x))
except ZeroDivisionError:
print ("Error! - derivative zero for x = ", x)
sys.exit(1) # Abort with error
f_value = f(x)
iteration_counter += 1
def f(x):
return x ** 2 - 1.34
def dfdx(x):
return 2 * x
data = np.array(list(newton(f, dfdx, 10, 10e-14)))
# plt.plot(data[:, 0], data[:, 1]) # x-axis: iteration, y-axis: x values
plt.plot(data[:, 0], data[:, 2]) # x-axis: iteration, y-axis: f(x) values
plt.yscale('log')
plt.show()