import java.awt.Graphics;
import java.applet.Applet;
import java.awt.Color;
public class secant2 extends java.applet.Applet{
    Color col1,col2;
    int N,taka,haba,ni,nj;
    int k,i,j,l,co,minj,minj2,minj3;
    int xz,xz2,yz,yz2,label;
    String NS,habaS,takaS;
    double Nd,takad,habad,th;
    Color cl[]=new Color[13];
    double ai[]=new double[200];
    int in;
    double a0;
    double a1;
    double a2;
    double a3;
    double a4;
    double a5;
    int ib;

  public double dou(String dous){
    double dou1;
    dou1 = (Double.valueOf(dous)).doubleValue();
    return dou1;
  }
  public double rand(){
    double rand1;
    rand1=Math.random();
    return rand1;
  }
  public double computey(double sx){
    double cy;
    int i;
    cy=0.0;
    if(ib<0){
        cy=0.0;
        for(i=0;i<=in;i++){
            cy=cy+ai[i]*Math.pow(sx,i);
        }
    }
    if(ib==0){
        cy=a5*Math.pow(sx,5.0)+a4*Math.pow(sx,4.0)+a3*Math.pow(sx,3.0)+a2*Math.pow(sx,2.0)+a1*sx+a0;
    }
    if(ib==1){
        cy=(a0+a1*sx+a2*Math.pow(sx,2.0))*(a3+Math.cos(a4*sx-a5)*Math.log(Math.abs(sx+1.0)));
    }
    if(ib==2){
        cy=a0+a1*Math.sin(a2*sx)+a3*Math.pow(Math.sin(a4*sx+a5),2.0);
    }
    if(ib==3){
        cy=a0+a1*Math.tan(a2*sx)+a3*Math.pow(Math.cos(a4*sx+a5),2.0);
    }
    if(ib==4){
        cy=(a0+a1*sx+a2*Math.pow(sx,2.0))*(a3+Math.sin(a4*sx-a5));
    }
    if(ib==5){
        cy=(a0+a1*Math.exp(a2*sx)+a3*Math.pow(Math.abs(a4),sx))*Math.cos(sx*a5);
    }
    return cy;
  }
  public void init(){
    col1=Color.black;
    col2=Color.yellow;
    cl[0]=Color.white;
    cl[1]=Color.cyan;
    cl[2]=Color.green;
    takaS=getParameter("takap");
    habaS=getParameter("habap");
    NS=getParameter("Np");
    habad=dou(habaS);
    takad=dou(takaS);
    Nd=4-50*Math.log(rand());
    haba=(int)habad;
    taka=(int)takad;
    N=(int)Nd;
  }
  double x1;
  double y1;
  int x;
  int y;
  double s;
  String sss;
  public double jou(double a,double b){
    double jou1;
    jou1=Math.pow(a,b);
    return jou1;
  }
  public double arcsinaw(double atasw){
    double artn1saw;
    artn1saw=Math.asin(atasw);
    return artn1saw;
  }
  public double d(double d1,double d2,double d3,double d4){
    double dw;
    dw=jou(jou(d3-d1,2.0)+jou(d4-d2,2.0),0.5);
    return dw;
  }

