java - 如何使用 Apache Commons 或其他解决 Java 中的非线性模型?

标签 java apache-commons nonlinear-optimization non-linear-regression

我有时间和温度 (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/

相关文章:

java - Foreach 并创建新对象给出空值

java - RecyclerView.OnScrollListener 的问题

java - 在 JSP 中渲染具有单键和多值的多值 HashMap

java - 带有 Signpost 和 Apache Commons HTTP 的 OAuth

java - CPLEX 最小化分段线性函数

java - 当我的应用程序运行时拒绝振动(我的应用程序除外)

java - 如何在映射 :util element in a spring xml file when the string is defined in the same file? 中引用字符串变量

java - apache.commons.lang3.DateUtils.setMonths 十二月

r - R 中的优化 : cost function with binary scheduling variables?

Scipy 选择 nan 作为输入同时最小化