0% found this document useful (0 votes)
5 views4 pages

Program - 19

.

Uploaded by

uniyalvaishnavi6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Program - 19

.

Uploaded by

uniyalvaishnavi6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PROGRAM -19

Name: Vaishnavi
Roll no.: 68
Section: A1

Objective: Write a program in “C” Language for Parabolic Curve Fitting.

Algorithm:

1. Input:
• Read the number of data points, n.
• Read the x and y coordinates for each data point.
2. Calculate Summations:
Compute the following sums:
• ∑X , ∑X2, ∑X3, ∑X4 , ∑Y, ∑XY, ∑X2Y
3. Form the Normal Equations:

4. Solve the Normal Equations:


Use Gaussian Elimination or another method to solve for the coefficients a,
b, and c.
5. Output:
Print the equation of the best-fitting parabolic curve.
6. End.
Code:
#include <stdio.h>
int main() {
int n;
double sumX = 0, sumX2 = 0, sumX3 = 0, sumX4 = 0;
double sumY = 0, sumXY = 0, sumX2Y = 0;
double a, b, c;
printf("Enter the number of data points: ");
scanf("%d", &n);
double x[n], y[n];
printf("Enter the x and y coordinates of the data points:\n");
for (int i = 0; i < n; i++) {
printf("Data point %d (x y): ", i + 1);
scanf("%lf %lf", &x[i], &y[i]);
sumX += x[i];
sumX2 += x[i] * x[i];
sumX3 += x[i] * x[i] * x[i];
sumX4 += x[i] * x[i] * x[i] * x[i];
sumY += y[i];
sumXY += x[i] * y[i];
sumX2Y += x[i] * x[i] * y[i];
}
double A[3][4] = {
{sumX2, sumX, n, sumY},
{sumX3, sumX2, sumX, sumXY},
{sumX4, sumX3, sumX2, sumX2Y}
};
for (int i = 0; i < 3; i++) {
for (int j = i + 1; j < 3; j++) {
double ratio = A[j][i] / A[i][i];
for (int k = 0; k < 4; k++) {
A[j][k] -= ratio * A[i][k];
}
}
}
c = A[2][3] / A[2][2];
b = (A[1][3] - A[1][2] * c) / A[1][1];
a = (A[0][3] - A[0][2] * c - A[0][1] * b) / A[0][0];
printf("\nThe best-fitting parabolic curve is: y = %.4lf * x^2 + %.4lf * x +
%.4lf\n", a, b, c);

return 0;
}
Output:

You might also like