# C++ Realisation of The Full Squares Method

• June 16th, 2010, 04:27 PM
Johny_Blackbird
C++ Realisation of The Full Squares Method
Hello, Gurus. Please, help me. I have written C++ Realisation of The Full Squares Method in Visual Studio 2010. My application has no compilation errors. But the idea of this method is approximation of experimental data by a parabolic function. Despite this, my program gives me no approximation!!! The graphic made by the results of approximation is particullary different from the experimental data graphic. Please, tell me, what can I do? Where is the error? Here is the text of my program:
__________________________________________________________________________
#include<stdio.h>
#include<math.h>
#include<conio.h>
#include <iostream>
#define n 3
#define m 4
using namespace std;
void main()
{
double x[20], y[20], dy[20];
double Sx, Sy, Sx2, Sx3, Sx4, Syx, Syx2, S;
Sx=0;
Syx=0;
Sy=0;
Syx2=0;
Sx2=0;
Sx3=0;
Sx4=0;
double z[20]={2.05,2.3,2.27,2.31,2.52,2.63,2.58,2.54,2.5,2.62,2.1,2.28,2.,1.62,1.44,1.04,0.84,0.5,0.13,-0.13};
int N1=14;
int N=20;
for(int i=0;i<20;i++)
{
x[i]=(i-1)*0.1;
y[i]=z[i]+N1*0.1;
Sx=Sx+x[i];
Sx2=Sx2+pow(x[i],2);
Sx3=Sx3+pow(x[i],3);
Sx4=Sx4+pow(x[i],4);
Sy=Sy+y[i];
Syx=Syx+y[i]*x[i];
Syx2=Sx2+y[i]*pow(x[i],2);
}
for(int i=0;i<20;i++)
{
printf("%le\t %le\n",x[i],y[i]);
}
{double a[3][4]={{Sx4,Sx3,Sx2,Syx2},{Sx3,Sx2,Sx,Syx},{Sx2,Sx,N,Sy}};
int ii,j,k;
double r[3],c,s;
printf("\n isxodnaya matrica: \n");
for(ii=0;ii<n;ii++)
{for(j=0;j<m;j++)
printf("%f\t",a[ii][j]);
printf("\n");
}
printf("\n____________________________\n");
for(ii=0;ii<n;ii++)
{c=a[ii][ii];
for(j=0;j<m;j++)
a[ii][j]=a[ii][j]/c;
if(ii!=n-1)
{
for(k=ii+1;k<n;k++)
for(j=0;j<m;j++)a[k][j]=a[k][j]-a[ii][j]*a[k][ii];
}
}
printf("\n 2-naya matrica: \n");
for(ii=0;ii<n;ii++)
{for(j=0;j<m;j++)
printf("%f\t",a[ii][j]);
printf("\n");
}
printf("n=%d\tm=%d\t",n,m);
for(ii=0;ii<n;ii++)r[ii]=0;
//x[n-1]=a[n-1][n];
for(ii=n-1;ii>=0;ii--)
{s=0;
for(j=n-1;j>=0;j--)s=s+a[ii][j]*r[j];
r[ii]=a[ii][m-1]-s;
}
printf("\n Korni: \n");
for(ii=0;ii<n;ii++)
printf("\n r[%d]=%f",ii,r[ii]);
printf("\n");
printf("\n proverka: \n");
for(ii=0;ii<n;ii++)
{s=0;for(j=0;j<n;j++)s+=a[ii][j]*r[j];
printf("\n %d-e: %f=%f\n",ii,s,a[ii][m-1]);
printf("\n");
}
double Y[20];
for(int i=0;i<20;i++)
{
x[i]=(i-1)*0.1;
Y[i]=r[0]*pow(x[i],2)+r[1]*x[i]+r[2];
}
S=0;
for(int i=0;i<20;i++)
{
dy[i]=y[i]-Y[i];
S=S+pow(dy[i],2);
}
for(int i=0;i<20;i++)
{
printf("%f\t %f\n",x[i],Y[i]);
}

printf("\n\nS=%f\t",S);

_getch();
}
}
_____________________________________________________________________________
• June 16th, 2010, 04:35 PM
Lindley
Re: C++ Realisation of The Full Squares Method
Obviously, the math you're doing is not precisely the math you need to be doing.

I'm not familiar with the method so I can't even guess where you're going wrong, and I doubt I'd try to interpret that code without code tags for indentation anyway.
• June 16th, 2010, 06:52 PM
Philip Nicoletti
Re: C++ Realisation of The Full Squares Method
This line looks suspect :

Code:

`Syx2=Sx2+y[i]*pow(x[i],2);`
(consider using += in all of those cases)
• June 17th, 2010, 03:52 AM
Johny_Blackbird
Re: C++ Realisation of The Full Squares Method
Thanks, Philip Nicoletti, I have corrected the mistake, but my program still gives me no approximation. As you can see:
Experimental data:
3.450000
3.700000
3.670000
3.710000
3.920000
4.030000
3.980000
3.940000
3.900000
4.020000
3.500000
3.680000
3.400000
3.020000
2.840000
2.440000
2.240000
1.900000
1.530000
1.270000
approximated data:
3.244045
3.207000
3.167476
3.125474
3.080992
3.034032
2.984592
2.932674
2.878277
2.821401
2.762046
2.700213
2.635900
2.569109
2.499839
2.428089
2.353861
2.277155
2.197969
2.116304
• June 17th, 2010, 09:12 AM
Philip Nicoletti
Re: C++ Realisation of The Full Squares Method
I think there is a problem when you are solving the 3x3 linear system. You have:

Code:

```for(k=ii+1;k<n;k++) for(j=0;j<m;j++)a[k][j]=a[k][j]-a[ii][j]*a[k][ii];```
The factor : a[k][ii] gets over written. Save it first :

Code:

```              for(k=ii+1;k<n;k++)               {                                double a_k_ii_save = a[k][ii];                 for(j=0;j<m;j++)                 {                     a[k][j]=a[k][j]-a[ii][j]*a_k_ii_save;                 }               }```
• June 17th, 2010, 06:33 PM
Johny_Blackbird
Re: C++ Realisation of The Full Squares Method
Thank you very much. The program is working correctly now!!!