You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Matthew Rowles <ro...@gmail.com> on 2019/04/04 15:15:22 UTC

[math] univariate nonlinear optimisation : how to start?

I'm having difficulty even beginning to solve this problem. All examples
that I have found are either too simple or way too complex to digest.

I want to to find the value S given a series of inputs. The function is
univariate but non-linear. S will always be between -3 .. 3.

I would like to use the Apache Commons library, as I have had prior
experience in other sections of the code.

For each time I want to solve my problem, I know the following information:

    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;

The actual values will change between solutions, but they are fixed for any
one particular solution.

The value I want to find is:

    double S   = 0.0;

such that

    double d1     = delta(R,om1,th21,S);
    double d2     = delta(R,om2,th22,S);
    double D_calc = d1 - d2;

have values to make

    double minme = Math.abs(D_obs - D_calc);

a minimum. (ideally zero, but a minimum).

The function delta is defined as

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));
    }
}

where, for example, Cosis defined elsewhere as Math.cos(Math.toRadians(val))

Where/what can I read/do to get a start on this problem?


Thanks


Matthew

Re: [math] univariate nonlinear optimisation : how to start?

Posted by Gilles Sadowski <gi...@gmail.com>.
Hello.

Le jeu. 4 avr. 2019 à 17:23, Matthew Rowles <ro...@gmail.com> a écrit :
>
> I'm having difficulty even beginning to solve this problem. All examples
> that I have found are either too simple or way too complex to digest.

What did you try?
Did you at the Javadocs for the "optim" and "fitting" package?
The unit tests could also help.

>
> I want to to find the value S given a series of inputs. The function is
> univariate but non-linear. S will always be between -3 .. 3.
>
> I would like to use the Apache Commons library, as I have had prior
> experience in other sections of the code.
>
> For each time I want to solve my problem, I know the following information:
>
>     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;
>
> The actual values will change between solutions, but they are fixed for any
> one particular solution.
>
> The value I want to find is:
>
>     double S   = 0.0;

So, you have it. ;-)

>
> such that
>
>     double d1     = delta(R,om1,th21,S);
>     double d2     = delta(R,om2,th22,S);
>     double D_calc = d1 - d2;
>
> have values to make
>
>     double minme = Math.abs(D_obs - D_calc);
>
> a minimum. (ideally zero, but a minimum).

So, a least-squares problem (with 1 observation?).

> The function delta is defined as
>
> 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));
>     }
> }

You could improve readability and performance by
* not calling "pow" to get the square,
* not compute the same value multiple times,
* perform the computation with radians throughout (i.e. only convert
  the inputs and outputs from and to degrees)

HTH,
Gilles

>
> where, for example, Cosis defined elsewhere as Math.cos(Math.toRadians(val))
>
> Where/what can I read/do to get a start on this problem?
>
>
> Thanks
>
>
> Matthew

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [math] univariate nonlinear optimisation : how to start?

Posted by Matthew Rowles <ro...@gmail.com>.
I managed to solve it to the extent that it works well enough for me.

I pinched some code and changed it a little for my purposes:
https://stackoverflow.com/questions/32494230/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));
    }

On Thu, 4 Apr 2019 at 23:15, Matthew Rowles <ro...@gmail.com> wrote:

> I'm having difficulty even beginning to solve this problem. All examples
> that I have found are either too simple or way too complex to digest.
>
> I want to to find the value S given a series of inputs. The function is
> univariate but non-linear. S will always be between -3 .. 3.
>
> I would like to use the Apache Commons library, as I have had prior
> experience in other sections of the code.
>
> For each time I want to solve my problem, I know the following information:
>
>     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;
>
> The actual values will change between solutions, but they are fixed for
> any one particular solution.
>
> The value I want to find is:
>
>     double S   = 0.0;
>
> such that
>
>     double d1     = delta(R,om1,th21,S);
>     double d2     = delta(R,om2,th22,S);
>     double D_calc = d1 - d2;
>
> have values to make
>
>     double minme = Math.abs(D_obs - D_calc);
>
> a minimum. (ideally zero, but a minimum).
>
> The function delta is defined as
>
> 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));
>     }
> }
>
> where, for example, Cosis defined elsewhere as
> Math.cos(Math.toRadians(val))
>
> Where/what can I read/do to get a start on this problem?
>
>
> Thanks
>
>
> Matthew
>