import java.io.*;
import java.awt.Graphics;
import java.applet.Applet;


class f5 extends fi_xi
{
  double[] func(double[] x)
  {
  double b[];
  b=new double[2];
  b[0]=0.7654*x[0]*x[0]*x[0]*x[0]-169.9*x[0]*x[0]*x[0]+12400*x[0]*x[0]-125*x[1]*x[1]
  -250*x[0]*x[1]-424700*x[0]+11.03e6;
  b[1]=0.9053*x[1]*x[1]*x[1]*x[1]-207.6*x[1]*x[1]*x[1]+15980.0*x[1]*x[1]-125*x[0]*x[0]
  -250*x[0]*x[1]-554800*x[1]+12.7e6;
  return b;
  }
}

public class PROBLEM10_11b extends Applet
{
// Solve the given set of equations
    public void paint(Graphics g)
    {
    f5 b1=new f5();
    double[] x=new double[2];
    x[0]=60.0;
    x[1]=100.0;
    double[] y=new double[2];
    x=newton(x,b1);
    y=b1.func(x);
    g.drawString("x0 = "+x[0]+"   f1="+y[0],25,70);
    g.drawString("x1 = "+x[1]+"   f2="+y[1],25,85);
    }

public static double[] newton( double x[],fi_xi y)
{
//solution of nonlinear system of equations
//by using newton raphson method.
//this function does not require derivatives
//it is utilised method derivative to calculate derivatives
double ti=1.0;
int i,ii,jj;
int nmax=500;
double tolerance=1.0e-15;
int n=x.length;
double b[];
b=new double[n];
double dy[][];
dy=new double[n][n];
i=0;
for(i=0;i<n;i++)
{
b[i]=1.0;
}
while( i++ < nmax && Matrix.abs(b) > tolerance )
{
  for(ii=0;ii<n;ii++)
  {
    for(jj=0;jj<n;jj++)
    {
        dy[ii][jj]=derivative(y,ii,x,jj);
    }
  }
    b=Matrix.multiply(Matrix.divide(y.func(x),dy),-ti);
    x=Matrix.add(x,b);
}
if(i >= nmax) System.out.println("warning maximum number of iterations"+
                                 " results may not be valid"          );
return x;
}

public static double derivative(fi_xi f_deriv,
int equation_ref,double x[],int x_ref)
{
// This method calculates derivative of a function selected from a set of
// functions. Accuracy of method can be adjusted by changing
// variables h0 and n
// function input should be in the form given in abstract class
// f_xi
double h0=0.0256;
int i,m;
int n=7;
double f1[];
f1=new double[x.length];
double f2[];
f2=new double[x.length];
double x1[];
x1=new double[x.length];
double x2[];
x2=new double[x.length];
for(i=0;i<x.length;i++)
{
x1[i]=x[i];
x2[i]=x[i];
}
//derivative of a simple function
double T[][];
T=new double[n][n];
double h[];
h=new double[n];
//vector<double> h(n,0);
for(i=0;i<n;i++)
{
  h[i]=0;
  for(int j=0;j<n;j++)
    T[i][j]=0;
}
h[0]=h0;
double r=0.5;
for( i=1;i<n;i++)
{
h[i]=h0*Math.pow(r,i);
}

for(i=0;i<n;i++)
{
x1[x_ref]+=h[i];
x2[x_ref]-=h[i];
f1=f_deriv.func(x1);
f2=f_deriv.func(x2);
T[i][0]=( f1[equation_ref] - f2[equation_ref])/(2.0*h[i]);
x1[x_ref]=x[x_ref];
x2[x_ref]=x[x_ref];
}
for(m=1;m<n;m++)
{
  for(i=0;i<n-m;i++)
  {
  T[i][m]=(h[i]*h[i]*T[i+1][m-1] - h[i+m]*h[i+m]*T[i][m-1])/(h[i]*h[i]
  - h[i+m]*h[i+m]);
  }
}
double xx=T[0][n-1];
return xx;
}

}


