public void setXY(double x, double y) {
    // not validated
    // x and y between -1 and 1
    double sigmoidSteepness, sigmoidZero, baseLength;
    double sensitivity =
        1.1; // evaluated with set to 1.1: a sensitivity of 1 means the complete space of the button
             // is used. 2 means only the middle part is used, making it more sensitive and more
             // easy to get to extreme arousal (from a CHI perspective).
    if (!down) {
      p = x * sensitivity;
      d = y * sensitivity;
      baseLength = Math.max(Math.abs(p), Math.abs(d)); // MAX (P, D) base
      // baseLength=Math.sqrt(Math.pow(p,2)+Math.pow(d,2))/Math.sqrt(2.0); //euclid P,D base

      // now do the mapping to arousal.
      // Arousal is controlled simple by using a Sigmoid function based on length of PD vector
      sigmoidSteepness = 11;
      sigmoidZero = 8;
      a = 2 / (1 + Math.exp(-(sigmoidSteepness * baseLength - sigmoidZero))) - 1;
      a_base = a;
      // Arousal is controlled simply by using a quadratic function that has 0,0,0 as middle point,
      // then drops to -1, then to 1 based MIN (P,D) length (mimics better original linear)
      // sigmoidSteepness=6;
      // sigmoidZero=0.35;
      // a=sigmoidSteepness*Math.pow(Math.max(Math.abs(p),Math.abs(d))-sigmoidZero, 2)-1;

      p = (p > 1 ? 1 : (p < -1 ? -1 : p));
      d = (d > 1 ? 1 : (d < -1 ? -1 : d));
      a = (a > 1 ? 1 : (a < -1 ? -1 : a));

    } else {
      double tmp_p, tmp_d;
      tmp_p = x * sensitivity;
      tmp_d = y * sensitivity;
      baseLength = Math.max(Math.abs(tmp_p), Math.abs(tmp_d)); // MAX (P, D) base
      // baseLength=Math.sqrt(Math.pow(p,2)+Math.pow(d,2))/Math.sqrt(2.0); //euclid P,D base

      // now do the mapping to arousal.
      // Arousal is controlled simple by using a Sigmoid function based on length of PD vector
      sigmoidSteepness = 11;
      sigmoidZero = 8;
      // a=a_base-(d-tmp_d)+2/(1+Math.exp(-(sigmoidSteepness*baseLength-sigmoidZero)))-1;
      a = tmp_d;
      // Arousal is controlled simply by using a quadratic function that has 0,0,0 as middle point,
      // then drops to -1, then to 1 based MIN (P,D) length (mimics better original linear)
      // sigmoidSteepness=6;
      // sigmoidZero=0.35;
      // a=sigmoidSteepness*Math.pow(Math.max(Math.abs(p),Math.abs(d))-sigmoidZero, 2)-1;

      // p=(p>1?1:(p<-1?-1:p));
      // d=(d>1?1:(d<-1?-1:d));
      a = (a > 1 ? 1 : (a < -1 ? -1 : a));
    }
  }
 protected void plotScatterDiagram() {
   // plot sample as one dimensional scatter plot and Gaussian
   double xmax = 5.;
   double xmin = -5.;
   DatanGraphics.openWorkstation(getClass().getName(), "E3Min_1.ps");
   DatanGraphics.setFormat(0., 0.);
   DatanGraphics.setWindowInComputingCoordinates(xmin, xmax, 0., .5);
   DatanGraphics.setViewportInWorldCoordinates(-.15, .9, .16, .86);
   DatanGraphics.setWindowInWorldCoordinates(-.414, 1., 0., 1.);
   DatanGraphics.setBigClippingWindow();
   DatanGraphics.chooseColor(2);
   DatanGraphics.drawFrame();
   DatanGraphics.drawScaleX("y");
   DatanGraphics.drawScaleY("f(y)");
   DatanGraphics.drawBoundary();
   double xpl[] = new double[2];
   double ypl[] = new double[2];
   // plot scatter diagram
   DatanGraphics.chooseColor(1);
   for (int i = 0; i < y.length; i++) {
     xpl[0] = y[i];
     xpl[1] = y[i];
     ypl[0] = 0.;
     ypl[0] = .1;
     DatanGraphics.drawPolyline(xpl, ypl);
   }
   // draw Gaussian corresponding to solution
   int npl = 100;
   xpl = new double[npl];
   ypl = new double[npl];
   double fact = 1. / (Math.sqrt(2. * Math.PI) * x.getElement(1));
   double dpl = (xmax - xmin) / (double) (npl - 1);
   for (int i = 0; i < npl; i++) {
     xpl[i] = xmin + (double) i * dpl;
     ypl[i] = fact * Math.exp(-.5 * Math.pow((xpl[i] - x.getElement(0)) / x.getElement(1), 2.));
   }
   DatanGraphics.chooseColor(5);
   DatanGraphics.drawPolyline(xpl, ypl);
   // draw caption
   String sn = "N = " + nny;
   numForm.setMaximumFractionDigits(3);
   numForm.setMinimumFractionDigits(3);
   String sx1 = ", x_1# = " + numForm.format(x.getElement(0));
   String sx2 = ", x_2# = " + numForm.format(x.getElement(1));
   String sdx1 = ", &D@x_1# = " + numForm.format(Math.sqrt(cx.getElement(0, 0)));
   String sdx2 = ", &D@x_2# = " + numForm.format(Math.sqrt(cx.getElement(1, 1)));
   caption = sn + sx1 + sx2 + sdx1 + sdx2;
   DatanGraphics.setBigClippingWindow();
   DatanGraphics.chooseColor(2);
   DatanGraphics.drawCaption(1., caption);
   DatanGraphics.closeWorkstation();
 }
 public double evaluate_1(double x) {
   return (2 * x / x * x + 1)
       - 0.4 * Math.exp(0.4 * x) * Math.cos(Math.PI * x)
       + Math.PI * Math.exp(0.4 * x) * Math.sin(Math.PI * x);
 }
 protected void compute() {
   n = (int) ni[0].parseInput();
   t0 = ni[1].parseInput();
   deltat = ni[2].parseInput();
   x1 = ni[3].parseInput();
   x2 = ni[4].parseInput();
   sigma = ni[5].parseInput();
   // generate data points
   t = new DatanVector(n);
   y = new DatanVector(n);
   dy = new DatanVector(n);
   rand = DatanRandom.standardNormal(n);
   for (int i = 0; i < n; i++) {
     t.setElement(i, t0 + (double) i * deltat);
     y.setElement(i, x1 * Math.pow(t.getElement(i), x2) + sigma * rand[i]);
     dy.setElement(i, sigma);
   }
   // find 1st approximation of unknowns by method of log-log plot
   tlog = new double[n];
   ylog = new double[n];
   dellog = new double[n];
   int npos = 0;
   for (int i = 0; i < n; i++) {
     if (t.getElement(i) > 0. && y.getElement(i) > 0.) {
       tlog[npos] = Math.log(t.getElement(i));
       ylog[npos] = Math.log(y.getElement(i));
       dellog[npos] = 1.;
       npos++;
     }
   }
   DatanVector vtlog = new DatanVector(npos);
   DatanVector vylog = new DatanVector(npos);
   DatanVector vdellog = new DatanVector(npos);
   for (int j = 0; j < npos; j++) {
     vtlog.setElement(j, tlog[j]);
     vylog.setElement(j, ylog[j]);
     vdellog.setElement(j, dellog[j]);
   }
   LsqPol lp = new LsqPol(vtlog, vylog, vdellog, 2);
   x = lp.getResult();
   x.setElement(0, Math.exp(x.getElement(0)));
   df.writeLine(" x = " + x.toString());
   // perform fit
   int[] list = {1, 1};
   powerlaw = new Powerlaw();
   LsqNon ln = new LsqNon(t, y, dy, x, list, powerlaw);
   x = ln.getResult();
   x1 = x.getElement(0);
   x2 = x.getElement(1);
   cov = ln.getCovarianceMatrix();
   delx1 = Math.sqrt(cov.getElement(0, 0));
   delx2 = Math.sqrt(cov.getElement(1, 1));
   rho = cov.getElement(1, 0) / (delx1 * delx2);
   m = ln.getChiSquare();
   p = 1. - StatFunct.cumulativeChiSquared(m, n - 1);
   // curve of fitted exponential
   xpl = new double[1001];
   ypl = new double[1001];
   dpl = (double) (n - 1) * deltat / 1000.;
   for (int i = 0; i < 1001; i++) {
     xpl[i] = t0 + (double) i * dpl;
     ypl[i] = x1 * Math.pow(xpl[i], x2);
   }
   // prepare data points for plotting
   datx = new double[n];
   daty = new double[n];
   datsx = new double[n];
   datsy = new double[n];
   datrho = new double[n];
   for (int i = 0; i < n; i++) {
     datx[i] = t.getElement(i);
     daty[i] = y.getElement(i);
     datsx[i] = 0.;
     datsy[i] = dy.getElement(i);
     datrho[i] = 0.;
   }
   // display data and fitted curve
   caption =
       "x_1#="
           + String.format(Locale.US, "%5.2f", x1)
           + ", x_2#="
           + String.format(Locale.US, "%5.2f", x2)
           + ", &D@x_1#="
           + String.format(Locale.US, "%5.2f", delx1)
           + ", &D@x_2#="
           + String.format(Locale.US, "%5.2f", delx2)
           + ", &r@="
           + String.format(Locale.US, "%5.2f", rho)
           + ", M="
           + String.format(Locale.US, "%5.2f", m)
           + ", P="
           + String.format(Locale.US, "%6.4f", p);
   new GraphicsWithDataPointsAndPolyline(
       getClass().getName(),
       "",
       xpl,
       ypl,
       1,
       .3,
       datx,
       daty,
       datsx,
       datsy,
       datrho,
       "t",
       "y",
       caption);
 }
 public double evaluate(double x) {
   return Math.log(x * x + 1) - Math.exp(0.4 * x) * Math.cos(Math.PI * x);
 }