Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Numerical MethodsRSS Feeds

Program of FITTING A POLYNOMIAL

Posted By: Bingham Fischer     Category: C Programming     Views: 4931

Write a program of FITTING A POLYNOMIAL.

Code for Program of FITTING A POLYNOMIAL in C Programming

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
  float x[10],y[10],sum[10],mat[3][4],temp=0,temp1=0,a1,a2,a3;
  int i,n,j;
  float fact(int);
  clrscr();
  printf("\nHow many record you will be enter: ");
  scanf("%d",&n);
  for(i=0; i<n; i++)
  {
   printf("\n\nenter the value of x%d: ",i);
   scanf("%f",&x[i]);
   temp=temp+x[i];
   printf("\n\nenter the value of y%d: ",i);
   scanf("%f",&y[i]);
   temp1=temp1+y[i];
  }
  sum[0]=temp;
  sum[1]=temp1;
  sum[2]=sum[3]=sum[4]=sum[5]=sum[6]=0;

  for(i=0;i<n;i++)
   {
    sum[2]=sum[2]+(x[i]*x[i]);
    sum[3]=sum[3]+(x[i]*x[i]*x[i]);
    sum[4]=sum[4]+(x[i]*x[i]*x[i]*x[i]);
    sum[5]=sum[5]+(x[i]*y[i]);
    sum[6]=sum[6]+(x[i]*x[i]*y[i]);
   }
   mat[0][0]=n;
   mat[0][1]=mat[1][0]=sum[0];
   mat[0][2]=mat[1][1]=mat[2][0]=sum[2];
   mat[1][2]=mat[2][1]=sum[3];
   mat[2][2]=sum[4];
   mat[0][3]=sum[1];
   mat[1][3]=sum[5];
   mat[2][3]=sum[6];

  temp=mat[1][0]/mat[0][0];
  temp1=mat[2][0]/mat[0][0];
  for(i=0,j=0;j<3+1;j++)
  {
   mat[i+1][j]=mat[i+1][j]-(mat[i][j]*temp);
   mat[i+2][j]=mat[i+2][j]-(mat[i][j]*temp1);
  }

  temp=mat[2][1]/mat[1][1];
  temp1=mat[0][1]/mat[1][1];
  for(i=1,j=0;j<3+1;j++)
  {
   mat[i+1][j]=mat[i+1][j]-(mat[i][j]*temp);
   mat[i-1][j]=mat[i-1][j]-(mat[i][j]*temp1);
  }


  temp=mat[0][2]/mat[2][2];
  temp1=mat[1][2]/mat[2][2];
  for(i=0,j=0;j<3+1;j++)
  {
   mat[i][j]=mat[i][j]-(mat[i+2][j]*temp);
   mat[i+1][j]=mat[i+1][j]-(mat[i+2][j]*temp1);
  }

  for(i=0;i<3;i++)
  {
   for(j=0;j<4;j++)
   {
    printf(" %.3f ",mat[i][j]);
   }
   printf("\n\n");
  }

  a3 = mat[2][3]/mat[2][2];
  a2 = mat[1][3]/mat[1][1];
  a1 = mat[0][3]/mat[0][0];
  printf("\n\nx = %.3f",a1);
  printf("\n\ny = %.3f",a2);
  printf("\n\nz = %.3f",a3);

  getch();
}

/*
______________________________________

OUT PUT
______________________________________

How many record you will be enter: 6


enter the value of x0: 1.0


enter the value of y0: 1.1


enter the value of x1: 1.5


enter the value of y1: 1.3


enter the value of x2: 2.0


enter the value of y2: 1.6


enter the value of x3: 2.5


enter the value of y3: 2.0


enter the value of x4: 3.1


enter the value of y4: 3.4


enter the value of x5: 4


enter the value of y5: 4.2

6.000 0.000 0.000 4.065

0.000 5.975 0.000 0.816

0.000 0.000 5.095 0.991



x = 0.678

y = 0.137

z = 0.195


*/
  
Share: 


Didn't find what you were looking for? Find more on Program of FITTING A POLYNOMIAL Or get search suggestion and latest updates.

Bingham Fischer
Bingham Fischer author of Program of FITTING A POLYNOMIAL is from Frankfurt, Germany.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!