我有时间和温度 (x,y) 的样本,我需要知道 Z 摄氏度下温度相等的时间(例如:35 C)。
我收集的样本很少,为了开始微积分,我使用了 3 个样本。关注:
Temperature | Time (s)
25 | 0
27 | 10
33 | 17
40 | ?
我知道我需要更多样本才能获得准确的结果,但首先我使用这个。
问题是,我如何为此实现代码?我对 Apache Commons 的了解如下:
org.apache.commons.math3.optim.nonlinear.scalar 但我不知道如何使用这个库。 我需要一个微积分代码示例。 谢谢!
最佳答案
除非我误解了您想要实现的目标,否则您似乎正在使一个简单的问题变得复杂。
解决问题的最简单方法是将其视为 Linear Regression问题并使用 SimpleRegression Apache Commons 类可以执行以下操作:
import org.apache.commons.math3.stat.regression.SimpleRegression;
public class MySimpleRegression {
public static void main(String[] args) {
// create a Simple Regression object
SimpleRegression simpleRegression = new SimpleRegression();
// create your data object with various instances of x, y - make the
// variable you want to predict the 'y' in your data
// i.e. if you wanna predict the time at a given temperature,
// 'x' would be temperature and 'y' time
double[][] data = { { 25, 0 }, {27, 10 }, {33, 17 }, {40, 20 }};
// pass this data to your simple regression object
simpleRegression.addData(data);
// and then you can predict the time at a given temperature value
System.out.println("Predicted Time: " + simpleRegression.predict(35));
// You can also get the slope and intercept from your data
System.out.println("slope = " + simpleRegression.getSlope());
System.out.println("intercept = " + simpleRegression.getIntercept());
}
}
这似乎是解决您的问题的更直接的方法。希望这对您或其他人有帮助。
P.S.:没有尝试运行这个。不过应该可以正常工作。
关于java - 如何使用 Apache Commons 或其他解决 Java 中的非线性模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41985427/