Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
301 views18 pages

Interpolation and Least Square

This document contains a lab report submitted by Umesh Kumar Gautam to the Department of Computer Engineering at Tribhuvan University, Purwanchal Campus in Dharan, Nepal on March 17, 2080. The report summarizes the Lagrange interpolation method and least squares method. It includes the algorithms, programs in C language, and sample outputs for calculating polynomial interpolation and determining the best fit line using least squares regression.

Uploaded by

rojandahal99
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)
301 views18 pages

Interpolation and Least Square

This document contains a lab report submitted by Umesh Kumar Gautam to the Department of Computer Engineering at Tribhuvan University, Purwanchal Campus in Dharan, Nepal on March 17, 2080. The report summarizes the Lagrange interpolation method and least squares method. It includes the algorithms, programs in C language, and sample outputs for calculating polynomial interpolation and determining the best fit line using least squares regression.

Uploaded by

rojandahal99
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/ 18

TRIBHUVAN UNIVERSITY

INSTITUTE OF ENGINEERING

PURWANCHAL CAMPUS, DHARAN – 8


Lab Report on: Lagrange Interpolation and Least
Square Method
Submitted by: Submitted to:
Name: Umesh Kumar Gautam Department of

Roll no:- PUR077BCE093. Computer Engineering

Faculty: BCE

Group: D Checked by

Date: 2080-03-17
Lagrange Interpolation Method

Lagrange interpolation theorem may be used to


generate a polynomial that passes over a group of
points and takes certain values randomly. The
formula for nth-degree polynomials is given by this
theorem (x) if f(x) is known at discrete points xi, I =
0, 1, 2,...

Algorithm
1. Start
2. Read number of data (n)

3. Read data Xi and Yi for i=1 ton n

4. Read value of independent variables say xp

Whose corresponding value of dependent say yp


is to be determined.
5. Initialize: yp = 0

6. For i = 1 to n

Set p = 1

For j =1 to n

If i ≠ j then

Calculate p = p * (xp – Xj)/(Xi – Xj)

End If
Next j

Calculate yp = yp + p * Yi

Next i

7. Display value of yp as interpolated value.

8. Stop
Program in C language

#include<stdio.h>

#include<conio.h>

void main()

float x[100], y[100], xp, yp=0, p;

int i,j,n;

clrscr();

/* Input Section */
printf("Enter number of data: ");

scanf("%d", &n);

printf("Enter data:\n");

for(i=1;i<=n;i++)

printf("x[%d] = ", i);

scanf("%f", &x[i]);

printf("y[%d] = ", i);

scanf("%f", &y[i]);

printf("Enter interpolation point: ");

scanf("%f", &xp);

/* Implementing Lagrange Interpolation */

for(i=1;i<=n;i++)

p=1;

for(j=1;j<=n;j++)
{

if(i!=j)

p = p* (xp - x[j])/(x[i] - x[j]);

yp = yp + p * y[i];

printf("Interpolated value at %.3f is %.3f.", xp, yp);

getch();

Output
Least square method
The method of least squares is a standard
approach in regression analysis to approximate the
solution of overdetermined systems (sets of
equations in which there are more equations than
unknowns) by minimizing the sum of the squares of
the residuals (a residual being the difference
between an observed value and the fitted value
provided by a model) made in the results of each
individual equation.

Algorithm
1. Start

2. Read Number of Data (n)

3. For i=1 to n:

Read Xi and Yi

Next i
4. Initialize:

sumX = 0

sumX2 = 0

sumY = 0

sumXY = 0

5. Calculate Required Sum

For i=1 to n:

sumX = sumX + Xi

sumX2 = sumX2 + Xi * Xi

sumY = sumY + Yi

sumXY = sumXY + Xi * Yi

Next i

6. Calculate Required Constant a and b of y = a +


bx:
B = (n * sumXY – sumX * sumY)/(n*sumX2 – sumX
* sumX)

A = (sumY – b*sumX)/n

7. Display value of a and b

9. Stop

Program in C language

#include<stdio.h>

#include<conio.h>

#define S 50
int main()

int n, i;

float x[S], y[S], sumX=0, sumX2=0, sumY=0, sumXY=0,


a, b;

clrscr();

/* Input */

printf(“How many data points?\n”);


scanf(“%d”, &n);

printf(“Enter data:\n”);

for(i=1;i<=n;i++)

printf(“x[%d]=”,i);

scanf(“%f”, &x[i]);

printf(“y[%d]=”,i);

scanf(“%f”, &y[i]);
}

/* Calculating Required Sum */

for(i=1;i<=n;i++)

sumX = sumX + x[i];

sumX2 = sumX2 + x[i]*x[i];

sumY = sumY + y[i];

sumXY = sumXY + x[i]*y[i];

}
/* Calculating a and b */

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

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

/* Displaying value of a and b */

printf(“Values are: a=%0.2f and b = %0.2f”,a,b);

printf(“\nEquation of best fit is: y = %0.2f + %0.2fx”,a,b);

getch();

return(0);

Output

You might also like