首页 > 解决方案 > x轴上下交点求根函数

问题描述

我需要找到与给出的曲线的 x 轴的上下交点

y=f(x)=10⋅exp(sin(x))−(x^2)/2

为了找到曲线的弧长,在 Python

我已经尝试了两种方法,我根本无法使用的割线法。以及找到一个交点的牛顿法。

from math import exp 
from math import sin
from math import cos

def func( x ): 
    return 10*exp(sin(x))-(x**2)/2

def derivFunc( x ): 
    return 10*exp(sin(x))*cos(x)-x

def newtonRaphson( x ): 
    h = func(x) / derivFunc(x) 
    while abs(h) >= 0.0001: 
        h = func(x)/derivFunc(x) 

        x = x - h 

    print("The value of the root is : ", 
                             "%.4f"% x) 


x0 = -20 
newtonRaphson(x0) 

这使

The value of the root is :  -5.7546

然后是第二种方法

import math 
from math import exp 
from math import sin


def f(x):

    f = 10*exp(sin(x))-(x**2)/2
    return f; 

def secant(x1, x2, E):
    n = 0; xm = 0; x0 = 0; c = 0;
    if (f(x1) * f(x2) < 0):
        while True:
            x0 = ((x1 * f(x2) - x2 * f(x1)) /(f(x2) - f(x1)));
            c = f(x1) * f(x0);
x1 = x2;
x2 = x0;
n += 1;
if (c == 0): 
    xm = ((x1 * f(x2) - x2 * f(x1)) /(f(x2) - f(x1)));
if(abs(xm - x0) < E):
    print("Root of the given equation =",round(x0, 6));
    print("No. of iterations = ", n); 
    print("Can not find a root in ","the given inteval"); 
x1 = 0; x2 = 1;
E = 0.0001;
secant(x1, x2, E);             

只会导致

NameError: name 'x2' is not defined

然而,每当我尝试定义字符时,它都不会运行

我希望能够得到与 x 轴的上下交点,所以我可以找到弧长。有没有办法让它也绘制图表

标签: pythonpython-3.xnumerical-methods

解决方案


关于 Newton-Raphson 方法:

正常行为

它主要按预期工作。该方法可能只收敛到一个根,这取决于起点。要获得另一个根,您需要另一个起点。

您的函数产生:

>>> newtonRaphson(-20)
-5.7545790362989
>>> newtonRaphson(5)
3.594007784799419

这似乎是正确的。

错误

Newton-Raphson 方法不能保证收敛,它可能会进入无限循环,在这种情况下您的程序将无限期挂起,或者某个点的导数可能为零,在这种情况下您无法计算h. 您需要处理这些情况。

风格

有很多可以改进的地方:

  • 错误必须修复
  • You Newton-Raphson 方法目前仅适用于一种功能。您应该将函数和导数作为参数传递,因此您可以将该方法应用于您想要的任何函数。
  • 所需的精度和最大迭代次数也可以作为参数传递
  • 在函数内打印是不好的做法。您应该返回该值,以便您可以决定对结果执行任何操作。
  • 你应该遵循 PEP8 的风格指南
  • 如果您打算重用它,请包含一个文档字符串(这是很有可能的,它是一个非常有用的工具!)

我对方法的看法:

def newton_raphson(f, df, x, epsilon = 0.0001, maxiter = 1000): 
    """ Estimates the root of a function.

    Gives an estimate to the required precision of a root of the given function
    using the Newton-Raphson method.

    Raises an Exception if the Newton-Raphson method doesn't converge in the
    specified number of iterations.
    Raises a ZeroDivisionError if the derivative is zero at a calculated point

    :param f: The function 
    :param df: The function's derivative
    :param x: the starting point for the method
    :param epsilon: The desired precision
    :param maxiter: The maximum number of iterations

    :return: The root extimate
    :rtype: float
    """

    for _ in range(maxiter):
        h = f(x)/df(x) 
        if abs(h) < epsilon:
            return x
        x = x - h 

    raise Exception("Newton Raphson method didn't "
                    + "converge in {} iterations".format(maxiter))

用法:

>>> print(newton_raphson(func, derivFunc, 20))
-5.7545790362989
>>> print(newton_raphson(func, derivFunc, 5, 0.1, 100))
3.5837828560043477
>>> print(newton_raphson(func, derivFunc, 5, 0.001, 100))
3.594007784799419
>>> print(newton_raphson(func, derivFunc, 5, 1e-9, 4))
Traceback (most recent call last):
(...)
Exception: Newton Raphson method didn't converge in 4 iterations

关于割线法:

我对那个不太熟悉,所以我只提一下您遇到的错误是由于识别错误造成的。这里是固定的:

def secant(x1, x2, E):
    n = 0; xm = 0; x0 = 0; c = 0;
    if (f(x1) * f(x2) < 0):
        while True:
            x0 = ((x1 * f(x2) - x2 * f(x1)) /(f(x2) - f(x1)));
            c = f(x1) * f(x0);
    x1 = x2;
    x2 = x0;
    n += 1;
    if (c == 0): 
        xm = ((x1 * f(x2) - x2 * f(x1)) /(f(x2) - f(x1)));
    if(abs(xm - x0) < E):
        print("Root of the given equation =",round(x0, 6));
        print("No. of iterations = ", n); 
    print("Can not find a root in ","the given inteval"); 

如果您打算积极实施该方法,关于 Newton-Raphson 方法的评论仍然成立。


推荐阅读