0% found this document useful (0 votes)
10 views17 pages

NumericalMethods LabReport

This lab report covers various numerical methods for finding roots and interpolating polynomials, including the Bisection Method, False Position Method, Secant Method, Newton-Raphson Method, and Fixed Point Iteration Method. Each method is explained with its theory, code implementation, and the equations solved. Additionally, it includes methods for polynomial evaluation using Horner's Method and interpolation using Lagrange and Newton's Divided Difference methods.

Uploaded by

ujwalghimire97
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)
10 views17 pages

NumericalMethods LabReport

This lab report covers various numerical methods for finding roots and interpolating polynomials, including the Bisection Method, False Position Method, Secant Method, Newton-Raphson Method, and Fixed Point Iteration Method. Each method is explained with its theory, code implementation, and the equations solved. Additionally, it includes methods for polynomial evaluation using Horner's Method and interpolation using Lagrange and Newton's Divided Difference methods.

Uploaded by

ujwalghimire97
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

LAB REPORT

Numerical Methods

Subject: Numerical Methods


Department: [Your Department]
College: [Your College Name]

Name: [Your Name]


Roll No.: [Your Roll No.]
Section: [Your Section]
Submission Date: [Date]
Index Page – List of Lab Works (Numerical Methods)
Lab No. Title / Question Submission Signature
Date
1(a) Bisection Method

1(b) False Position Method

1(c) Secant Method

1(d) Newton Raphson Method

1(e) Fixed Point Iteration Method

2 Horner's Method

3(a) Lagrange Interpolation

3(b) Newton's Divided Difference Interpolation

3(c) Newton's Forward Difference Interpolation

3(d) Newton's Backward Difference Interpolation

3(e) Cubic Spline Interpolation

4(a) Linear Least Square (Linear Regression)

4(b) Nonlinear Regression – Exponential Model

4(c) Nonlinear Regression – Polynomial Model


Lab No. 1(a): Bisection Method
Theory:
The Bisection Method is a bracketing method for finding roots of a nonlinear equation f(x) = 0. It
requires two initial guesses 'a' and 'b' such that f(a) * f(b) < 0 (i.e., the root lies between them).
The method repeatedly bisects the interval and selects the subinterval where the sign change
occurs, guaranteeing convergence. The midpoint c = (a + b) / 2 is computed each iteration. The
process continues until the interval width or |f(c)| is within the given error tolerance. Equation
solved: x² - 4cos(x) = 0.
Code:
#include <stdio.h>
#include <math.h>

double f(double x) {
return x*x - 4*cos(x);
}

int main() {
double a, b, tol, c, fa, fb, fc;
int iter = 0;
printf("Enter interval [a, b] and tolerance: ");
scanf("%lf %lf %lf", &a, &b, &tol);
fa = f(a); fb = f(b);
if (fa * fb > 0) {
printf("No root in this interval.\n");
return 1;
}
printf("\nIter\t a\t\t b\t\t c\t\tf(c)\n");
printf("--------------------------------------------------------------\n");
do {
c = (a + b) / 2.0;
fc = f(c);
iter++;
printf("%d\t%.6lf\t%.6lf\t%.6lf\t%.6lf\n", iter, a, b, c, fc);
if (fc == 0) break;
if (fa * fc < 0) { b = c; fb = fc; }
else { a = c; fa = fc; }
} while (fabs(b - a) >= tol);
printf("\nRoot = %.6lf (after %d iterations)\n", c, iter);
printf("\n------------------------------\n");
printf("Lab No.:1(a)/Name: [Your Name]/Roll No.: [No.]/Section:[X]\n");
printf("------------------------------\n");
return 0;
}

