/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import java.text.*;
/*
 * NewJApplet.java
 *
 * Created on 14.03.2010, 09:16:51
 */

/**
 *
 * @author poj
 */
public class NewJApplet extends javax.swing.JApplet {

    int nmax = 10000;
    int Npoints = 100;
    int nplot = 100;
    double[] q1, q2, a1, a2, bk, x, y;
    double a = 10;
    double b = 0.001;
    double reler = 0.1;
    double af = 0;
    double bf = 0;
    int[] ixp, iyp;
    DecimalFormat df;

    /** Initializes the applet NewJApplet */
    public void init() {
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {

                public void run() {
                    initComponents();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        q1 = new double[nmax];
        q2 = new double[nmax];
        a1 = new double[nmax];
        a2 = new double[nmax];
        bk = new double[nmax];
        x = new double[nmax];
        y = new double[nmax];
        ixp = new int[nplot];
        iyp = new int[nplot];

        df = new DecimalFormat("##0.000000");

        a = jSlider2.getValue();
        jLabel1.setText("true a=" + df.format(a));
        b = jSlider1.getValue() * 0.0001;
        jLabel2.setText("true b=" + df.format(b));
        reler = jSlider3.getValue() * 0.005;
        jLabel5.setText("err=" + df.format(reler));
        gendat();
        fit1();
        jPanel1.repaint();
    }

    /** This method is called from within the init() method to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new myPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jSlider1 = new javax.swing.JSlider();
        jLabel2 = new javax.swing.JLabel();
        jSlider2 = new javax.swing.JSlider();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jSlider3 = new javax.swing.JSlider();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 429, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 495, Short.MAX_VALUE)
        );

        jLabel3.setText("jLabel3");

        jLabel4.setText("jLabel4");

        jSlider1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                jSlider1MouseDragged(evt);
            }
        });

        jLabel2.setText("jLabel2");

        jSlider2.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                jSlider2MouseDragged(evt);
            }
        });

        jLabel1.setText("jLabel1");

        jButton1.setText("Go");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jSlider3.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                jSlider3MouseDragged(evt);
            }
        });

        jLabel5.setText("jLabel5");

        jLabel6.setText("fitted parameters:");

        jLabel7.setText("jLabel7");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jSlider3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jSlider2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6)))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(88, 88, 88)
                        .addComponent(jLabel5))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(84, 84, 84)
                        .addComponent(jLabel2))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(86, 86, 86)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(75, 75, 75)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(434, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSlider3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(27, 27, 27)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSlider2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(50, 50, 50)
                        .addComponent(jButton1)
                        .addGap(58, 58, 58)
                        .addComponent(jLabel6)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel4)
                        .addGap(28, 28, 28)
                        .addComponent(jLabel7))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(84, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        gendat();
        fit1();
        jPanel1.repaint();// TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jSlider2MouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jSlider2MouseDragged
        a = jSlider2.getValue();
        jLabel1.setText("true a=" + df.format(a));

    // TODO add your handling code here:
    }//GEN-LAST:event_jSlider2MouseDragged

    private void jSlider1MouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jSlider1MouseDragged
        b = jSlider1.getValue() * 0.0001;

        jLabel2.setText("true b=" + df.format(b));
    // TODO add your handling code here:
    }//GEN-LAST:event_jSlider1MouseDragged

    private void jSlider3MouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jSlider3MouseDragged
        reler = jSlider3.getValue() * 0.005;

        jLabel5.setText("err=" + df.format(reler)); // TODO add your handling code here:
    }//GEN-LAST:event_jSlider3MouseDragged

    public double RMS()
    {
    double s,ds,xp,yp; int i;
    s=0;
     for (i = 0; i < Npoints; i++) {
          xp =x[i];
          yp = (af * xp + bf * xp * xp * xp)-y[i];
          s=s+yp*yp;
     }
     return Math.sqrt(s/Npoints);
     }
    
    public double randgauss() {
        double x, r, phi, rho;

        r = Math.random();
        phi = 2.0 * Math.PI * r;

        r = Math.random();
        rho = Math.sqrt(-Math.log(1 - r));

        x = rho * Math.cos(phi) * Math.sqrt(2.0);

        return x;
    }

    public void gendat() {

        int i, j;
        double r;

        //jTextArea1.append("new \n");
        for (i = 0; i < Npoints; i++) {
            x[i] = i * 3.0 + 10.0;
            y[i] = a * x[i] + b * x[i] * x[i] * x[i];

            r = randgauss();



            y[i] = y[i] * (1.0 + r * reler);
            
            //jTextArea1.append(" "+x[i]+"  "+y[i]+"\n");

        }
    }

    public void fit1() {


        double xys, x2s, x4s, x6s, yx3s, det, det1, det2, ao, bo;
        int i;
        double xp,yp,sp, sp1;
        double f1, f2, r11, r12, r22, qb1, qb2;

        xys = x2s = x4s = x6s = yx3s = 0.0;
        
        //jTextArea1.append("\n");

        for (i = 0; i < Npoints; i++) {
            xys = xys + x[i] * y[i];
            x2s = x2s + x[i] * x[i];
            x4s = x4s + x[i] * x[i] * x[i] * x[i];
            x6s = x6s + x[i] * x[i] * x[i] * x[i] * x[i] * x[i];
            yx3s = yx3s + y[i] * x[i] * x[i] * x[i];
        }

        xys = xys / Npoints;
        x2s = x2s / Npoints;
        x4s = x4s / Npoints;
        x6s = x6s / Npoints;
        yx3s = yx3s / Npoints;


        det = x2s * x6s - x4s * x4s;
        det1 = xys * x6s - yx3s * x4s;
        det2 = x2s * yx3s - xys * x4s;

        af = det1 / det;
        bf = det2 / det;

        jLabel3.setText("a=" + df.format(af));
        jLabel4.setText("b=" + df.format(bf));
        
        jLabel7.setText("RMS="+df.format(RMS()));



            for (i = 0; i < nplot; i++) {
                xp = i * 3 + 10;
                yp = (a * xp + b * xp * xp * xp);
                
                //jTextArea1.append(" "+xp+"   "+yp+"\n");
              
            }  //jTextArea1.append("\n");
            
             for (i = 0; i < nplot; i++) {
                xp = i * 3 + 10;
                yp = (af * xp + bf * xp * xp * xp);
                //jTextArea1.append(" "+xp+"   "+yp+"\n");
              
            }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JSlider jSlider2;
    private javax.swing.JSlider jSlider3;
    // End of variables declaration//GEN-END:variables

    public class myPanel extends javax.swing.JPanel {

        int ix, iy, i;
        double xp, yp;

        public void paintComponent(Graphics g) {


            super.paintComponent(g);

            g.setColor(Color.BLACK);

            g.drawLine(0,400,300,400);
            g.drawLine(0,400,0,0);
            for(i=100;i<400;i+=100){

                g.drawLine(i,400,i,410);
                g.drawString(""+i,i-15,430);

            }
            for(i=100;i<400;i+=100){

                g.drawLine(0,i,10,i);
                g.drawString(""+(i*100),20,400-i);

            }
            g.drawString("x",320,410);
            g.drawString("y=a*x+b*x*x*x", 10, 10);

            for (i = 0; i < Npoints; i++) {
                ix = (int) x[i];
                iy = 400 - (int) (0.01 * y[i]);
                g.fillOval(ix, iy, 5, 5);

            }

            for (i = 0; i < nplot; i++) {
                xp = i * 3 + 10;
                yp = (a * xp + b * xp * xp * xp);
                ixp[i] = (int) xp;
                iyp[i] = 400 - (int) (0.01 * yp);
            }
            g.drawString("black=true curve", 200, 370);
            g.drawPolyline(ixp, iyp, nplot);

            g.setColor(Color.RED);
            g.drawString("red=fitted curve", 200, 390);
            for (i = 0; i < nplot; i++) {
                xp = i * 3 + 10;
                yp = (af * xp + bf * xp * xp * xp);
                ixp[i] = (int) xp;
                iyp[i] = 400 - (int) (0.01 * yp);
            }

            g.drawPolyline(ixp, iyp, nplot);

        }
    }
}
