0% found this document useful (0 votes)
388 views6 pages

To Implement Curve Fitting Using Least Squares Curve Fitting Method

The document describes the least squares curve fitting method algorithm. The algorithm has the following steps: 1) Initialize arrays to store input data and coefficients and set iteration counter to 0. 2) Calculate initial values of x array elements from input coefficients. 3) Iteratively update x array elements until difference between x and y arrays is less than threshold or maximum iterations reached. 4) Print final x values.

Uploaded by

Befzz
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)
388 views6 pages

To Implement Curve Fitting Using Least Squares Curve Fitting Method

The document describes the least squares curve fitting method algorithm. The algorithm has the following steps: 1) Initialize arrays to store input data and coefficients and set iteration counter to 0. 2) Calculate initial values of x array elements from input coefficients. 3) Iteratively update x array elements until difference between x and y arrays is less than threshold or maximum iterations reached. 4) Print final x values.

Uploaded by

Befzz
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/ 6

Least Squares Curve Fitting Method

Aim: To implement curve fitting using least squares curve fitting method.
Algorithm
Step 1: Start the program
Step 2: Declare x[10], y[10], a[10][10] as float data type and i, j, k, n, itr as integer data type
Step 3: Read the size and elements of a matrix using for loop
Step 4: initialise x[i] & y[i] =0.0 and using loop:
1) itr = itr + 1
2) Using for loop:
a) x[i] = a[i][n+1]
b) if (i==j)
continue
else
x[i] = x[i] a[i][j]*x[i]
c) x[i] = x[i]/a[i][j]
using for loop:
if (fabs(x[k]-y[k]>0.0001)
print itr
d) using for loop:
calculate y[i] = x[i]
and print i, x[i]
repeat the slips
Step 5: if not return
Step 6: Stop the program

Flowchart
Start

float x[10], y[10], a[10][10]


int i, j, k, n, itr
read n

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


Read a[i][j]
for(i = 1; i <= n; i++)
x[i] = 0.0
y[i] = 0.0
itr = 0.0
Top
itr = itr + 1

False

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


False

False

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


x[i] = a[i][n+1]

False

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


if
i==j

continue
x[i] = x[i] a[i][j] * x[i]
x[i] = x[i]/a[i][j]

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


if
fabs(x[k] y[k]>0.0001)

print itr
False

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


y[i] = x[i]
print i, x[i]
goto Top
continue
return
Stop

Program
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
float x[10],y[10],a[10][10];
int i,j,k,n,itr;
clrscr();
printf ("**--(57*s)--**\n");
printf ("*\t\t\t\t\t\t\t*\n");
printf ("*\t\t\tCurve Fitting\t\t\t*\n");
printf ("*\t\t\t\t\t\t\t*\n**--(57*s)--**\n\n");
printf("\n ENTER THE SIZE OF MATRIX n:");
scanf("%d",&n);
printf("\n ENTER MATRIX,'A' ELEMENTS AND X:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
scanf("%f",&a[i][j]);
}
for(i=1;i<=n;i++)
{
x[i]=0.0;
y[j]=0.0;
}
itr=0.0;
top:
itr=itr+1;
for(i=1;i<=n;i++)
{
x[i]=a[i][n+1];
for(j=1;j<=n;j++)
{
if(i==j)
continue;
else
x[i]=x[i]-a[i][j]*x[j];
}
x[i]=x[i]/a[i][j];
}
for(k=1;k<=n;k++)
if(fabs(x[k]-y[k])>0.0001)
{
printf("\n ITERATION=[%d]",itr);
for(i=1;i<=n;i++)

{
y[i]=x[i];
printf("\t x(%d)=%f",i,x[i]);
}
goto top;
}
else
continue;
return;
}

Output

You might also like