根寻找函数用于x轴的上下交叉点

问题描述 投票:-1回答:1

我需要找到由下面给出的曲线的x轴的下部和上部交叉点

y=f(x)=10exp(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轴的上下交叉点,所以我可以找到弧长。有没有办法让它绘制图形

python python-3.x numerical-methods
1个回答
1
投票

关于Newton-Raphson方法:

正常行为

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

你的函数产生:

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

这似乎是正确的。

错误

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

样式

有很多事情可以改进:

  • 必须修复错误
  • 您N​​ewton-Raphson方法目前仅适用于一个函数。您应该将函数和派生作为参数传递,因此您可以将该方法应用于您想要的任何函数。
  • 期望的精度和最大迭代也可以作为参数传递
  • 在函数内打印是不好的做法。您应该返回该值,以便您可以决定对结果执行任何操作。
  • 你应该遵循PEP8的风格指南
  • 如果你计划重用它,请包含一个docstring(这是非常有可能的,它是一个非常有用的工具!)

我采取的方法:

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方法的评论仍然有效。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.