0% found this document useful (0 votes)
3 views3 pages

Program MMM

The document presents two C programs for numerical methods: Newton Backward Interpolation and Curve Fitting using Least Squares. The first program calculates interpolated values using Newton's backward formula, while the second program fits a linear equation to a set of data points by minimizing vertical deviations. Each program includes explanations of the concepts, formulas, and the corresponding C code.

Uploaded by

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

Program MMM

The document presents two C programs for numerical methods: Newton Backward Interpolation and Curve Fitting using Least Squares. The first program calculates interpolated values using Newton's backward formula, while the second program fits a linear equation to a set of data points by minimizing vertical deviations. Each program includes explanations of the concepts, formulas, and the corresponding C code.

Uploaded by

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

Here are 2 complete C programs with their explanations for numerical methods:

1. Newton Backward Interpolation


📌 Concept:

Newton’s backward interpolation formula is used when the given value lies near the end of the
data table.

🔹 Formula:

f(x)=yn+u∇yn+u(u+1)2!∇2yn+⋯f(x) = y_n + u \nabla y_n + \frac{u(u+1)}{2!} \nabla^2 y_n


+ \cdots

Where:

∇\nabla = backward difference operator


 u=x−xnhu = \frac{x - x_n}{h}

 hh = constant spacing of x values

🔹 C Program:
#include <stdio.h>

int factorial(int n) {
int f = 1;
for (int i = 2; i <= n; i++)
f *= i;
return f;
}

int main() {
float x[10], y[10][10], xp, h, u, yp;
int n;

printf("Enter number of data points: ");


scanf("%d", &n);

printf("Enter x values:\n");
for (int i = 0; i < n; i++)
scanf("%f", &x[i]);

printf("Enter y values:\n");
for (int i = 0; i < n; i++)
scanf("%f", &y[i][0]);
// Constructing backward difference table
for (int j = 1; j < n; j++)
for (int i = n - 1; i >= j; i--)
y[i][j] = y[i][j - 1] - y[i - 1][j - 1];

printf("Enter interpolation point: ");


scanf("%f", &xp);

h = x[1] - x[0];
u = (xp - x[n - 1]) / h;
yp = y[n - 1][0];

float u_term = 1;
for (int i = 1; i < n; i++) {
u_term *= (u + i - 1);
yp += (u_term * y[n - 1][i]) / factorial(i);
}

printf("Interpolated value at x = %.2f is %.4f\n", xp, yp);


return 0;
}

2. Curve Fitting – Least Squares (Linear Fit)


📌 Concept:

Fit a line of the form y=a+bxy = a + bx through a set of points such that the sum of the squares of
the vertical deviations is minimized.

🔹 Formulas:

b=n∑xy−∑x∑yn∑x2−(∑x)2,a=∑y−b∑xnb = \frac{n \sum xy - \sum x \sum y}{n \sum x^2 - (\


sum x)^2}, \quad a = \frac{\sum y - b \sum x}{n}

🔹 C Program:
#include <stdio.h>

int main() {
float x[10], y[10], sumX = 0, sumY = 0, sumXY = 0, sumX2 = 0;
float a, b;
int n;

printf("Enter number of data points: ");


scanf("%d", &n);

printf("Enter x and y values:\n");


for (int i = 0; i < n; i++) {
scanf("%f%f", &x[i], &y[i]);
sumX += x[i];
sumY += y[i];
sumXY += x[i] * y[i];
sumX2 += x[i] * x[i];
}

b = (n * sumXY - sumX * sumY) / (n * sumX2 - sumX * sumX);


a = (sumY - b * sumX) / n;

printf("Fitted line: y = %.2f + %.2fx\n", a, b);


return 0;
}

Would you like the next 2 programs now (e.g., Trapezoidal Rule and Simpson’s Rule), or should
I generate a Word file with these?

You might also like