我甚至难以开始解决这个问题。我发现的所有例子都太简单或太复杂而无法消化。
我想在给出一系列输入的情况下找到值S
。该函数是单变量但非线性的。 S
将始终在-3 .. 3之间。
我想使用Apache Commons库,因为我之前在该代码的其他部分有过经验。
每次我想解决我的问题,我都知道以下信息:
double R =250.0;
double om1 = 5.0;
double om2 = 15.0;
double th21 = 29.07965;
double th22 = 29.69008;
double D_obs = th21 - th22;
实际值将在解决方案之间变化,但对于任何一个特定解决方案,它们都是固定的。
我想找的价值是:
double S = 0.0;
这样的
double d1 = delta(R,om1,th21,S);
double d2 = delta(R,om2,th22,S);
double D_calc = d1 - d2;
有价值
double minme = Math.abs(D_obs - D_calc);
最小的,或者替代地,解决
double minme = D_obs - D_calc;
哪里有minme=0
。
函数delta
定义为
public static double delta(double R, double om, double th2, double s)
{
if(Math.abs(s) <= 0.0001) //is the displacement == 0?
{
return 0.0;
}
else
{
return Math.toDegrees((-1*Cos(th2)*s-R*Sin(om)+Sqrt(-1*Math.pow(Cos(th2),2)*Math.pow(s,2)+2*Cos(th2)*Sin(om)*R*s-Math.pow(Cos(om),2)*Math.pow(R,2)+Math.pow(R,2)+2*Math.pow(s,2)))/(Sin(th2)*s));
}
}
例如,Cos
is在其他地方定义为Math.cos(Math.toRadians(val))
在哪里/我可以阅读/做什么来开始解决这个问题?
我找到了一个可以使用的答案:Newton-Raphson method using the Math.Commons library
关键代码是
public static void main(String args[])
{
//setup all variables
final double R =(new Double(args[0])).doubleValue(); //=250.0;
final double om1 =(new Double(args[1])).doubleValue(); //= 5.0;
final double om2 =(new Double(args[2])).doubleValue(); //= 15.0;
final double th21=(new Double(args[3])).doubleValue(); //= 29.07965;
final double th22=(new Double(args[4])).doubleValue(); //= 29.69008;
final double D_obs = th21 - th22;
BisectionSolver solver = new BisectionSolver();
UnivariateFunction f = new UnivariateFunction()
{
public double value(double s) {
return ((delta(R,om1,th21,s)-delta(R,om2,th22,s)) - (D_obs));
}
};
System.out.printf("The speciment offset is %.3f mm.\n", solver.solve(1000, f, -3, 3));
}