gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 21:12:37 26 4
gpt4 key购买 nike

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

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

例如,函数 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);
}
}
}

关于java - 在特定步长间隔内逼近连续函数的导数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31898219/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com