在特定步长间隔内逼近连续函数的导数

Approximate a derivative for a continuous function throughout certain step intervals

我想在 Java 中编写一个方法,它可以找到连续函数的导数。这些是对方法所做的一些假设 -

  1. 函数从 x = 0 到 x = 无穷大是连续的。
  2. 导数存在于每个区间。
  3. 需要将步长定义为参数。
  4. 该方法将找到给定区间 [a:b] 内连续函数的 max/min。

例如,函数 cos(x) 可以显示在 0、pi、2pi、3pi、... npi 处具有最大值或最小值。

我正在寻找一种方法来找到所有这些最大值或最小值,只要给定一个函数、lowerBound、upperBound 和步长即可。

为了简化我的测试代码,我为cos(x)写了一个程序。我使用的函数与 cos(x) 非常相似(至少在图形上如此)。这是我写的一些测试代码 -

public class Test {
    public static void main(String[] args){
        Function cos = new Function () 
        {
        public double f(double x) {
        return Math.cos(x);
        }
    };

        findDerivative(cos, 1, 100, 0.01);      
    }

    // Needed as a reference for the interpolation function.
    public static interface Function {
    public double f(double x);
    }

     private static int sign(double x) {
    if (x < 0.0)
            return -1;
        else if (x > 0.0)
            return 1;
        else
            return 0;
    }

     // Finds the roots of the specified function passed in with a lower bound,
    // upper bound, and step size.
    public static void findRoots(Function f, double lowerBound,
                  double upperBound, double step) {
    double x = lowerBound, next_x = x;
    double y = f.f(x), next_y = y;
    int s = sign(y), next_s = s;

    for (x = lowerBound; x <= upperBound ; x += step) {
        s = sign(y = f.f(x));
        if (s == 0) {
        System.out.println(x);
        } else if (s != next_s) {
        double dx = x - next_x;
        double dy = y - next_y;
        double cx = x - dx * (y / dy);
        System.out.println(cx);
        }
        next_x = x; next_y = y; next_s = s;
    }
    }

    public static void findDerivative(Function f, double lowerBound, double 
            upperBound, double step) {
    double x = lowerBound, next_x = x;
    double dy = (f.f(x+step) - f.f(x)) / step;

    for (x = lowerBound; x <= upperBound; x += step) {
        double dx = x - next_x;
        dy = (f.f(x+step) - f.f(x)) / step;
        if (dy < 0.01 && dy > -0.01) {
            System.out.println("The x value is " + x + ". The value of the "
                    + "derivative is "+ dy);
            }
        next_x = x;
        }
    }   
}

求根的方法是用来求零的(这个绝对管用)。我只将它包含在我的测试程序中,因为我认为我可以以某种方式在查找导数的方法中使用类似的逻辑。

的方法
public static void findDerivative(Function f, double lowerBound, double 
            upperBound, double step) {
    double x = lowerBound, next_x = x;
    double dy = (f.f(x+step) - f.f(x)) / step;

    for (x = lowerBound; x <= upperBound; x += step) {
        double dx = x - next_x;
        dy = (f.f(x+step) - f.f(x)) / step;
        if (dy < 0.01 && dy > -0.01) {
            System.out.println("The x value is " + x + ". The value of the "
                    + "derivative is "+ dy);
            }
        next_x = x;
        }
    }   

绝对可以改进。我怎么能以不同的方式写这个?这是示例输出。

The x value is 3.129999999999977. The value of the derivative is -0.006592578364594814
The x value is 3.1399999999999766. The value of the derivative is 0.0034073256197308943
The x value is 6.26999999999991. The value of the derivative is 0.008185181673381337
The x value is 6.27999999999991. The value of the derivative is -0.0018146842631128202
The x value is 9.409999999999844. The value of the derivative is -0.009777764220086915
The x value is 9.419999999999844. The value of the derivative is 2.2203830347677922E-4
The x value is 12.559999999999777. The value of the derivative is 0.0013706082193754021
The x value is 12.569999999999776. The value of the derivative is -0.00862924258597797
The x value is 15.69999999999971. The value of the derivative is -0.002963251265619693
The x value is 15.70999999999971. The value of the derivative is 0.007036644660118885
The x value is 18.840000000000146. The value of the derivative is 0.004555886794943564
The x value is 18.850000000000147. The value of the derivative is -0.005444028885981389
The x value is 21.980000000000636. The value of the derivative is -0.006148510767989279
The x value is 21.990000000000638. The value of the derivative is 0.0038513993028788107
The x value is 25.120000000001127. The value of the derivative is 0.0077411191450771355
The x value is 25.13000000000113. The value of the derivative is -0.0022587599505241585

在 f 的计算成本很高的情况下,我认为可以提高性能的主要事情是,您可以保存 f(x) 的先前值,而不是每次迭代都计算两次。此外,dx 从未使用过,并且始终等于 step 。 next_x 也没用过。一些变量可以在循环内声明。将变量声明移到内部可以提高可读性,但不会提高性能。

public static void findDerivative(Function f, double lowerBound, double upperBound, double step) {
    double fxstep = f.f(x);

    for (double x = lowerBound; x <= upperBound; x += step) {
        double fx = fxstep;
        fxstep = f.f(x+step);
        double dy = (fxstep - fx) / step;
        if (dy < 0.01 && dy > -0.01) {
            System.out.println("The x value is " + x + ". The value of the "
                    + "derivative is " + dy);
        }
    }
}

您基于(来自 rosettacode)的 java 代码不正确,请勿依赖它。

  • 预计 y(双精度值)将变为零。
    对于此类测试,您需要一个公差值。
  • 正在计算导数,用Newton's Method计算下一个x值,
    但不使用它来更新 x,那里没有任何优化。

Here there is an example of Newton's Method in Java

是的,您可以使用牛顿法优化您的代码,
因为它可以求解 f(x) = 0 when f'(x) given,
也可以解决 f'(x) = 0f''(x) 给出,同样的事情。

为了澄清我的评论,我修改了 link 中的代码。
我使用了 step = 2,得到了正确的结果。 与其他相比,检查它的速度。
这就是使用优化的原因,
否则减小步长并使用蛮力就可以完成工作。

class Test {

    static double f(double x) {
        return Math.sin(x);
    }

    static double fprime(double x) {
        return Math.cos(x);
    }

    public static void main(String argv[]) {

        double tolerance = .000000001; // Our approximation of zero
        int max_count = 200; // Maximum number of Newton's method iterations

        /*
         * x is our current guess. If no command line guess is given, we take 0
         * as our starting point.
         */

        double x = 0.6;
        double low = -4;
        double high = 4;
        double step = 2;
        int inner_count = 0;

        for (double initial = low; initial <= high; initial += step) {
            x = initial;
            for (int count = 1; (Math.abs(f(x)) > tolerance)
                    && (count < max_count); count++) {
                inner_count++;
                x = x - f(x) / fprime(x);
            }

            if (Math.abs(f(x)) <= tolerance) {
                System.out.println("Step: " + inner_count + ", x = " + x);
            } else {
                System.out.println("Failed to find a zero");
            }
        }
    }

}