You are on page 1of 3

(Q:1) Write a C Program for finding the Least Square Line if data is input through Keyboard.

Script
1
2 /*C Program for finding the Least Square Line if data is input through Keyboard.*/
3 #include<stdio.h>
4 main()
5 {
6 int i, n;
7 float x[30], y[30], a, b;
8 float sum_x = 0.0, sum_x2 = 0.0, sum_y = 0.0, sum_xy = 0.0;
9
10 printf("\n Curve Fitting for 30 or Less Data Points.");
11 printf("\n Enter the Number of Data Points: ");
12 scanf("%d", &n);
13 printf("\n");
14 for(i=0; i<n; i++)
15 {
16 printf(" Enter the X and Y Co-ordinates of Data Point %d: ", i+1);
17 scanf("%f %f", &x[i], &y[i]);
18 sum_x = sum_x + x[i];
19 sum_x2 = sum_x2 + (x[i] * x[i]);
20 sum_y = sum_y + y[i];
21 sum_xy = sum_xy + (x[i] * y[i]);
22 }
23
24 /* Value of coefficients a and b can be found by solving a set of linear equations. */
25 a = (n * sum_xy - sum_x * sum_y)/(n * sum_x2 - sum_x * sum_x);
26 b = (sum_x2 * sum_y - sum_x * sum_xy)/(n * sum_x2 - sum_x * sum_x);
27 printf("\n\n Least Square Line: Y = (%f)X + (%f)\n\n", a, b);
28 }
29

Output
(Q:2) Write a C Program to find First, Second and Third Order Differentiation of a data set
input through keyboard.
Script
1 /* C Program to find First, Second and Third Order Differentiation of a data set
input through keyboard. */
2 #include<stdio.h>
3 main()
4 {
5 float x[30], y[30], store1, store2;
6 int i, j, n;
7
8 printf(" Enter the Number of Points: ");
9 scanf("%d", &n);
10 for(i=0 ; i<n ; i++)
11 {
12 printf(" Enter the X and Y coordinates of Point %d: ", i+1);
13 scanf("%f %f", &x[i], &y[i]);
14 }
15
16 /* We use Bubble Sort to arrange X values in ascending order. */
17 for(i=0 ; i<n ; i++)
18 {
19 for(j=0 ; j<n ; j++)
20 {
21 if(x[j]>x[i])
22 {
23 store1 = x[i];
24 store2 = y[i];
25 x[i] = x[j];
26 y[i] = y[j];
27 x[j] = store1;
28 y[j] = store2;
29 }
30 }
31 }
32 printf(" The Points in Ascending Order of X are: \n");
33 for(i=0 ; i<n ; i++)
34 {
35 printf(" \t %f \t %f \n", x[i], y[i]);
36 }
37
38 /* D_1, D_2 and D_3 are respective 1st, 2nd and 3rd order Differentials at a Point i. */
39
40 float D_1[n-1], D_2[n-2], D_3[n-3];
41
42 for(i=0 ; i<(n-1) ; i++)
43 {
44 D_1[i] = (y[i+1] - y[i])/(x[i+1] - x[i]);
45 }
46 for(i=0 ; i<(n-2) ; i++)
47 {
48 D_2[i] = (D_1[i+1] - D_1[i])/(x[i+2] - x[i]);
49 }
50 for(i=0 ; i<(n-3) ; i++)
51 {
52 D_3[i] = (D_2[i+1] - D_2[i])/(x[i+3] - x[i]);
53 }
54
55 printf(" The Respective First Derivatives at Different Values of X are: \n");
56 for(i=0 ; i<(n-1) ; i++)
57 {
58 printf("\t %f \t %f \n", x[i], D_1[i]);
59 }
60 printf("\n The Respective First Derivatives at Different Values of X are: \n");
61 for(i=0 ; i<(n-2) ; i++)
62 {
63 printf("\t %f \t %f \n", x[i], D_2[i]);
64 }
65 printf("\n The Respective First Derivatives at Different Values of X are: \n");
66 for(i=0 ; i<(n-3) ; i++)
67 {
68 printf("\t %f \t %f \n", x[i], D_3[i]);
69 }
70 }
71
Output

You might also like