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

2022-09-04 23:25:48

我希望用Java编写一个方法,为连续函数找到一个导数。这些是为该方法所做的一些假设 -

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

例如,函数cos(x)可以显示为在0,pi,2pi,3pi,...新产品。

我希望编写一个方法,该方法将找到所有这些最大值或最小值,前提是给定一个函数,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

答案 1

在 f 计算成本高昂的情况下,为了提高性能,我可以看到的主要事情是,您可以保存 f(x) 的先前值,而不是每次迭代计算两次。此外,dx从未使用过,无论如何都会始终等于步长。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);
        }
    }
}

答案 2

你基于的java代码(来自rosettacode)是不行的,不要依赖它。

  • 它期望y(双精度值)将完全变为零。
    您需要为此类测试提供容差值。
  • 它计算导数,并使用牛顿方法计算下一个x值,
    但不使用它来更新x,那里没有任何优化。

这里有一个Java中牛顿方法的例子

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