超越方程的牛顿法 [英] Newton method for transcendental equation

查看:87
本文介绍了超越方程的牛顿法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先验方程:tan(x)/x + b = 0,其中b是任何实数.我需要介绍n并给我n个方程的解.

Transcendental equation: tan(x)/x + b = 0, where b is any real number. I need to introduce n and give me n solutions of this equation.

我的代码(Python):

My code (Python):

    from math import tan, cos, pi, sqrt, sin,exp
    import numpy as np 
    from matplotlib.figure import Figure
    import matplotlib.pyplot as plt

    def f(x,b):
        return tan(x)/x + b

    def f1(x,b):
        return (x/(cos(x)*cos(x)) - tan(x))/(x**2)

    e = 0.00000000001

    def newtons_method(x0, f, f1, e):
        x0 = float(x0)
        while True:
            x1 = x0 - (f(x0,b) / f1(x0,b))
            if abs(x1 - x0) < e:
                return x1
            x0 = x1

    result = []
    n = int(input("Input n: "))
    b = float(input("Input b: "))
    for i in range(2,4*n,1):
        result.append(newtons_method(i, f , f1, e))
    lambda_result = sorted(list(set(result)))
    print(len(lambda_result))

我用第1步更改初始近似值.以〜pi为周期重复根,因此第二个参数4 * n.我通过设置排除了重复的解决方案.如果n为50,那么他只能找到18个解.要使其正常工作,需要解决什么问题?请帮帮我.

I change the initial approximation with step 1.The roots are repeated with a period of ~pi, so the second argument 4*n. I exclude repeated solutions through set. If n is 50 then he finds only 18 solution. What needs to be fixed to make it work? Help me, please.

推荐答案

在交叉发布中 https://math.stackexchange.com/q/2942400/115115 Yves Daoust强烈建议您将牛顿方法基于该函数

In your cross-post https://math.stackexchange.com/q/2942400/115115 it was strongly recommended by Yves Daoust to base your Newton method on the function

f(x)=sin(x) + b*x*cos(x)

f(x)=sin(x)/x + b*cos(x)

因为这些函数没有极点或其他奇点(如果 x 不接近 0).

as these functions do not have poles or other singularities (if x is not close to 0).

至少对于较大的 c ,解决方案接近于范围(n)中i的初始值(i + 0.5)* pi.然后,结果不需要排序或缩短结果.

The solutions are, at least for large c, close to the initial values (i+0.5)*pi for i in range(n). The result does then not require sorting or shortening of the result.

可以用一个正弦项在余弦的根部具有交替的符号.这非常适合应用括弧法,例如 regula falsi(illinois),Dekker's fzeroin,Brent方法,...这些方法几乎与Newton方法一样快并确保在间隔内找到根.

One could use that at the roots of the cosine the sine term has alternating sign. This makes a perfect situation to apply a bracketing method like regula falsi (illinois), Dekker's fzeroin, Brent's method,... These methods are almost as fast as the Newton method and are guaranteed to find the root inside the interval.

唯一的麻烦是间隔(0,pi/2),因为 b< -1 的根将为非零.必须从 x=0 中删除寻根过程,这对于 b 接近于 -1 是很重要的.

The only complication is the interval (0,pi/2) as there will be a non-zero root for b<-1. One has to remove the root-finding process from x=0 which is non-trivial for b close to -1.

牛顿法,当初始点离根足够近时,可靠地将零插入根.如果该点更远,接近函数的极值,则切线的根可能会很远.因此,要成功地应用牛顿法,就需要从一开始就找到良好的根近似值.为此,可以使用全局收敛的定点迭代或所考虑函数的结构简单近似.

Newton's method only zero's in to the root reliably when the initial point is close enough to the root. If the point is farther away, close to an extremum of the function, the root of the tangent may be very far away. Thus to successfully apply the Newton method, one needs to find good root approximations from the start. To that end one may use globally convergent fixed-point iterations or structurally simple approximations of the function under consideration.

  • 使用收缩定点迭代:围绕 k * pi 的解决方案也是等式 x + arctan(b * x)= k * pi的根.这给出了近似解 x = g(k * pi)= k * pi-arctan(b * k * pi).由于即使对于较小的 k ,圆弧切线也相当平坦,因此可以很好地近似.

  • Using the contracting fixed-point iteration: the solution around k*pi is also a root of the equivalent equation x+arctan(b*x)=k*pi. This gives the approximate solution x=g(k*pi)=k*pi-arctan(b*k*pi). As the arcus tangent is rather flat even for small k, this gives a good approximation.

如果 b< -1 表示 k = 0 的正根,则间隔为(0,pi/2).在这种情况下,前一种方法不起作用,因为在此间隔内,反正切线的斜率在 1 附近.根是由于方程的高阶非线性项,因此需要对方程的等效形式之一进行非线性近似.近似 tan(x)= x/(1-(2 * x/pi)^ 2) +-pi/2 处给出相同的极点,并且在之间.将这个近似值插入给定的方程式并求解,即可得出 x = pi/2 * sqrt(1 + 1/b)的初始根近似值.

If b<-1 there is a positive root for k=0, that is in the interval (0,pi/2). The previous method does not work in this case as the arcus tangent has a slope around 1 in this interval. The root is due to the higher order, non-linear terms of the equation, so one needs a non-linear approximation of one of the equivalent forms of the equation. Approximating tan(x)=x/(1-(2*x/pi)^2) gives the same poles at +-pi/2 and is sufficiently close in between. Inserting this approximation into the given equation and solving gives the initial root approximation at x=pi/2*sqrt(1+1/b).

在实现中,必须转移 b< -1 的根集以包括其他第一个解决方案.

In the implementation one has to shift the root set for b<-1 to include the additional first solution.

from math import tan, cos, pi, sqrt, sin, atan

def f(x,b):
    return sin(x)/x+b*cos(x)

def f1(x,b):
    return cos(x)/x-(b+x**-2)*sin(x)

e = 1e-12

def newtons_method(x0, f, f1, e):
    x0 = float(x0)
    while True:
        x1 = x0 - (f(x0,b) / f1(x0,b))
        if abs(x1 - x0) < e:
            return x1
        x0 = x1

result = []
n = int(input("Input n: "))
b = float(input("Input b: "))
for i in range(n):
    k=i; 
    if b >= -1: k=k+1
    x0 = pi/2*sqrt(1+1/b) if k==0 else  k*pi-atan(b*k*pi)
    result.append(newtons_method(x0, f , f1, e))
lambda_result = sorted(list(set(result)))
print(len(result), len(lambda_result))

这篇关于超越方程的牛顿法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