Output:
[Attach screenshot of program output here]
Lab No. 1(b): False Position Method
Theory:
The False Position Method (Regula Falsi) is a bracketing root-finding technique. Like the Bisection
Method, it requires two initial guesses where the function changes sign. Instead of bisecting, it
uses linear interpolation to compute the new estimate: c = (a*f(b) - b*f(a)) / (f(b) - f(a)). This often
converges faster than bisection. The bracket is then updated based on the sign of f(c). The
iteration continues until |f(c)| is within the error tolerance. Equation solved: x³ - 3x + 1 = 0.
Code:
#include <stdio.h>
#include <math.h>

double f(double x) {
return x*x*x - 3*x + 1;
}

int main() {
double a, b, tol, c, fa, fb, fc;
int iter = 0;
printf("Enter interval [a, b] and tolerance: ");
scanf("%lf %lf %lf", &a, &b, &tol);
fa = f(a); fb = f(b);
if (fa * fb > 0) {
printf("No root in this interval.\n");
return 1;
}
printf("\nIter\t a\t\t b\t\t c\t\tf(c)\n");
printf("--------------------------------------------------------------\n");
do {
c = (a*fb - b*fa) / (fb - fa);
fc = f(c);
iter++;
printf("%d\t%.6lf\t%.6lf\t%.6lf\t%.6lf\n", iter, a, b, c, fc);
if (fc == 0) break;
if (fa * fc < 0) { b = c; fb = fc; }
else { a = c; fa = fc; }
} while (fabs(fc) >= tol);
printf("\nRoot = %.6lf (after %d iterations)\n", c, iter);
printf("\n------------------------------\n");
printf("Lab No.:1(b)/Name: [Your Name]/Roll No.: [No.]/Section:[X]\n");
printf("------------------------------\n");
return 0;
}

Output:
[Attach screenshot of program output here]
Lab No. 1(c): Secant Method
Theory:
The Secant Method is an open root-finding method that uses two initial guesses x₀ and x₁ (not
necessarily bracketing the root). It approximates the derivative using a finite difference, computing
the next estimate as: x₂ = x₁ - f(x₁) * (x₁ - x₀) / (f(x₁) - f(x₀)). It converges faster than the
Bisection Method but may not always converge. No derivative computation is needed. Equation
solved: e^(-x) - 3x = 0.
Code:
#include <stdio.h>
#include <math.h>

double f(double x) {
return exp(-x) - 3*x;
}

int main() {
double x0, x1, x2, tol, fx0, fx1, fx2;
int iter = 0;
printf("Enter two initial guesses x0, x1 and tolerance: ");
scanf("%lf %lf %lf", &x0, &x1, &tol);
printf("\nIter\t x0\t\t x1\t\t x2\t\tf(x2)\n");
printf("--------------------------------------------------------------\n");
do {
fx0 = f(x0); fx1 = f(x1);
x2 = x1 - fx1*(x1 - x0)/(fx1 - fx0);
fx2 = f(x2);
iter++;
printf("%d\t%.6lf\t%.6lf\t%.6lf\t%.6lf\n", iter, x0, x1, x2, fx2);
x0 = x1;
x1 = x2;
} while (fabs(fx2) >= tol);
printf("\nRoot = %.6lf (after %d iterations)\n", x2, iter);
printf("\n------------------------------\n");
printf("Lab No.:1(c)/Name: [Your Name]/Roll No.: [No.]/Section:[X]\n");
printf("------------------------------\n");
return 0;
}

Output:
[Attach screenshot of program output here]
Lab No. 1(d): Newton-Raphson Method
Theory:
The Newton-Raphson Method is a powerful open root-finding method. Starting from an initial
guess x₀, it uses the tangent line to the curve to find successively better approximations: x₁ = x₀ -
f(x₀) / f'(x₀). It requires the derivative f'(x) of the function. It converges quadratically when the
initial guess is close to the root. If f'(x) is close to zero, the method may fail. Equation solved:
cos(x) - x*e^x = 0. Derivative: f'(x) = -sin(x) - e^x - x*e^x.
Code:
#include <stdio.h>
#include <math.h>

