Friday, July 6, 2012

SOME IMPORTANT NUMERICAL METHODS CODE IN JAVA




class Processor
{
double method(double x)
 {
   double result=((x*x)-(5*x)+4);
   return result;
 }
 double error(double x1,double x2)
 {
  double result=(((x2-x1)/x2)*100);
   double error= Math.abs(result);
   return error;
   }
  double middle(double a,double b )
   {  
  double mid=((a+b)/2);
   return mid;
  }
}
{
 public static void main(String args[])
 {   
  Processor  p=new Processor();  
  double Xr_old=2;
   double Xu_old=8;
   double x1=Xr_old;
   double x2=Xu_old;
   double f1=p.method(x1);
  System.out.println("Function for Lower Limit :"+f1);
  double f2=p.method(x2);
  System.out.println("Function for Lower Limit :"+f2);
  System.out.println("\n");
  if(f1*f2==0)
  {
  if(f1==0)
  System.out.println("Root is :"+x1);
  else
  System.out.println("Root is :"+x2);
  }
 else if(f1*f2<0)  
 {
 for(int i=0;i<4;i++)
 {   
 System.out.println("Lower limit is :"+x1);
 System.out.println("Upper limit is :"+x2);
 f1=p.method(x1);
 f2=p.method(x2);
 System.out.println("f1="+f1+"\nf2="+f2);
 double Xr_new=p.middle(x1, x2);
 System.out.println("The root="+Xr_new);
 x2=Xr_new;
 double error=p.error(Xr_old,Xr_new);
 System.out.println("Error is :"+error);

 }
 }
  else if(f1*f2>0)  
 {
 for(int i=0;i<4;i++)
 {   
 System.out.println("Lower limit is :"+x1);
 System.out.println("Upper limit is :"+x2);
 f1=p.method(x1);
 f2=p.method(x2);
 System.out.println("f1="+f1+"\nf2="+f2);
 double Xr_new=p.middle(x1, x2);
 System.out.println("The root="+Xr_new);
 x1=Xr_new;
 double error=p.error(Xr_old,Xr_new);
 System.out.println("Error is :"+error);
 System.out.println("Xr is :"+Xr_new);
 double fr=p.method(Xr_new);
 System.out.println("fr:"+fr);
 }
  }
 }
 }


class method
{
    double equation(double x)
    {
        double equ=x*x-x-2;
        return equ;
    }
 }
 class falsepositionmethod
{
 public static void main(String args[])
  { 
     method m= new method();
     double x1=1;
     double x2=3;
     for(int i=0;i<3;i++)
     {
     double fx1=m.equation(x1);
     double fx2=m.equation(x2);
    System.out.println("f(x1)="+fx1);
    System.out.println("f(x2)="+fx2);
     double x0=(x1)-((fx1)*((x2-x1)/fx2-fx1));
    System.out.println("X0="+x0);
    double fx=m.equation(x0);
    double f=(fx*fx1);
    System.out.println("ITERATION="+f);
    x1=x0;
     }
 }
}



public class gausselemination
{
public static void main(String args[])
{
double c,u;
double a[][]=new double[4][4];
double b[]=new double[4];
double s[][]=new double[4][4];
double n[][]=new double[4][4];
double d[]=new double[4];
double m[]=new double[4];
a[1][1]=2;            a[1][2]=1;            a[1][3]=1;            b[1]=5;
a[2][1]=3;            a[2][2]=5;            a[2][3]=2;            b[2]=15;
a[3][1]=2;            a[3][2]=1;            a[3][3]=4;            b[3]=8;
c=a[1][1];
for(int i=1;i<3;i++)
               {
if(a[i][1]<a[i+1][1])

                                             c=a[i+1][1];
                              }
               }
               int p=1;
                              u=0;
               for(int i=1;i<4;i++)
               {
                              if(c==a[i][1])
                              {
                                             p=i;
                                             d[1]=a[p][1];      d[2]=a[p][2];      d[3]=a[p][3];      u=b[p];
                                             break;
                              }
               }
               if(p!=1)
               {
                              for(int i=1;i<4;i++)
               {
                                             if(p==i)
                                             {
                                                            for(int j=1;j<4;j++)
                                                                           a[p][j]=a[1][j];
                                             b[p]=b[1];
                                             break;
                                             }
                              }
                              a[1][1]=d[1];      a[1][2]=d[2];      a[1][3]=d[3];      b[1]=u;
               }
               System.out.println();
               for(int i=1;i<4;i++)
               {
                              for(int j=1;j<4;j++)
                              {
                                             System.out.print("               a["+i);System.out.print("]["+j);System.out.print("]="+a[i][j]);
                              }             System.out.print("               b["+i);System.out.print("]="+b[i]);System.out.println();
               }
               double a220,a230,a320,a330,a3300,x3,x2,x1,b20,b30,b300;
               a220=(a[1][2]*a[2][1]/a[1][1])-a[2][2];
               a230=(a[1][3]*a[2][1]/a[1][1])-a[2][3];
               b20=(b[1]*a[2][1]/a[1][1])-b[2];
               a320=(a[1][2]*a[3][1]/a[1][1])-a[3][2];
               a330=(a[1][3]*a[3][1]/a[1][1])-a[3][3];
               b30=(b[1]*a[3][1]/a[1][1])-b[3];
               a3300=(a230*a320/a220)-a330;
               b300=(b20*a320/a220)-b30;
               x3=b300/a3300;
               x2=(b20-(a230*x3))/a220;
               x1=(b[1]-a[1][2]*x2-a[1][3]*x3)/a[1][1];
              System.out.println();
               System.out.println("         X1== "+x1);       System.out.println();
               System.out.println("         X2== "+x2);       System.out.println();
               System.out.println("         X3== "+x3);       System.out.println();
}
}





1 comment: