C Program For Unit 3
C Program For Unit 3
UNIT 3 C PROGRAMS
Interpolation is the process of estimation of an unknown data by analyzing the given reference data.
For the given data, (say ‘y’ at various ‘x’ in tabulated form), the ‘y’ value corresponding to ‘x’ values
can be found by interpolation.
1. Start
2. Input two values of x1 & x2 between which unknown value of ‘x’ lies
3. Input f1 = f(x1) and f2 = f(x2)
4. Input the value of ‘x’ for which unknown function ‘ f ’ you want to find.
5. Compute: f = f1 + ( (f2 - f1) * (x - x1) / (x2 - x1) )
6. Output the value of ‘f ‘ as a result.
7. Stop
Below is a source code in C program for Linear Interpolation to find the square root of 2.5 from
the given data for square root of various integers.
x 1 2 3 4 5
11.
12. puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-");
13.
14. puts("Program for Linear Interpolation");
15.
16. puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
17.
18. printf("Enter the values of x1 and f(x1)\n");
19.
20. scanf("%f %f",&x1,&f1);
21.
22. puts("----------------------------------------");
23.
24. printf("Enter the values of x2 and f(x2)\n");
25.
26. scanf("%f %f",&x2,&f2);
27.
28. puts("----------------------------------------");
29.
30. printf("Enter the values of x ");
31.
32. scanf("%f",&x);
33.
34. puts("----------------------------------------");
35.
36. f=f1+((f2-f1)*(x-x1)/(x2-x1));
37.
38. printf("Correct Answer:\t%f",f);
39.
40. puts("\n--------------------------------------");
41.
42. getch();
Input/Output:
In case of equally spaced ‘x’ values, a number of interpolation methods are available such as the
Newton’s forward and backward interpolation, Gauss’s forward and backward interpolation,
Bessel’s formula, Laplace-Everett’s formula etc. But, all these methods fail when the spacing of ‘x’
is unequal. In such case, Lagrange interpolation is one of the best options.
1. Start
2. Enter the total number of given data.
3. Enter all the values of x and corresponding y in one dimensional arrays
4. Input the value of ‘x’ at which the unknown value of ‘y’ is to be interpolated.
5. Compute: prod=prod*(X-x[j])/(x[i]-x[j]) in user defined function “Li”, where “prod”
The source code given below in C program for Lagrange interpolation is for interpolating data in
which augments are unequally spaced or in cases where it is not possible to fit the curve of given
data.
******LAGRANGE INTERPOLATION*******
**********************************/
1. #include<stdio.h>
2.
3. /*Function to evaluate Li(x)*/
4.
5. double Li(int i, int n, double x[n+1], double X)
6. {
7. int j;
8. double prod=1;
9. for(j=0;j<=n;j++){
COMPILED BY: ER.SARBESH CHAUDHARY 3
C PROGRAM FOR NUMERICAL METHODS UNIT 3
10. if(j!=i)
11. prod=prod*(X-x[j])/(x[i]-x[j]);
12. }
13. return prod;
14. }
15.
16. /*Function to evaluate Pn(x) where Pn is the Lagrange
interpolating polynomial of degree n*/
17.
18. double Pn(int n, double x[n+1], double y[n+1], double X){
19. double sum=0;
20. int i;
21. for(i=0;i<=n;i++){
22. sum=sum+Li(i,n,x,X)*y[i];
23. }
24. return sum;
25. }
26. main(){
27. int i,n; //n is the degree
28. printf("**********************************\n******LAGRANGE
INTERPOLATION*******\n**********************************\n\n");
29. printf("Enter the number of data-points:\n");
30. scanf("%d",&n); //no. of data-points is n+1
31. n=n-1;
32. //Arrays to store the (n+1) x and y data-points of size n+1
33. double x[n+1];
34. double y[n+1];
35. printf("Enter the x data-points:\n");
36. for(i=0;i<n+1;i++){
37. scanf("%lf",&x[i]);
38. }
39.
40. printf("Enter the y data-points:\n");
41. for(i=0;i<n+1;i++){
42. scanf("%lf",&y[i]);
43. }
44.
45. double X; //value of x for which interpolated value is
required
46. printf("Enter the value of x for which you want the
interpolated value of y(x):\n");
47. scanf("%lf",&X);
48. printf("The interpolated value is %lf",Pn(n,x,y,X));
49. }
Input/Output:
Fitting Linear Curve using Least Square Method ALGORITHM, FLOWCHART AND
CODE IN C
Algorithm :
Step 1: Start
Step 2: Enter the value for total number of data, n.
Step 3: Input the values of x and corresponding y. In the program, x and y are defined as array.
Therefore, x and y are input using for loop.
Step 4: Calculates the sum of x, y, xy, x2 etc.
Step 5: Calculate a and b using following formula:
a=(∑y – b∑x)/n
Step 6: Output the value of a & b
Step 7: Output the required linear equation in the form y=a+bx
Step 8: Stop
Below is a source code in C program for Fitting Linear equation y=a+bx using Least Square Method
for the given data.
Velocity(m/s) 10 20 30 40 50 60 70 80
Source Code for Fitting Linear cuve using Least Square Method in C:
1. #include<stdio.h>
2. #include<conio.h>
3. #include<math.h>
4. int main()
5. {
6.
7. int n,i,x[20],y[20],sumx=0,sumy=0,sumxy=0,sumx2=0;
8. float a,b;
9. printf("\n C program for Linear Curve Fitting \n ");
10. printf("\n Enter the value of number of terms n:");
11. scanf("%d",&n);
12. printf("\n Enter the values of x:\n");
13. for(i=0;i<=n-1;i++)
14. {
15. scanf(" %d",&x[i]);
16.
17. }
18. printf("\n Enter the values of y:\n");
19. for(i=0;i<=n-1;i++)
20. {
21. scanf("%d",&y[i]);
22. }
23. for(i=0;i<=n-1;i++)
24. {
25. sumx=sumx +x[i];
26. sumx2=sumx2 +x[i]*x[i];
27. sumy=sumy +y[i];
28. sumxy=sumxy +x[i]*y[i];
29.
30. }
31. a=((sumx2*sumy -sumx*sumxy)*1.0/(n*sumx2-sumx*sumx)*1.0);
32. b=((n*sumxy-sumx*sumy)*1.0/(n*sumx2-sumx*sumx)*1.0);
33. printf("\n\n Thus a=%3.3f and b=%3.3f",a,b);
34. printf("\n\nThe required linear equation is Y=%3.3f +
%3.3f X",a,b);
35. return(0);
36. }
Input/Output:
The working principle of curve fitting C program as exponential equation is also similar to linear but
this program first converts exponential equation into linear equation by taking log on both sides
Algorithm :
Step 1: Start
Step 2: Enter the value for total number of data, n.
Step 3: Input the values of x and corresponding y. In the program, x and y are defined as array.
Therefore, x and y are input using for loop.
Step 4: Calculates the sum of x, y, logy, xlogy & x2.
Step 5: Calculate a and b for y = ae^(bx) which gives lny= bx + lna using following formula:
a=e^R
Below is a source code in C program for Fitting exponential equation y=aebx using Least Square
Method for the given data.
x 1 2 3 4
Source Code for Fitting exponential equation using Least Square Method in C:
1. #include<stdio.h>
2. #include<conio.h>
3. #include<math.h>
4. int main()
5. {
6.
7. int n,i;
8. float Y[20],sumx=0,sumy=0,sumxy=0,sumx2=0,x[20],y[20];
9. float a,b,A;
10. printf("\n C program for Exponential Curve fitting\n");
11. printf("\n Enter the value of number of terms n:");
12. scanf("%d",&n);
13. printf("\n Enter the values of x:\n");
14. for(i=0;i<=n-1;i++)
15. {
16. scanf("%f",&x[i]);
17.
18. }
19. printf("\n Enter the values of y:\n");
20. for(i=0;i<=n-1;i++)
21. {
22. scanf("%f",&y[i]);
23. }
24. for(i=0;i<=n-1;i++)
25. {
26. Y[i]=log(y[i]);
27. }
28. for(i=0;i<=n-1;i++)
29. {
30. sumx=sumx +x[i];
31. sumx2=sumx2 +x[i]*x[i];
COMPILED BY: ER.SARBESH CHAUDHARY 8
C PROGRAM FOR NUMERICAL METHODS UNIT 3
Input/Output:
THE END