double f(double x) {
return cos(x) - x*exp(x);
}

double df(double x) {
return -sin(x) - exp(x) - x*exp(x);
}

int main() {
double x, tol, fx, dfx, x1;
int iter = 0;
printf("Enter initial guess and tolerance: ");
scanf("%lf %lf", &x, &tol);
printf("\nIter\t x\t\tf(x)\t\tf'(x)\n");
printf("----------------------------------------------\n");
do {
fx = f(x); dfx = df(x);
x1 = x - fx/dfx;
iter++;
printf("%d\t%.6lf\t%.6lf\t%.6lf\n", iter, x, fx, dfx);
x = x1;
} while (fabs(f(x)) >= tol);
printf("\nRoot = %.6lf (after %d iterations)\n", x, iter);
printf("\n------------------------------\n");
printf("Lab No.:1(d)/Name: [Your Name]/Roll No.: [No.]/Section:[X]\n");
printf("------------------------------\n");
return 0;
}

Output:
[Attach screenshot of program output here]
Lab No. 1(e): Fixed Point Iteration Method
Theory:
Fixed Point Iteration solves f(x) = 0 by rearranging it into the form x = g(x). Starting from an initial
guess x₀, the iteration is: x₁ = g(x₀). Convergence is guaranteed when |g'(x)| < 1 near the root.
The equation 1 + (1/2)sin(x) - x = 0 is rearranged to x = 1 + (1/2)sin(x), which satisfies the
convergence criterion since the derivative of the right side, (1/2)cos(x), has magnitude at most 0.5
< 1.
Code:
#include <stdio.h>
#include <math.h>

/* Rearranged: x = 1 + (1/2)*sin(x) */
double g(double x) {
return 1.0 + 0.5*sin(x);
}

double f(double x) {
return 1 + 0.5*sin(x) - x;
}

int main() {
double x0, x1, tol;
int iter = 0;
printf("Enter initial guess and tolerance: ");
scanf("%lf %lf", &x0, &tol);
printf("\nIter\t x0\t\t x1\t\tf(x1)\n");
printf("----------------------------------------------\n");
do {
x1 = g(x0);
iter++;
printf("%d\t%.6lf\t%.6lf\t%.6lf\n", iter, x0, x1, f(x1));
x0 = x1;
} while (fabs(f(x1)) >= tol);
printf("\nRoot = %.6lf (after %d iterations)\n", x1, iter);
printf("\n------------------------------\n");
printf("Lab No.:1(e)/Name: [Your Name]/Roll No.: [No.]/Section:[X]\n");
printf("------------------------------\n");
return 0;
}

Output:
[Attach screenshot of program output here]
Lab No. 2: Horner's Method (Polynomial Evaluation)
Theory:
Horner's Method efficiently evaluates a polynomial P(x) = aₙxⁿ + a ₙ₋₁xⁿ ⁻¹ + ... + a₁x + a₀ by
rewriting it in nested form: P(x) = (...((aₙx + aₙ₋₁)x + aₙ₋₂)x + ...)x + a₀. This reduces the number
of multiplications from O(n²) to O(n), making it highly efficient. The coefficients are entered from
the highest degree to the lowest.
Code:
#include <stdio.h>

int main() {
int n, i;
double coef[20], x, result;
printf("Enter degree of polynomial: ");
scanf("%d", &n);
printf("Enter %d coefficients (highest degree first): ", n+1);
for (i = 0; i <= n; i++) scanf("%lf", &coef[i]);
printf("Enter value of x: ");
scanf("%lf", &x);
result = coef[0];
for (i = 1; i <= n; i++) {
result = result * x + coef[i];
}
printf("\nP(%.4lf) = %.6lf\n", x, result);
printf("\n------------------------------\n");
printf("Lab No.:2/Name: [Your Name]/Roll No.: [No.]/Section:[X]\n");
printf("------------------------------\n");
return 0;
}

