Interpolation and Least Square
Interpolation and Least Square
Algorithm
1. Start
2. Read number of data (n)
5. Initialize: yp = 0
6. For i = 1 to n
Set p = 1
For j =1 to n
If i ≠ j then
End If
Next j
Calculate yp = yp + p * Yi
Next i
8. Stop
Program in C language
#include<stdio.h>
#include<conio.h>
void main()
/* Input Section */
n"); for(i=1;i<=n;i++)
scanf("%f", &x[i]);
printf("y[%d] = ", i);
scanf("%f", &y[i]);
{ p=1;
for(j=1;j<=n;j++) {
if(i!=j)
yp = yp + p * y[i];
}
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
3. For i=1 to n:
Read Xi and Yi
Next i
4. Initialize: sumX = 0 sumX2 = 0 sumY = 0 sumXY = 0
For i=1 to n:
Yi sumXY = sumXY + Xi * Yi
Next i
A = (sumY – b*sumX)/n
9. Stop
Program in C language
#include<stdio.h>
#include<conio.h>
#define S 50
int main()
int n, i;
clrscr();
/* Input */
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]);
for(i=1;i<=n;i++)
/* Calculating a and b */
b = (n*sumXY-sumX*sumY)/(n*sumX2-sumX*sumX);
a = (sumY – b*sumX)/n;
getch(); return(0);
}
Output