-
Notifications
You must be signed in to change notification settings - Fork 0
/
Method of Least Squares.cpp
58 lines (58 loc) · 1.18 KB
/
Method of Least Squares.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* Parabolic fit by least squares */
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float augm[3][4]={{0,0,0,0},{0,0,0,0},{0,0,0,0}};
float t,a,b,c,x,y,xsq;
int i,j,k,n;
cout << "Enter the no. of pairs of"
<<"observed values:" << endl;
cin >> n;
cout << fixed;
augm [0] [0] = n;
for (i=0;i<n;i++)
{
cout << "Pair no. " << i+1 << endl;
cin >> x >> y;
xsq = x*x;
augm[0][1] += x;
augm[0][2] += xsq;
augm[1][2] += x*xsq;
augm[2][2] += xsq*xsq;
augm[0][3] += y;
augm[1][3] += x*y;
augm[2][3] += xsq*y;
}
augm[1][1] = augm[0][2];
augm[2][1] = augm[1][2];
augm[1][0] = augm[0][1];
augm[2][0] = augm[1][1];
cout << "The augmented matrix is:-" << endl;
for (i=0;i<3;i++)
{
for (j=0;j<4;j++)
cout << setw(9) << setprecision (4) << augm[i][j];
cout << endl;
}
/* Now solving for a,b,c
by Gauss Jordan Method */
for (j=0;j<3;j++)
for (i=0;i<3;i++)
if (i!=j)
{
t = augm[i][j]/augm[j][j];
for (k=0;k<4;k++)
augm[i][k] -= augm[j][k]*t;
}
a = augm[0][3]/augm[0][0];
b = augm[1][3]/augm[1][1];
c = augm[2][3]/augm[2][2];
cout << setprecision(4)
<< "a = " << setw(8) << a
<< "b = " << setw(8) << b
<< "c = " << setw(8) << c
<< endl;
return 0;
}