Output:
[Attach screenshot of program output here]
Lab No. 3(a): Lagrange Interpolation
Theory:
Lagrange Interpolation finds a polynomial that passes through a given set of data points (xᵢ, yᵢ).
The interpolating polynomial is: P(x) = Σ yᵢ * Lᵢ(x), where Lᵢ(x) = ∏ ⱼ≠ᵢ (x - x ⱼ) / (xᵢ - x ⱼ). It does not
require equally spaced data. The degree of the resulting polynomial is (n-1) for n data points. It is
widely used in numerical integration and differentiation.
Code:
#include <stdio.h>

int main() {
int n, i, j;
double x[20], y[20], xi, yi, L, num, den;
printf("Enter number of data points: ");
scanf("%d", &n);
printf("Enter x and y values:\n");
for (i = 0; i < n; i++) {
printf("x[%d] = ", i); scanf("%lf", &x[i]);
printf("y[%d] = ", i); scanf("%lf", &y[i]);
}
printf("Enter interpolation point xi: ");
scanf("%lf", &xi);
yi = 0;
for (i = 0; i < n; i++) {
num = 1; den = 1;
for (j = 0; j < n; j++) {
if (j != i) {
num *= (xi - x[j]);
den *= (x[i] - x[j]);
}
}
L = num / den;
yi += L * y[i];
}
printf("\nInterpolated value at x = %.4lf is y = %.6lf\n", xi, yi);
printf("\n------------------------------\n");
printf("Lab No.:3(a)/Name: [Your Name]/Roll No.: [No.]/Section:[X]\n");
printf("------------------------------\n");
return 0;
}

Output:
[Attach screenshot of program output here]
Lab No. 3(b): Newton's Divided Difference Interpolation
Theory:
Newton's Divided Difference Interpolation constructs the interpolating polynomial using divided
differences. The zeroth order divided difference is f[xᵢ] = yᵢ. Higher order differences are:
f[xᵢ,...,xᵢ₊ₖ] = (f[xᵢ₊₁,...,xᵢ₊ₖ] - f[xᵢ,...,xᵢ ₊ₖ₋₁]) / (xᵢ ₊ₖ - xᵢ). The polynomial is: P(x) = f[x₀] + f[x₀,x₁](x-
x₀) + f[x₀,x₁,x₂](x-x₀)(x-x₁) + ... This method works for unevenly spaced data points.
Code:
#include <stdio.h>

int main() {
int n, i, j;
double x[20], y[20][20], xi, yi, term, prod;
printf("Enter number of data points: ");
scanf("%d", &n);
printf("Enter x and y values:\n");
for (i = 0; i < n; i++) {
printf("x[%d] = ", i); scanf("%lf", &x[i]);
printf("y[%d] = ", i); scanf("%lf", &y[i][0]);
}
/* Build divided difference table */
for (j = 1; j < n; j++)
for (i = 0; i < n-j; i++)
y[i][j] = (y[i+1][j-1] - y[i][j-1]) / (x[i+j] - x[i]);
printf("Enter interpolation point xi: ");
scanf("%lf", &xi);
yi = y[0][0];
prod = 1;
for (j = 1; j < n; j++) {
prod *= (xi - x[j-1]);
yi += prod * y[0][j];
}
printf("\nInterpolated value at x = %.4lf is y = %.6lf\n", xi, yi);
printf("\n------------------------------\n");
printf("Lab No.:3(b)/Name: [Your Name]/Roll No.: [No.]/Section:[X]\n");
printf("------------------------------\n");
return 0;
}

Output:
[Attach screenshot of program output here]
Lab No. 3(c): Newton's Forward Difference Interpolation
Theory:
Newton's Forward Difference Interpolation is used when the interpolation point is near the
beginning of the data table and data is equally spaced. The forward difference operator is: Δyᵢ =
yᵢ₊₁ - yᵢ. The formula is: P(x) = y₀ + uΔy₀ + u(u-1)/2!Δ²y₀ + ..., where u = (x - x₀) / h and h is the
common spacing.
Code:
#include <stdio.h>

