/*
 * 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.*;
import java.awt.image.BufferedImage;
/*
 * NewJApplet.java
 *
 * Created on 19.03.2010, 13:48:22
 */

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

    double gx, gy, h, x, y, hxx, hyy, hxy, hixx, hiyy, hixy,xstart,ystart,
            pointx,pointy;
    double[] xsd, ysd,gsd,hsd;
    int nmax = 50000;
    int count = 0;
    int i;
    int[] ixarr, iyarr;
    Graphics2D gb;
    BufferedImage image;
    Point point1, point2;
    boolean conjug = false;
    double lastgrad=0;
    double lasth=0;

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

                public void run() {
                    initComponents();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        xsd = new double[nmax];
        ysd = new double[nmax];
        gsd=new double[nmax];
        hsd=new double[nmax];
        ixarr = new int[nmax];
        iyarr = new int[nmax];

        count = 0;
        for (i = 0; i < nmax; i++) {
            ixarr[i] = 0;
            iyarr[i] = 0;
        }

        image =
                new BufferedImage(400, 400, BufferedImage.TYPE_INT_RGB);
        gb = (Graphics2D) image.createGraphics();
        gb.setBackground(Color.white);
        gb.clearRect(0, 0, 400, 400);

        x = 0.0;
        y = 2.0;
        
        xstart=x;
        ystart=y;
        
        

        drawiso();


    }

    /** 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();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jPanel2 = new myPanel2();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        jPanel1.setMaximumSize(new java.awt.Dimension(420, 420));
        jPanel1.setMinimumSize(new java.awt.Dimension(420, 420));
        jPanel1.setPreferredSize(new java.awt.Dimension(420, 420));
        jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jPanel1MousePressed(evt);
            }
        });
        jPanel1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                jPanel1MouseDragged(evt);
            }
        });

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

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

        jButton2.setText("Newton");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel1.setText("jLabel1");

        jButton3.setText("Conj Grad");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 453, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 420, Short.MAX_VALUE)
        );

        jLabel2.setText("jLabel1");

        jLabel3.setText("jLabel1");

        jLabel4.setText("click to select initial point");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(141, 141, 141)
                        .addComponent(jLabel4)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(43, 43, 43)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(400, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton2, jButton3});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel3))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(70, 70, 70)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton2)
                            .addComponent(jButton1)
                            .addComponent(jButton3))))
                .addContainerGap(69, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

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

    private void jPanel1MouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanel1MouseDragged
        double x0, y0, slope, pref;

        Graphics g = jPanel1.getGraphics();
        Graphics2D g2 = (Graphics2D) g;
        point1 = jPanel1.getMousePosition();
        x0 = point1.getX() * 0.005 - 0.1;
        y0 = point1.getY() * 0.005 - 0.1;

        if ((x0 > 0.0) && (x0 < 2.0) && (y0 > 0.0) && (y0 < 2.0)) {

            g2.drawImage(image, 20, 20, null);
            g.setColor(Color.RED);
            g2.fillOval((int) point1.getX()-4, (int) point1.getY()-4, 8, 8);

            x = x0;
            y = y0;
            xstart=x;
            ystart=y;
            pointx = point1.getX();
            pointy = point1.getY();
    }

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

    private void jPanel1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanel1MousePressed
        double x0, y0, slope, pref;
        int ah, aw;
        Graphics g = jPanel1.getGraphics();
        Graphics2D g2 = (Graphics2D) g;
        point1 = jPanel1.getMousePosition();
        x0 = point1.getX() * 0.005 - 0.1;
        y0 = point1.getY() * 0.005 - 0.1;

        ah = jPanel1.getHeight();
        aw = jPanel1.getWidth();
        if ((x0 > 0.0) && (x0 < 2.0) && (y0 > 0.0) && (y0 < 2.0)) {

        g.clearRect(20, 20, 400, 400);


        g2.drawImage(image, 20, 20, null);
        g.setColor(Color.RED);
        g2.fillOval((int) point1.getX()-4, (int) point1.getY()-4, 8, 8);

        x = x0;
        y = y0;
 xstart=x;
            ystart=y;
            pointx = point1.getX();
            pointy = point1.getY();
        }
    // TODO add your handling code here:
    }//GEN-LAST:event_jPanel1MousePressed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        NR();
        jPanel1.repaint();
        jPanel2.repaint();
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        conjug = true;
        steepest();
        jPanel1.repaint();
        jPanel2.repaint();
    // TODO add your handling code here:
    }//GEN-LAST:event_jButton3ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables

    public void getx0() {
    }

    public void drawiso() {
        int ix, iy, ic, ah, aw;
        double x, y, f;

        Graphics g;
        g = jPanel1.getGraphics();
        Graphics2D g2 = (Graphics2D) g;
        //ah = jPanel1.getHeight();
        //aw = jPanel1.getWidth();


        //g.clearRect(0, 0, aw, ah);

        for (ix = 0; ix < 400; ix++) {
            x = ix * 0.005;
            for (iy = 0; iy < 400; iy++) {
                y = iy * 0.005;

                f = rosf(x, y);

                //ic = (int) (2 * f);
                //if ((ic % 2.0) > 0.5) {
                //    gb.setColor(Color.LIGHT_GRAY);
                //} else {
                //    gb.setColor(Color.WHITE);
                //}

                if (f < 0.1) {
                    gb.setColor(Color.getHSBColor((float)0.5,(float)0.2,(float)1.));
                } else {
                    if (f < 0.5) {
                        gb.setColor(Color.LIGHT_GRAY);
                    } else {
                        if (f < 5.0) {
                            gb.setColor(Color.WHITE);
                        } else {
                            if (f < 50.0) {gb.setColor(Color.getHSBColor((float)0.25,(float)0.2,(float)1.));
                               
                            } else {
                                if (f < 100.0) {
                                    gb.setColor(Color.WHITE);
                                } else {
                                    gb.setColor(Color.getHSBColor((float)0.,(float)0.2,(float)1.));
                                }
                            }
                        }
                    }
                }

                
                gb.drawLine(ix, iy, ix, iy);


            }

        }



        g2.drawImage(image, 20, 20, null);


    }

    public void drawaxes() {
        Graphics g;
        int ix, iy;
        g = jPanel1.getGraphics();
        Graphics2D g2 = (Graphics2D) g;



    }

    public double drosl(double x, double y, double sx, double sy, double xlam) {
        double x1, y1, dh;

        x1 = x + xlam * sx;
        y1 = y + xlam * sy;
        dh = 200.0 * (y1 - x1 * x1) * (sy - 2. * x1 * sx) - 2. * (1. - x1) * sx;

        return dh;

    }

    public double rosl(double x, double y, double sx, double sy, double xlam) {
        double c1, c2, h, x1, y1;

        x1 = x + xlam * sx;
        y1 = y + xlam * sy;
        c1 = y1 - x1 * x1;
        c2 = 1 - x1;
        h = 100.0 * c1 * c1 + c2 * c2;

        return h;
    }

    public double rosf(double x, double y) {
        double c1, c2;

        c1 = y - x * x;
        c2 = 1 - x;
        h = 100.0 * c1 * c1 + c2 * c2;

        return h;

    }

    public void rosb(double x, double y) {
        double c1, c2;

        c1 = y - x * x;
        c2 = 1 - x;
        h = 100.0 * c1 * c1 + c2 * c2;


        gx = 400.0 * x * (x * x - y) + 2.0 * (x - 1.0);
        gy = 200.0 * (y - x * x);


    }

    public void roshess(double x, double y) {
        double c1, c2;

        c1 = y - x * x;
        c2 = 1 - x;

        h = 100.0 * c1 * c1 + c2 * c2;
        gx = -400.0 * x * c1 - 2.0 * c2;
        gy = 200.0 * c1;
        hxx = 1200. * x * x - 400.0 * y + 2.0;
        hyy = 200.0;
        hxy = -400. * x;


    }

    public void steepest() {
        int i, i200, ix, iy, ic, ixo, iyo, ah, aw,countl;
        double f0,f1,f2, xl, sx, sy, df0, df1, xl0, xl1, xo, yo, gnorm, gnorm2,gnorm0, beta, sx0, sy0;
        double xa,xb,xc,xd,fa,fb,fc,fd;
        
        Graphics g;
        g = jPanel1.getGraphics();
        Graphics2D g2 = (Graphics2D) g;

        g2.drawImage(image, 20, 20, null);


        ix = 20 + (int) (x / 0.005);
        iy = 20 + (int) (y / 0.005);

        count = 0;

        xsd[0] = x;
        ysd[0] = y;
        rosb(x, y);
        gnorm = Math.hypot(gx, gy);

        gsd[0]=gnorm;
        hsd[0]=h;

        sx0 = -gx;
        sy0 = -gy;

        sx = sx0;
        sy = sy0;


        i200 = 1;
        while ((i200 < nmax-2) && (gnorm > 1.0E-20)) {
            ixo = ix;
            iyo = iy;

            sx0 = sx;
            sy0 = sy;

            rosb(x, y);

            gnorm0 = gnorm;
            gnorm = Math.hypot(gx, gy);

            if (conjug) {
                beta = gnorm / gnorm0;
                beta = beta * beta;


                //jTextArea1.append(" beta2=" + beta + "\n");



                sx = -gx + beta * sx0;
                sy = -gy + beta * sy0;
            } else {
                sx = -gx;
                sy = -gy;

            }

//     determine xlam
              gnorm2=gx*gx+gy*gy;
           
         
            f0=h;
            
            {xl=(1.0E-15+1.0E-14*h)/gnorm2;}
            
            
            f1=rosf(x+xl*sx,y+xl*sy);
            f2=f1;
         
            if (f1 > f0) {  //step too large??
                    xa=xl;
                    countl = 0;
                    do {
                        
                        xl = 0.5 * xl;
                        f1 = rosf(x + xl * sx, y + xl * sy);
                        countl++;
                    } while ((countl < 100) && (f1 > f0));
                    
                    if(f1>=f0) break;
                    xb=xl;
                    if(xb<xa){
                    xb=xa;
                    xa=xl;
                    
                    }
                do {  //ternary search

                    xc = (2 * xa + xb) / 3.0;
                    xd = (xa + 2 * xb) / 3.0;
                    fc = rosf(x + xc * sx, y + xc * sy);
                    fd = rosf(x + xd * sx, y + xd * sy);
                    if (fc < fd) {
                        xb = xd;
                    } else {
                        xa = xc;
                    }

                } while ((Math.abs(xb - xa) > 1.0E-14)&& !(fc==fd));
                xl = (xa + xb) / 2.0;
            }
            else{
                f2 = f1;
                if (f1 < f0) {
                    countl = 0;
                    do {
                        f1 = f2;
                        xl = 2 * xl;
                        f2 = rosf(x + xl * sx, y + xl * sy);
                        countl++;
                    } while ((countl < 100) && (f2 < f1));
                }

                if (xl < 0) {
                    xa = xl;
                    xb = xl*0.5;
                } else {
                    xa = xl*0.5;
                    xb = xl;
                }
                do {  //ternary search

                    xc = (2 * xa + xb) / 3.0;
                    xd = (xa + 2 * xb) / 3.0;
                    fc = rosf(x + xc * sx, y + xc * sy);
                    fd = rosf(x + xd * sx, y + xd * sy);
                    if (fc < fd) {
                        xb = xd;
                    } else {
                        xa = xc;
                    }

                } while ((Math.abs(xb - xa) > 1.0E-14)&& !(fc==fd));
                xl = (xa + xb) / 2.0;
            }
            
            
          
            x = x + xl * sx;
            y = y + xl * sy;
            
            g.fillOval(16 + (int) (x / 0.005),16 + (int) (y / 0.005), 8, 8);

            //jTextArea1.append(" " + x + "  " + y + "  " + h + "  " + gx + "  " + gy + "\n");

            rosb(x,y);


            xsd[i200] = x;
            ysd[i200] = y;
            gsd[i200] =gnorm;
            hsd[i200] =h;
            count++;

            //jLabel1.setText(" count " + count);

            ix = 20 + (int) (x / 0.005);
            iy = 20 + (int) (y / 0.005);

            g.setColor(Color.BLACK);
            g.drawLine(ixo, iyo, ix, iy);

             //jTextArea1.append(" " + i200 + "  "  + h + "  " + gnorm +"\n");
            i200++;
        }

        ix = 0;
         rosb(x,y);
         gnorm = Math.hypot(gx, gy);
        gsd[count+1]=gnorm;
        hsd[count+1]=h;

        lastgrad=gnorm;
        lasth=h;

         x=xstart;
         y=ystart;
         g.fillOval((int) pointx-4, (int) pointy-4, 8, 8);

    }

    public void NR() {
        int i, i200, ix, iy, ixo, iyo;

        double det, sx, sy, gnorm;



        Graphics g;
        g = jPanel1.getGraphics();
        //Graphics2D g2 = (Graphics2D) g;

        //g2.drawImage(image, 20, 20, null);

        ix = 20+(int) (x / 0.005);
        iy = 20+(int) (y / 0.005);


        xsd[0] = x;
        ysd[0] = y;
        rosb(x,y);
         gnorm = Math.hypot(gx, gy);
        gsd[0]=gnorm;
        hsd[0]=h;

        i200 = 1;
        count = 0;
        do {

            ixo = ix;
            iyo = iy;

            roshess(x, y);
            gnorm = Math.hypot(gx, gy);

// invert Hessian

            det = hxx * hyy - hxy * hxy;
            hixx = hyy / det;
            hiyy = hxx / det;
            hixy = -hxy / det;



            sx = -(hixx * gx + hixy * gy);
            sy = -(hixy * gx + hiyy * gy);

            x = x + sx;
            y = y + sy;

            

            xsd[i200] = x;
            ysd[i200] = y;
            hsd[i200] = h;
            gsd[i200] = gnorm;

            //jTextArea1.append(" " + i200 + "  "  + h + "  " + gnorm +"\n");

            count++;

            ix = 20 + (int) (x / 0.005);
            iy = 20 + (int) (y / 0.005);

            g.setColor(Color.RED);
            g.drawLine(ixo, iyo, ix, iy);


            i200++;
        } while ((i200 < nmax) && (gnorm > 1.0E-14));
         rosb(x,y);
         gnorm = Math.hypot(gx, gy);
        gsd[count+1]=gnorm;
        hsd[count+1]=h;

        lastgrad=gnorm;
        lasth=h;
         x=xstart;
         y=ystart;
         g.fillOval((int) pointx-4, (int) pointy-4, 8, 8);
    }

    public class myPanel extends javax.swing.JPanel {

        int ix, iy, i, j, ic;
        double f, x, y;

        public void paint(Graphics g) {
            int ix, iy, ir, ix5, ix10, ix15, ix20, ix25, k, i, j, ah, aw;
            double xp, yp;
            Graphics2D g2 = (Graphics2D) g;
            int size = 200;

            //if(count==0) {super.paintComponent(g);

            //}

            super.paintComponent(g);
            ah = jPanel1.getHeight();
            aw = jPanel1.getWidth();

            jLabel1.setText("steps    " + count);
            jLabel2.setText("gradient "+lastgrad);
            jLabel3.setText("value    "+lasth);

            //g.clearRect(0, 0, aw, ah);

            g2.drawImage(image, 20, 20, null);


            g.setColor(Color.BLACK);
            g.drawLine(19, 19, 19, 420);
            g.drawLine(19, 19, 420, 19);
            for (i = 0; i < 5; i++) {
                ix = 20 + i * 100;
                g.drawLine(ix, 19, ix, 15);
                g.drawString("" + (i * 0.5), ix, 10);

            }
            for (i = 0; i < 5; i++) {
                iy = 20 + i * 100;
                g.drawLine(15, iy, 19, iy);
                g.drawString("" + (i * 0.5), 1, iy);

            }

            for (i = 0; i < nmax; i++) {
                ixarr[i] = 20 + (int) (xsd[i] / 0.005);
                iyarr[i] = 20 + (int) (ysd[i] / 0.005);


            g.drawString("x",400,10);
             g.drawString("y",10,380);

            }
            g.setColor(Color.RED);
            if (count > 1 && count < nmax) {
                g.drawPolyline(ixarr, iyarr, count);
                
                 for(i=0;i<count;i++) g.fillOval(ixarr[i]-4, iyarr[i]-4, 8, 8);
                
            }
              g.fillOval((int) pointx-4, (int) pointy-4, 8, 8);



        }





    }
    public class myPanel2 extends javax.swing.JPanel {
 int i,ix,iy;
        public void paintComponent(Graphics g) {


            super.paintComponent(g);

            for(i=1;i<count+1;i++)
            {
             ixarr[i-1]=(int)(Math.log10(i+1.0)*100);
             if(gsd[i]>0)
             {iyarr[i-1]=50-(int)(Math.log10(gsd[i])*20);}
             else {iyarr[i-1]=330;}

             //g.drawOval(ixarr[i-1], iyarr[i-1], 5, 5);

            }
             g.setColor(Color.RED);
            if (count > 1 && count+1 < nmax) {
            g.drawPolyline(ixarr, iyarr, count);
            
            
           
            
            }

            
             g.setColor(Color.GRAY);
            for(i=-2;i<15;i++){
            iy=50+20*i;
            g.drawLine(0,iy,400,iy);

            if(i>0) g.drawString("E-"+i,410,iy); else g.drawString("E+"+(-i),410,iy);

            }
            for(i=1;i<6;i++){
            ix=100*i;
            g.drawLine(ix,0,ix,330);

            g.drawString("E"+i,ix,350);

            }

             g.setColor(Color.BLACK);
             g.drawString("gradient norm",300,10);
             g.drawString("steps",200,380);



        }

    }
}

