package jasext.hist.function;
import jas2.hist.*;
/**
* Fittable Exponential function with handles:
* y(x) = A exp(-(x-x0)/E)
* A = amplitude, corresponding to function value for x=x0
* x0 = origin, corresponding to y(xo) = A
* E = reciprocal exponent (decay constant)
*
* The initial function will be a negative exponential based
* on the current maximum and minimum axis values (not the data).
* A positive exponential can be formed by dragging the handles.
*
* Handles:
* There are two handles.
* Handle 1: Changes the function amplitude and origin. The handle
* position corresponds to the current amplitude and origin values.
* Handle 2: Changes the reciprocal exponent value. The handle y position
* is 1/e * amplitude (=0.367*A); the x position is offset from the
* function origin by the reciprocal of the exponent.
*
* @author Robert J. Wilson
* @version August 7th., 1999
* */
public class ExponentialFunction extends Fittable1DFunction implements HasHandles
{
/**
*
*/
private static final long serialVersionUID = 1L;
ExponentialFunction(double amplitude, double origin, double exponent)
{
this.max = amplitude;
this.origin = origin;
this.exponent = exponent;
}
public ExponentialFunction(double xmin, double xmax, double ymin, double ymax)
{
this.max = ymin + (ymax - ymin)*.8;
this.origin = xmin + (xmax - xmin)*.05;
this.exponent = -(xmax - xmin)/(Math.log(ymax) - Math.log(ymin+0.05*ymax));
}
public double valueAt(double x)
{
return max * Math.exp((x-origin)/exponent);
}
public double valueAt(double x, double[] a)
{
return a[0] * Math.exp((x-a[1])/a[2]);
}
public String[] getParameterNames()
{
final String[] result = { "amplitude","origin","exponent" };
return result;
}
public double[] getParameterValues()
{
final double[] result = { max, origin, exponent };
return result;
}
public void setParameter(int index, double value)
{
if (index == 0) max = value;
else if (index == 1) origin = value;
else if (index == 2) exponent = value;
else throw new IllegalArgumentException("Invalid index to setParameter");
clearFit();
setChanged();
}
public void setFit(Fitter fit, double[] value)
{
if (value.length != 3)
throw new IllegalArgumentException("Argument to setParameters is of wrong length");
max = value[0];
origin = value[1];
exponent = value[2];
setFit(fit);
setChanged();
}
public String getTitle()
{
return "Exponential";
}
public Handle[] getHandles(double xLow, double xHigh, double yLow, double yHigh)
{
Handle[] result = new Handle[2];
result[0] = new Handle()
// Amplitude and origin
{
public void moveTo(double x, double y)
{
origin = x;
max = y;
clearFit();
setChanged();
}
public double getX()
{
return origin;
}
public double getY()
{
return max;
}
};
result[1] = new Handle()
// Reciprocal exponent
{
public void moveTo(double x, double y)
{
exponent = (x-origin)/Math.log(y/max);
clearFit();
setChanged();
}
public double getY()
{
return max * 0.367;
}
public double getX()
{
return origin - exponent;
}
};
return result;
}
private double max;
private double origin;
private double exponent;
}
Ads help maintain this website.