int main() {
int n, i, j, p;
double x[20], y[20][20], h, xi, u, term, result;
printf("Enter number of data points: ");
scanf("%d", &n);
printf("Enter equally spaced x and y values:\n");
for (i = 0; i < n; i++) {
printf("x[%d] = ", i); scanf("%lf", &x[i]);
printf("y[%d] = ", i); scanf("%lf", &y[i][0]);
}
h = x[1] - x[0];
for (j = 1; j < n; j++)
for (i = 0; i < n-j; i++)
y[i][j] = y[i+1][j-1] - y[i][j-1];
printf("Enter interpolation point xi: ");
scanf("%lf", &xi);
u = (xi - x[0]) / h;
result = y[0][0];
term = 1;
double fact = 1;
for (j = 1; j < n; j++) {
term *= (u - (j-1));
fact *= j;
result += (term / fact) * y[0][j];
}
printf("\nInterpolated value at x = %.4lf is y = %.6lf\n", xi, result);
printf("\n------------------------------\n");
printf("Lab No.:3(c)/Name: [Your Name]/Roll No.: [No.]/Section:[X]\n");
printf("------------------------------\n");
return 0;
}

Output:
[Attach screenshot of program output here]
Lab No. 3(d): Newton's Backward Difference Interpolation
Theory:
Newton's Backward Difference Interpolation is used when the interpolation point is near the end of
the data table and data is equally spaced. The backward difference operator is: ∇y ₙ = y ₙ - y ₙ₋₁.
The formula is: P(x) = yₙ + u∇yₙ + u(u+1)/2!∇²yₙ + ..., where u = (x - x ₙ) / h.
Code:
#include <stdio.h>

int main() {
int n, i, j;
double x[20], y[20][20], h, xi, u, term, result;
double fact;
printf("Enter number of data points: ");
scanf("%d", &n);
printf("Enter equally spaced x and y values:\n");
for (i = 0; i < n; i++) {
printf("x[%d] = ", i); scanf("%lf", &x[i]);
printf("y[%d] = ", i); scanf("%lf", &y[i][0]);
}
h = x[1] - x[0];
for (j = 1; j < n; j++)
for (i = 0; i < n-j; i++)
y[i][j] = y[i+1][j-1] - y[i][j-1];
printf("Enter interpolation point xi: ");
scanf("%lf", &xi);
u = (xi - x[n-1]) / h;
result = y[n-1][0];
term = 1; fact = 1;
for (j = 1; j < n; j++) {
term *= (u + (j-1));
fact *= j;
result += (term / fact) * y[n-1-j][j];
}
printf("\nInterpolated value at x = %.4lf is y = %.6lf\n", xi, result);
printf("\n------------------------------\n");
printf("Lab No.:3(d)/Name: [Your Name]/Roll No.: [No.]/Section:[X]\n");
printf("------------------------------\n");
return 0;
}

Output:
[Attach screenshot of program output here]
Lab No. 3(e): Cubic Spline Interpolation
Theory:
Cubic Spline Interpolation fits piecewise cubic polynomials between consecutive data points,
ensuring smooth transitions (continuity of first and second derivatives at interior points). For n data
points, n-1 cubic polynomials are constructed: Sᵢ(x) = aᵢ + bᵢ(x-xᵢ) + cᵢ(x-xᵢ)² + dᵢ(x-xᵢ)³. Natural
spline boundary conditions (S''(x₀) = S''(xₙ) = 0) are used. The coefficients are found by solving a
tridiagonal system.
Code:
#include <stdio.h>
#include <stdlib.h>

