【发布时间】:2012-04-10 20:54:51
【问题描述】:
我正在尝试使用 Python 对以下隐式方程进行数值求解:
y*sin(c)+sin(y*c)=0
其中c 是一个常数。你会建议我怎么做?我实现了经典的 Newton-Raphson 方法,但它似乎没有收敛。
编辑:
代码如下:
f = open('results.dat','w')
import math
alpha = input('Define the alpha angle: ')
print >> f, 'alpha =', alpha
lambda_0 = input('Define an initial value: ')
print >> f, 'y_0 = ', y_0
gamma = math.pi - math.radians(alpha)
# def f(x,y):
# p = 2.0 * x
# t = p * y
# val = t * math.cos(t) - math.sin(t)
# val /= (p * math.cos(t) + math.sin(p))
# return val
toll = 1e-12
itmax = 50
diff = 1.0
it = 0
while diff > toll and it <= itmax:
p = 2.0 * gamma
t = p * y_0
y_1 = t * math.cos(t) - math.sin(t)
y_1 /= (p * math.cos(t) + math.sin(p))
print >> f, 'p = ', p
print >> f, 't = ', t
print >> f, 'y at iteration ', it, ' = ', y
diff = abs(y - y_0)
y_0 = y
it += 1
print >> f, 'diff = ', diff
print >> f, 'y_0 = ', y_0
print >> f, 'it = ', it
f.close()
【问题讨论】:
-
请显示您尝试过的代码。
-
你如何定义收敛?您使用什么标准?
-
@StevenRumbalski:抱歉耽搁了,我添加了代码。
-
@PhilH:我定义了两次连续迭代之间结果差异的期望容差(现在您也发布了我的代码)。
标签: python algorithm math numerical-methods