Pages

Tuesday, November 18, 2014

Numerical Mathematics : LINEAR INTERPOLATION–QUADRATIC -LAGRANCE (C Programming )

Numerical Mathematics: LINEAR INTERPOLATION–QUADRATIC -LAGRANGE

Sample input

T

700

720

740

760

V

0.0977

0.12184

0.1406

0.15509

 

 #include<stdio.h>  
 int main()  
 {  
   double y[50],x[50],X,f1,f11,b2,f2,sum=0,G;  
   int n,op;  
 printf("Enter the Range :");  
 scanf("%d",&n);  
 //....................................  
 printf("ENTER x =\n");  
 for(int i=0;i<n;i++)  
 {  
   scanf("%lf",&x[i]);  
 }  
 //....................................  
 printf("ENTER y =\n");  
 for(int i=0;i<n;i++)  
 {  
   scanf("%lf",&y[i]);  
   //((y[2]-y[1])/(x[2]-x[1]));  
 }  
 printf("ENTER the value of X =\n");  
 scanf("%lf",&X);  
 //.....................................  
   for(int i=0;i<n;i++)  
   {  G=1;  
     for(int j=0;j<n;j++)  
     {  
       if(i==j)  
       {  
         continue;  
       }  
       G=G*((X-x[j])/(x[i]-x[j]));  
     }  
     sum=sum+G*y[i];  
   }  
 //.....................................  
 printf("\n\nEnter Your Option\n__________________________\n");  
 printf("1.lINEAR INTERPOLATION\n\n2.QUADRATIC\n\n3.LAGRANCE\n__________________________\n");  
 while(scanf("%d",&op)==1)  
 {  
 switch(op)  
 {  
 case 1:  
 f1=y[0]+(((y[1]-y[0])*(X-x[0]))/(x[1]-x[0]));  
 printf("LINEAR INTERPOLATION RESULT is = %lf\n\n",f1);  
   break;  
 case 2:  
   f11=y[0]+(((y[1]-y[0])*(X-x[0]))/(x[1]-x[0]));  
   b2=((((y[2]-y[1])/(x[2]-x[1]))-((y[1]-y[0])/(x[1]-x[0])))/(x[2]-x[0]));  
   f2=(f11+(b2*((X-x[0])*(X-x[1]))));  
   printf("\nQUADRATIC RESULT is = %lf\n\n",f2);  
   break;  
 case 3:  
   printf("Lagrance RESULT is = %lf \n",sum);  
   break;  
 default:  
   printf("ERORR");  
   break;  
 }  
 }  
 //.....................................  
 return 0;  
 }  


No comments:

Post a Comment