int main() {
int n, i, j;
double x[20], y[20], h[20], a[20], b[20], c[20], d[20];
double alpha[20], l[20], mu[20], z[20];
double xi, result;
printf("Enter number of data points: ");
scanf("%d", &n);
printf("Enter x and y values:\n");
for (i = 0; i < n; i++) {
printf("x[%d] = ", i); scanf("%lf", &x[i]);
printf("y[%d] = ", i); scanf("%lf", &y[i]);
a[i] = y[i];
}
for (i = 0; i < n-1; i++) h[i] = x[i+1] - x[i];
for (i = 1; i < n-1; i++)
alpha[i] = (3/h[i])*(a[i+1]-a[i]) - (3/h[i-1])*(a[i]-a[i-1]);
l[0]=1; mu[0]=0; z[0]=0;
for (i = 1; i < n-1; i++) {
l[i] = 2*(x[i+1]-x[i-1]) - h[i-1]*mu[i-1];
mu[i] = h[i]/l[i];
z[i] = (alpha[i] - h[i-1]*z[i-1])/l[i];
}
l[n-1]=1; z[n-1]=0; c[n-1]=0;
for (j = n-2; j >= 0; j--) {
c[j] = z[j] - mu[j]*c[j+1];
b[j] = (a[j+1]-a[j])/h[j] - h[j]*(c[j+1]+2*c[j])/3;
d[j] = (c[j+1]-c[j])/(3*h[j]);
}
printf("Enter interpolation point xi: ");
scanf("%lf", &xi);
for (i = 0; i < n-1; i++) {
if (xi >= x[i] && xi <= x[i+1]) {
double t = xi - x[i];
result = a[i] + b[i]*t + c[i]*t*t + d[i]*t*t*t;
break;
}
}
printf("\nCubic Spline value at x = %.4lf is y = %.6lf\n", xi, result);
printf("\n------------------------------\n");
printf("Lab No.:3(e)/Name: [Your Name]/Roll No.: [No.]/Section:[X]\n");
printf("------------------------------\n");
return 0;
}

Output:
[Attach screenshot of program output here]
Lab No. 4(a): Linear Least Square Method (Linear Regression)
Theory:
Linear Regression fits the best straight line y = ax + b through a set of data points by minimizing
the sum of squared residuals (errors). The normal equations derived from this minimization give: a
= (nΣxy - ΣxΣy) / (nΣx² - (Σx)²) and b = (Σy - aΣx) / n. This method is fundamental in statistics and
data analysis for finding linear trends.
Code:
#include <stdio.h>
#include <math.h>

int main() {
int n, i;
double x[50], y[50], sumX=0, sumY=0, sumXY=0, sumX2=0;
double a, b;
printf("Enter number of data points: ");
scanf("%d", &n);
printf("Enter x and y values:\n");
for (i = 0; i < n; i++) {
printf("x[%d] = ", i); scanf("%lf", &x[i]);
printf("y[%d] = ", i); scanf("%lf", &y[i]);
sumX += x[i];
sumY += y[i];
sumXY += x[i]*y[i];
sumX2 += x[i]*x[i];
}
a = (n*sumXY - sumX*sumY) / (n*sumX2 - sumX*sumX);
b = (sumY - a*sumX) / n;
printf("\nLinear Regression Line: y = %.4lf*x + %.4lf\n", a, b);
printf("\n------------------------------\n");
printf("Lab No.:4(a)/Name: [Your Name]/Roll No.: [No.]/Section:[X]\n");
printf("------------------------------\n");
return 0;
}

Output:
[Attach screenshot of program output here]
Lab No. 4(b): Nonlinear Regression – Exponential Model
Theory:
Nonlinear regression with an exponential model fits y = a*e^(bx) to data. By taking the natural
logarithm, ln(y) = ln(a) + bx, the problem is linearized: let Y = ln(y), A = ln(a), then Y = A + bx.
Linear regression is then applied to (x, ln(y)) pairs to find b and A, from which a = e^A is
recovered. Note: all y values must be positive.
Code:
#include <stdio.h>
#include <math.h>

/* Exponential model: y = a*e^(b*x) => ln(y) = ln(a) + b*x */


int main() {
int n, i;
double x[50], y[50], lny[50];
double sumX=0, sumY=0, sumXY=0, sumX2=0;
double A, b, a;
printf("Enter number of data points: ");
scanf("%d", &n);
printf("Enter x and y values (y must be positive):\n");
for (i = 0; i < n; i++) {
printf("x[%d] = ", i); scanf("%lf", &x[i]);
printf("y[%d] = ", i); scanf("%lf", &y[i]);
lny[i] = log(y[i]);
sumX += x[i];
sumY += lny[i];
sumXY += x[i]*lny[i];
sumX2 += x[i]*x[i];
}
b = (n*sumXY - sumX*sumY) / (n*sumX2 - sumX*sumX);
A = (sumY - b*sumX) / n;
a = exp(A);
printf("\nExponential Fit: y = %.4lf * e^(%.4lf * x)\n", a, b);
printf("\n------------------------------\n");
printf("Lab No.:4(b)/Name: [Your Name]/Roll No.: [No.]/Section:[X]\n");
printf("------------------------------\n");
return 0;
}

Output:
[Attach screenshot of program output here]
Lab No. 4(c): Nonlinear Regression – Polynomial Model
Theory:
Polynomial regression fits y = a₀ + a₁x + a₂x² (degree 2) to data by setting up and solving a
system of normal equations. The coefficients of the system involve sums Σxᵏ and Σxᵏy for various
powers k. A 3×3 augmented matrix is formed and solved using Gaussian elimination with back
substitution. This generalizes linear regression to capture curved trends in data.
Code:
#include <stdio.h>

/* Polynomial regression: y = a0 + a1*x + a2*x^2 (degree 2) */


int main() {
int n, i, j, k;
double x[50], y[50];
double S[5], T[3], A[3][4];
double ratio, result;
int deg = 2;
printf("Enter number of data points: ");
scanf("%d", &n);
printf("Enter x and y values:\n");
for (i = 0; i < n; i++) {
printf("x[%d] = ", i); scanf("%lf", &x[i]);
printf("y[%d] = ", i); scanf("%lf", &y[i]);
}
for (i = 0; i <= 2*deg; i++) {
S[i] = 0;
for (j = 0; j < n; j++) {
double pw = 1;
for (k = 0; k < i; k++) pw *= x[j];
S[i] += pw;
}
}
for (i = 0; i <= deg; i++) {
T[i] = 0;
for (j = 0; j < n; j++) {
double pw = 1;
for (k = 0; k < i; k++) pw *= x[j];
T[i] += y[j] * pw;
}
}
/* Build augmented matrix */
for (i = 0; i <= deg; i++) {
for (j = 0; j <= deg; j++)
A[i][j] = S[i+j];
A[i][deg+1] = T[i];
}
/* Gauss elimination */
for (i = 0; i <= deg; i++) {
for (j = i+1; j <= deg; j++) {
ratio = A[j][i] / A[i][i];
for (k = 0; k <= deg+1; k++)
A[j][k] -= ratio * A[i][k];
}
}
/* Back substitution */
double a[3];
for (i = deg; i >= 0; i--) {
a[i] = A[i][deg+1];
for (j = i+1; j <= deg; j++) a[i] -= A[i][j]*a[j];
a[i] /= A[i][i];
}
printf("\nPolynomial Fit: y = %.4lf + %.4lf*x + %.4lf*x^2\n", a[0], a[1], a[2]);
printf("\n------------------------------\n");
printf("Lab No.:4(c)/Name: [Your Name]/Roll No.: [No.]/Section:[X]\n");
printf("------------------------------\n");
return 0;
}
Output:
[Attach screenshot of program output here]

You might also like