-
Notifications
You must be signed in to change notification settings - Fork 0
/
POLynomialSecondtry.cpp
115 lines (115 loc) · 2.21 KB
/
POLynomialSecondtry.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include<bits/stdc++.h>
using namespace std;
int n;
double a=0,b=0,sx=0.0,sxx=0.0,sy=0.0,sxy=0.0,matrix[100][100],coeff[100],sum=0.0,A[10],B[10];
double squaresum(int k)
{
double x,sum=0.0;
if(k>=1)
{
for(int i=1;i<=n;i++)
{
x=pow(A[i],k);
sum+=x;
}
return sum;
}
else
{
return 1;
}
}
double squaresumyyy(int k)
{
double x=1,sum=0.0;
for(int i=1;i<=n;i++)
{
if(k>1)
{
x=pow(A[i],k-1);
}
sum+=B[i]*x;
}
return sum;
}
int main()
{
cout<<"Enter the value of n: "<<endl;
cin>>n;
double sy=0,x=0;
cout<<"Enter x and y "<<endl;
for(int i=1;i<=n;i++)
{ cin>>A[i]>>B[i];
}
for(int i=1;i<=n;i++)
{
sy+=B[i];
}
for(int i=1;i<n;i++)
{
for(int j=1;j<=n;j++)
{ x=0;
if(i==1&&j==1)
{ matrix[i][j]=n;
}
else
{
x=0;
x=squaresum(i+j-2);
matrix[i][j]=x;
if(j==n)
{
matrix[i][j]=squaresumyyy(i);
}}}
}
for(int i=1;i<n;i++)
{
for(int j=1;j<=n;j++)
{
cout<<matrix[i][j]<<" ";
}
puts("");
}
for(int i=1;i<n-1;i++)
{
for(int j=1;j<n;j++)
{
if(i<j)
{
a=matrix[j][i]/matrix[i][i];
for(int k=1;k<=n+1;k++)
{
matrix[j][k]=matrix[j][k]-(a*matrix[i][k]);
}
}
}
}
coeff[n-1]=matrix[n-1][n]/matrix[n-1][n-1];
for(int i=n-2;i>=1;i--)
{ sum=0.0;
for(int j=i+1;j<n;j++)
{
sum+=coeff[j]*matrix[i][j];
}
coeff[i]=(matrix[i][n]-sum)/matrix[i][i];
}
puts("");
for(int i=1;i<n;i++)
{
cout<<"X"<<i<<" : "<<coeff[i]<<endl;
puts("");
}
cout<<"The least squares quadratic polynomial is: y=";
for(int i=1;i<n;i++)
{
if(i>1)
{
if(coeff[i]==abs(coeff[i]))
{ cout<<"+";
}
}
cout<<coeff[i];
if(i>1)
{ cout<<"x"<<"^"<<i-1;
}}
}