CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2010
    Posts
    3

    Question 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();
    }
    }
    _____________________________________________________________________________

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    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)

  4. #4
    Join Date
    Jun 2010
    Posts
    3

    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

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    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;
    		}
    	      }

  6. #6
    Join Date
    Jun 2010
    Posts
    3

    Re: C++ Realisation of The Full Squares Method

    Thank you very much. The program is working correctly now!!!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured