Showing posts with label Numerical Methods. Show all posts
Showing posts with label Numerical Methods. Show all posts

Tuesday, February 01, 2011

Curvefitting


Source Code:
/*      The following C program implements Curve Fitting by the Method of Least
Squares for some given tabular values. The tabular values (abscissa and
ordinate) are read as input.         */
 
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define max 10
#define minvalue 0.0005

void main()
{
 int degree, count, i,j,k, noofequations, temp;
 float xtable[max][max], ytable[max][max], augmentedmatrix[max][max], temporary, r, solutions[max];

 printf("\n How many points are to be entered:-");
 scanf("%d",&count);

 printf("\n Enter x & y values in tabular format:-\n");
 for(i=0; i<count; i++)
  for(j=0; j<=1; j++)
            if(j%2==0)
             scanf("%f",&xtable[i][1]);
   else
             scanf("%f",&ytable[i][0]);


 printf("\n Degree of polynomial that is to be fitted:-  ");
 scanf("%d",&degree);

/* Constructing table with powers of x */

 for(i=0; i<count; i++)
  for(j=2; j<=2*degree; j++)
   xtable[i][j]=pow(xtable[i][1],j);

/* Constructing table with combined terms of x and y */

 for(i=0; i<count; i++)
   
for(j=1; j<=degree; j++)
   ytable[i][j]=ytable[i][0]*xtable[i][j];

/* Calculating sum */

 for(j=0;j<=2*degree;j++)
 {
  xtable[count][j]=ytable[count][j]=0;
  for(i=0;i<count;i++)
  {
  if(j!=0)
            xtable[count][j]+=xtable[i][j];
  if(j<=degree)
   ytable[count][j]+=ytable[i][j];
  }
 }

/* printing tables */

 printf("\n\nTABLE WITH POWERS OF X \n");

 for(i=0;i<count;i++)
 {
  printf("\n ");

  for(j=1;j<=2*degree;j++)
   printf("%.3f  ",xtable[i][j]);
 }

 printf("\n\n        TABLE WITH X-Y TERMS \n");

 for(i=0;i<count;i++)
 {
  printf("\n ");

  for(j=0;j<=degree;j++)
   printf("%.3f  ",ytable[i][j]);
 }

 /* Using GAUSS-JORDAN METHOD OF ELIMINATION
      to find solutions of simultaneous equations */

 noofequations=degree+1;
 xtable[count][0]=count;


/* storing augmented co-efficient matrix as a matrix of dimension
   (noofequations)x(noofequations+1) in 2D array */

 for(i=0;i<noofequations;i++)
 {
  for(j=i,k=0;j<i+noofequations;j++)
   augmentedmatrix[i][k++]=xtable[count][j];

  augmentedmatrix[i][k]=ytable[count][i];
 }

 /* converting augmented matrix into upper triangular form */

 for(j=0; j<noofequations; j++)
 {
  temp=j;

 /* finding maximum coefficient of Xj in last (noofequations-j) equations */

  for(i=j+1; i<noofequations; i++)
            if(augmentedmatrix[i][j]>augmentedmatrix[temp][j])
                        temp=i;

  if(fabs(augmentedmatrix[temp][j])<minvalue)
             {
              printf("\n Coefficients are too small to deal with !!!");
              goto end;
             }

 /* swapping row which has maximum coefficient of Xj */

  if(temp!=j)
            for(k=0; k<=noofequations; k++)
            {
            temporary=augmentedmatrix[j][k] ;
            augmentedmatrix[j][k]=augmentedmatrix[temp][k] ;
            augmentedmatrix[temp][k]=temporary ;
            }

/* performing row operations to form required diagonal matrix */

  for(i=0; i<noofequations; i++)
            if(i!=j)
            {
            r=augmentedmatrix[i][j];


for(k=0; k<=noofequations; k++)
              augmentedmatrix[i][k]-=(augmentedmatrix[j][k]/augmentedmatrix[j][j])*r ;
            }
 }

  /* storing solutions */

 for(i=0;i<noofequations; i++)
  solutions[i]=augmentedmatrix[i][noofequations]/augmentedmatrix[i][i] ;

 /* printing solutions */

 printf("\n\n\n Polynomial to be fitted is :- \n");

 for(i=0; i<noofequations; i++)
  if(i>1)
            printf("(%f)*x^%d + ",solutions[i],i);
  else if(i==1)
            printf("(%f)*x + ",solutions[1]);
  else
            printf(" %f + ",solutions[0]);

 end: getch() ;

}

Output:
Fitting the following tabular values by a 2nd degree polynomial:

x
-2
-1
0
1
2
y
-9
-4
1
6
10


Matrix Inversion by Gauss Jordan


/*      The following C program implements Gauss-Jordan Elimination method for
         finding the inverse of a non-singular matrix. The matrix is read as input.        */
#include<stdio.h>
#include<math.h>
#include<conio.h>
#define maximum 10
#define minvalue 0.0005

void main()
{
 float augmentedmatrix[maximum][2*maximum] ;  
                                                                /* 2D array declared to store augmented matrix */
 float temporary, r ;
 int i, j, k, dimension, temp;                                  /* declaring counter variables for loops */

 clrscr();

 printf("\n  INVERSE OF NON-SINGULAR MATRIX BY GAUSS-JORDAN
                  ELIMINATION  METHOD");

 printf("\n Enter the dimension of the matrix to be provided as input : \n");
 scanf("%d",&dimension);

 /*   storing augmented matrix as a matrix of dimension
      (dimension)x(2*dimension) in 2D array  */

 printf("\n Enter a non-singular %dx%d matrix : \n",dimension,dimension);
 for(i=0; i<dimension; i++)
  for(j=0; j<dimension; j++)
            scanf("%f",&augmentedmatrix[i][j]) ;

 /* augmenting with identity matrix of similar dimensions */

 for(i=0;i<dimension; i++)
  for(j=dimension; j<2*dimension; j++)
      if(i==j%dimension)
         augmentedmatrix[i][j]=1;
      else
         augmentedmatrix[i][j]=0;

 /* using gauss-jordan elimination */

 for(j=0; j<dimension; j++)
 {
  temp=j;

 /* finding maximum jth column element in last (dimension-j) rows */

  for(i=j+1; i<dimension; i++)
if(augmentedmatrix[i][j]>augmentedmatrix[temp][j])
                        temp=i;

  if(fabs(augmentedmatrix[temp][j])<minvalue)
             {
                printf("\n Elements are too small to deal with !!!");
                goto end;
             }

 /* swapping row which has maximum jth column element */

  if(temp!=j)
            for(k=0; k<2*dimension; k++)
            {
            temporary=augmentedmatrix[j][k] ;
            augmentedmatrix[j][k]=augmentedmatrix[temp][k] ;
            augmentedmatrix[temp][k]=temporary ;
            }

/* performing row operations to form required identity matrix out of the input matrix */

  for(i=0; i<dimension; i++)
            if(i!=j)
            {
            r=augmentedmatrix[i][j];
            for(k=0; k<2*dimension; k++)
              augmentedmatrix[i][k]-=(augmentedmatrix[j][k]/augmentedmatrix[j][j])*r ;
            }
            else
            {
            r=augmentedmatrix[i][j];
            for(k=0; k<2*dimension; k++)
              augmentedmatrix[i][k]/=r ;
            }

 }
          
 /* Display augmented matrix */

 printf("\n After Gauss-Jordan elimination, augmented matrix is : \n\n") ;

 for(i=0; i<dimension; i++)
 {
  for(j=0; j<2*dimension; j++)
            printf("  %4.2f",augmentedmatrix[i][j]) ;
  printf("\n");
 }


 /* displaying inverse of the non-singular matrix */

 printf("\n\n\n The inverse of the entered non-singular matrix is : \n\n");

 for(i=0; i<dimension; i++)
 {
  for(j=dimension; j<2*dimension; j++)
            printf("  %.5f",augmentedmatrix[i][j]);
  printf("\n");
 }
 end: getch() ;
 }

Output:


Gauss Central Difference formula



/*      The following C program implements Gauss Central Difference Formula in
         estimating functional values near the middle part of a given table. The tabular
         values (abscissa and ordinate) are read as input.         */
  
#include<stdio.h>
#include<conio.h>
#define max 30

void main()
{
 float table[max][max];
 int count, mid, i, j;
 float x, n, h, y, p ;

 /* Setting all the contents of the table to zero */

 for(i=0; i<max; i++)
 for(j=0; j<max; j++)
  table[i][j]=0;

 printf("\n How many values are to be entered? \n");
 scanf("%d",&count);

 printf("\n Enter the values of x and y in tabular format: \n\n");

 for(i=0; i<=2*count-2; i+=2)
 for(j=0; j<=1; j++)
 scanf("%f",&table[i][j]) ;

 printf("\n Find the value of y at x = ? \n");
 scanf("%f",&x);

 /* constructing the central difference table */

 for(i=2; i<=count; i++)
 for(j=i-1, n=1; n<=count+1-i; j+=2, n++)
 {
  table[j][i]=table[j+1][i-1] - table[j-1][i-1];
 }

 
/* Finding the middle of the x column of the table */

 if(count%2==1)
            mid=count-1;
 else
mid=count;

 h=table[mid+2][0] - table[mid][0] ;
 n=p=(x-table[mid][0])/h;

 /* Using interpolation formula */

 if(x>table[mid][0])
 {
  printf("\n APPLYING GAUSS FORWARD CENTRAL
                  DIFFERENCE FORMULA   \n");

  y=table[mid][1] + n*table[mid+1][2];

  for(i=3; i<=count; i++)
            if(i%2==1)
              {
              n*=(p-(int)i/2)/(i-1);
              y+=n*table[mid][i];
              }
            else
              {
              n*=(p+(int)i/2 -1)/(i-1);
              y+=n*table[mid+1][i];
              }
 }
 else
 {
  printf("\n------ APPLYING GAUSS BACKWARD CENTRAL DIFFERENCE FORMULA ------\n");

  y=table[mid][1] + n*table[mid-1][2];

  for(i=3; i<=count; i++)
            if(i%2==1)
              {
              n*=(p+(int)i/2)/(i-1);
              y+=n*table[mid][i];
              }
            else
                                                                                                                                                                                              {
              n*=(p-(int)i/2 +1)/(i-1);
              y+=n*table[mid-1][i];
              }
 }

 /* printing the central difference table */

 printf("\n CENTRAL DIFFERENCE TABLE   \n");

 for(i=0; i<=2*count-2; i++)
 {
  if(i%2==0)
            printf("%2.3f %2.3f",table[i][0], table[i][1]);
  else
            printf("           ");

  for(j=2; j<=count; j++)
            if((j%2==0 && i%2==1) || (j%2==1 && i%2==0))
            {
             if(i>=j-1 && i<2*count-j)
                         printf(" %2.3f",table[i][j]);
            }
            else
             printf("     ");

  printf("\n");

 }

  printf("\n\n At x=%f,  y=%f \n",x,y);

  getch();

}

 
SAMPLE OUTPUT:

Evaluating the value of y at x = 0.425, given the following table of values:


x
0.0
0.2
0.4
0.6
0.8
y
0.0000
0.1823
0.3365
0.4700
0.5878


Gauss-Jordan Elimination Method


/*      The following C program implements Gauss-Jordan Elimination method for
         finding the solution of a set of linear simultaneous equations. The number of
        equations and the coefficient matrix are read as input. The determinant of the
        coefficient matrix is also evaluated. The results are printed properly.        */

#include<stdio.h>
#include<math.h>
#include<conio.h>
#define maximum 10
#define minvalue 0.0005

void main()
{
 float augmentedmatrix[maximum][maximum] ;  
        /* 2D array declared to store augmented co-efficient matrix */
 float temporary, r ;
 float solutions[maximum] ;  
                              /* 1D array declared to store solutions of the simultaneous equations */

 int i,j,k, noofequations, temp, minus=0;             /* declaring counter variables */

 clrscr();

 printf("\n===============================================");
 printf("\n        GAUSS-JORDAN  ELIMINATION  METHOD");
 printf("\n===============================================");

 printf("\n Enter the number of total equations to be provided as input : \n");
 scanf("%d",&noofequations);

 /* storing augmented co-efficient matrix as a matrix of dimension (noofequations)x(noofequations+1) in 2D array */

 printf("\n Enter the augmented co-efficient matrix : \n");
 for(i=0; i<noofequations; i++)
  for(j=0; j<=noofequations; j++)
            scanf("%f",&augmentedmatrix[i][j]) ;


 /* converting augmented matrix into matrix of diagonal form */

 for(j=0; j<noofequations; j++)
 {
  temp=j;

 /* finding maximum coefficient of Xj in last (noofequations-j) equations */

  for(i=j+1; i<noofequations; i++)
            if(augmentedmatrix[i][j]>augmentedmatrix[temp][j])
                        temp=i;

      if(fabs(augmentedmatrix[temp][j])<minvalue)
        {
              printf("\n Coefficients are too small to deal with !!!");
              goto end;
        }

 /* swapping row which has maximum coefficient of Xj */

  if(temp!=j)
  {
        minus++;
            for(k=0; k<=noofequations; k++)
            {
            temporary=augmentedmatrix[j][k] ;
            augmentedmatrix[j][k]=augmentedmatrix[temp][k] ;
            augmentedmatrix[temp][k]=temporary ;
            }
  }

/* performing row operations to form required diagonal matrix */

  for(i=0; i<noofequations; i++)
            if(i!=j)
            {
            r=augmentedmatrix[i][j];
            for(k=0; k<=noofequations; k++)
              augmentedmatrix[i][k]-=(augmentedmatrix[j][k]/augmentedmatrix[j][j])*r ;
            }
 }
 
 /* Display augmented coefficient matrix */

 printf("\n After diagonalization of entered augmented coefficient matrix : \n\n") ;
 for(i=0; i<noofequations; i++)
 {
  for(j=0; j<=noofequations; j++)
            printf("  %4.2f",augmentedmatrix[i][j]) ;
  printf("\n");
 }

 /* finding and displaying determinant of coefficient matrix */

 temporary=1.0 ;
 for(i=0; i<noofequations; i++)
            temporary*=augmentedmatrix[i][i] ;        /* multiplying diagonal elements only */

 if(minus%2==0)
    printf("\n Determinant of coefficient matrix = %.2f", temporary) ;
 else
    printf("\n Determinant of coefficient matrix = %.2f", -temporary) ;

  /* storing solutions */

 for(i=0;i<noofequations; i++)
  solutions[i]=augmentedmatrix[i][noofequations]/augmentedmatrix[i][i] ;

 /* printing solutions */

 printf("\n\n\n");

 for(i=0; i<noofequations; i++)
  printf("  X%d  =  %4.2f \n", i, solutions[i]) ;

 end: getch() ;

 }

SAMPLE OUTPUT:
Solving the following set of linear simultaneous equations by Gauss-Jordan elimination:

   x0 + 5x1 + 3x2 = 10
x0 + 3x1 + 2x2 = 5
2x0 + 4x1 – 6x2 = -4