commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From luc.maison...@free.fr
Subject Re: [math] help writing a solver for equation
Date Thu, 18 Sep 2008 13:12:02 GMT
Try this (take care, I didn't check this does work, I typed the code directly in my mail client):

public class MyFunctionimplements UnivariateRealFunction {

  private final double z;
  private final double[] xa;

  public MyFunctionimplements(double z, double[] xa) {
    this.z = z;
    this.xa = xa;
  }

  public double value(double y) {
     double sum = 0;
     for (int i = 0; i < xa.length; ++i) {
       sum += Math.exp(y - xa[i]) / Math.exp(xa[i]);
     }
     return sum - z;
  }

}

UnivariateRealSolver solver = new BrentSolver(new MyFunction(z, xa));
y = solver.solve(yMin, yMax);

However, as Ted said in a previous message, if the equation is really this one, it can be
solved formally.

Luc

----- Mail Original -----
De: mickeydog@TaosNet.com
À: "Commons Users List" <user@commons.apache.org>
Cc: "Commons Users List" <user@commons.apache.org>
Envoyé: Mercredi 17 Septembre 2008 22:20:31 GMT +01:00 Amsterdam / Berlin / Berne / Rome
/ Stockholm / Vienne
Objet: Re: [math] help writing a solver for equation

Are there examples of how to use the UnivariateRealSolvers?
or better yet, can you sketch how I would approach with my equation?

> mickeydog@TaosNet.com a écrit :
>> I have an equation like so:
>> z = Sum of a = 1 to n [ e**(y - x(a))/ e**(x(a)]
>> I want to solve for y.
>> I have z and x(a).
>>
>> I have created a class EquationSolver which has these methods and inner
>> class:
>>
>> private EstimatedParameter xa;
>>
>> public VintageQualityFitter() {
>>
>>   xa = new EstimatedParameter("xa", 0.0);
>
> If you want to solve for y and know all x(a), your parameter should be y.
>
>>
>>   // provide the parameters to the base class which
>>   // implements the getAllParameters and getUnboundParameters methods
>>   addParameter(xa);
>> }
>>
>> public double theoreticalValue(double z) {
>>   double summand = ( Math.exp(y * xa.getEstimate()) /
>> Math.exp(xa.getEstimate());
>>   return summand;
>> }
>
> the name "z" is confusing there, I don't understand if you have several
> x(a) and only one value z which is a sum (as seemed to be implied by the
> start of your message) or if you have one z for each x(a) as seemed to
> be implied by the previous method.
>
> If you have only one z, then the estimation algorithm is not what you
> need. You need either to rewrite your equation to solve it (has Ted
> proposed) or you need to use a UnivariateRealSolver from package
> analysis, and use a simple loop to compute your function for the set of
> x(a) you know.
>
>>
>> public void addPoint(double xa) {
>>   addMeasurement(new LocalMeasurement(xa, 1.0)); // all weights same
>> }
>>
>> public double getXa() {
>>   return xa.getEstimate();
>> }
>>
>> private class LocalMeasurement extends WeightedMeasurement {
>>
>>   private final double z;
>>
>>   // constructor
>>   public LocalMeasurement(double xa, double weight) {
>
>   This xa seems strange to me, isn't it rather a "z", or is it linked to
> a "z" ?
>
> Luc
>
>>      super(xa, weight);
>>      this.z = xa;
>>   }
>>
>>   public double getTheoreticalValue() {
>>     // the value is provided by the model for the local xa
>>     return theoreticalValue(z);
>>   }
>>
>> // don't need this i think
>>   public double getPartial(EstimatedParameter parameter) {
>>     return 0.0;
>>   }
>> }
>>
>> I use this like so:
>>
>> EquationFitter equationFitter = new EquationFitter();
>>
>> for (int a = 0; a <= numberOfAs; a++) {
>>   double xa = <get x(a)>
>>   equationFitter.addPoint(xa.doubleValue());
>>  }
>>
>> try {
>>     // solve the problem, using a Levenberg-Marquardt algorithm with
>> default settings
>> 	LevenbergMarquardtEstimator estimator = new
>> LevenbergMarquardtEstimator();
>>     estimator.estimate(equationFitter);
>> } catch (EstimationException e) {
>> 	e.printStackTrace();
>> }
>>
>> // fitter.theoreticalValue() is alpha
>> <get some z>
>> y = equationFitter.theoreticalValue(z);
>>
>> but something in all this isn't right, especially with
>> theoreticalValue(),
>> i think.
>> Can anyone help?
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
>> For additional commands, e-mail: user-help@commons.apache.org
>>
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>


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


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


Mime
View raw message