    public void paint(java.awt.Graphics g){
        g.setColor(col1);
        g.fillRect(1,1,haba,taka);

        double centerx;
        double centery;
        double ratex;
        double ratey;
        double pi;
        double x;
        double y;

        double stepx;
        double iniratex;
        centerx=250.0;
        centery=175.0;
        pi=3.141592653589793;
        iniratex=3.0;
        stepx=0.1;
        ib=(int)(9.0*rand())-3;
        if(ib==-3){
            in=7;
        }
        if(ib==-2){
            in=10;
        }
        if(ib==-1){
            in=5+(int)(95*rand());
        }
        if(ib<0){
            for(i=0;i<=in;i++){
                if(rand()<0.5){
                    ai[i]=-Math.log(rand())/(i*i+1);
                 }else{
                    ai[i]=Math.log(rand())/(i*i+1);
                }
            }
        }
        if(ib==0){
            if(rand()<0.3){
                a5=0.0;
            }else{
                if(rand()<0.5){
                    a5=-0.001*Math.log(rand());
                 }else{
                    a5=0.001*Math.log(rand());
                }
            }
            a4=-0.4*Math.log(rand());
            if(a5==0.0){
                if(rand()<0.3){
                    a4=0.0;
                }
            }

            a3=-Math.log(rand());
            if(a4==0.0){
                if(rand()<0.3){
                    a3=0.0;
                }
            }
            a2=-Math.log(rand());
            a1=-10.0*Math.log(rand());
            a0=-100.0*Math.log(rand());
        }
        if(ib>0){
            if(rand()<0.5){
                a5=-Math.log(rand());
            }else{
                a5=Math.log(rand());
            }
            if(rand()<0.5){
                a4=-Math.log(rand());
            }else{
                a4=Math.log(rand());
            }
            if(rand()<0.5){
                a3=-Math.log(rand());
            }else{
                a3=Math.log(rand());
            }
            if(rand()<0.5){
                a2=-Math.log(rand());
            }else{
                a2=Math.log(rand());
            }
            if(rand()<0.5){
                a1=-Math.log(rand());
            }else{
                a1=Math.log(rand());
            }
            if(rand()<0.5){
                a0=-Math.log(rand());
            }else{
                a0=Math.log(rand());
            }
        }
        double maxabsy;
        double ylimit;
        double maxx;
        double maxxp;
        double maxxm;
        double itex;
        double x0;
        double x1;
        double xp;
        double xm;
        double ym;
        double eps;
        eps=0.0000001;
        g.setColor(Color.yellow);
        g.drawLine(1,(int)centery,550,(int)centery);
        g.drawLine((int)centerx,1,(int)centerx,400);
        maxabsy=0.0;

        double h;
        h=Math.pow(0.1,6.0);

        x0=-3.0+6.0*rand();
        x1=-3.0+6.0*rand();
        xm=x0;
        double xh;
        double yh;
        double yp;
        double ymp;
        y=computey(xm);
        xh=xm+h;
        yh=computey(xh);
        yp=(yh-y)/h;
        ymp=yp;
        x=x1;
        y=0.0;
        for(i=0;i<1000;i++){
            y=computey(x);
            xh=x+h;
            yh=computey(xh);
            yp=(yh-y)/h;
            if(Math.abs(yp)<eps){
                break;
            }
            xp=x-yp*(x-xm)/(yp-ymp);
            xm=x;
            ymp=yp;
            x=xp;
        }//i
        ylimit=1000.0;
        if(ib<0){
            ylimit=10.0;
        }
        itex=Math.abs(x)*1.3;
        maxxp=250.0/iniratex;
        for(x=0.0;x<250.0/iniratex;x=x+stepx){
            y=computey(x);
            if(Math.abs(y)>maxabsy){
                maxabsy=Math.abs(y);
                if(x>3.0 && maxabsy>ylimit){
                    maxxp=x;
                    break;
                }
            }
        }//x
        maxxm=250.0/iniratex;
        for(x=0.0;x>-250.0/iniratex;x=x-stepx){
            y=computey(x);
            if(Math.abs(y)>maxabsy){
                maxabsy=Math.abs(y);
                if(x<-3.0 && Math.abs(y)>ylimit){
                    maxxm=-x;
                    break;
                }
            }
        }//x
        if(maxabsy>ylimit){
            maxabsy=ylimit;
        }
        maxx=maxxp;
        if(maxxm<maxx){
            maxx=maxxm;
        }
        if(itex>maxx){
            maxx=itex;
        }
        ratex=250.0/maxx;
        ratey=150.0/maxabsy;
        g.setColor(Color.pink);
        g.drawLine((int)(centerx-10),(int)(centery-150),(int)(centerx+10),(int)(centery-150));
        g.drawString(""+maxabsy,(int)(centerx-10),(int)(centery-135));
        g.drawLine((int)(centerx-10),(int)(centery+150),(int)(centerx+10),(int)(centery+150));
        g.drawString(""+(-maxabsy),(int)(centerx-10),(int)(centery+170));
        stepx=maxx/10000.0;
        x=-maxx;
        for(i=0;i<20000;i++){
            if(i%1000==0){
                g.setColor(Color.black);
                g.fillRect(15,1,230,15);
                g.setColor(Color.yellow);
                g.drawString("Please wait "+ (int)(i*100/200000)+"%",15,15);
                g.setColor(Color.white);
            }
            y=computey(x);
            g.setColor(Color.white);
            g.drawOval((int)(centerx+ratex*x-0.5),(int)(centery-ratey*y-0.5),1,1);
            xh=x+h;
            yh=computey(xh);
            yp=(yh-y)/h;
            g.setColor(Color.green);
            g.drawOval((int)(centerx+ratex*x-0.5),(int)(centery-ratey*yp-0.5),1,1);
            x=x+stepx;
        }//x
        g.setColor(Color.black);
        g.fillRect(15,1,230,15);
        x=x0;
        g.setColor(Color.pink);
        g.drawString("x0="+x0,15,120);
        g.drawString("x1="+x1,15,135);
        xm=x0;
        y=computey(xm);
        xh=xm+h;
        yh=computey(xh);
        yp=(yh-y)/h;
        ymp=yp;
        x=x1;
        g.drawString("0",(int)(centerx+ratex*x0),(int)(centery));
        g.drawString("1",(int)(centerx+ratex*x1),(int)(centery));
        y=0.0;
        for(i=0;i<1000;i++){
            y=computey(x);
            xh=x+h;
            yh=computey(xh);
            yp=(yh-y)/h;
            if(Math.abs(yp)<eps){
                break;
            }
            g.setColor(Color.gray);
            g.drawString(""+i,(int)(centerx+ratex*x),(int)(centery));
            g.setColor(Color.blue);
            g.drawLine((int)(centerx+ratex*x),(int)(centery-ratey*y),(int)(centerx+ratex*x),(int)(centery));
            xp=x-yp*(x-xm)/(yp-ymp);
            g.setColor(Color.red);
            g.drawLine((int)(centerx+ratex*x),(int)(centery-ratey*y),(int)(centerx+ratex*xp),(int)(centery));
            xm=x;
            ymp=yp;
            x=xp;
        }//i
        g.setColor(Color.cyan);
        if(ib<0){
            g.drawString("f(x)=sum_{i=0,n}ai*x^i,  n="+in,15,15);
        }
        if(ib==0){
            g.drawString("f(x)=a5*x^5+a4*x^4+a3*x^3+a2*x^2+a1*x+a0",15,15);
        }
        if(ib==1){
            g.drawString("f(x)=(a0+a1*x+a2*x^2)*(a3+cos(a4*x-a5)*log(|x+1|))",15,15);
        }
        if(ib==2){
            g.drawString("f(x)=a0+a1*sin(a2*x)+a3*sin(a4*x+a5)^2",15,15);
        }
        if(ib==3){
            g.drawString("f(x)=a0+a1*tan(a2*x)+a3*cos(a4*x+a5)^2",15,15);
        }
        if(ib==4){
            g.drawString("f(x)=(a0+a1*x+a2*x^2)*(a3+sin(a4*x-a5)",15,15);
        }
        if(ib==5){
            g.drawString("f(x)=(a0+a1*exp(a2*x)+a3*|a4|^x)*cos(x*a5)",15,15);
        }

        if(ib>=0){
            g.drawString("a5="+a5,15,30);
            g.drawString("a4="+a4,15,45);
            g.drawString("a3="+a3,15,60);
            g.drawString("a2="+a2,15,75);
            g.drawString("a1="+a1,15,90);
            g.drawString("a0="+a0,15,105);
        }
        g.drawString("x*="+x,15,150);
        g.drawString("f(x*)="+y,15,165);
        g.drawString("f'(x*)="+yp,15,195);
        g.drawString("cnt="+i,15,210);
        g.drawString("eps="+eps,15,225);
        g.setColor(Color.cyan);
        g.drawString(""+maxx,450,190);

    }
}
