Closed Thread
Results 1 to 5 of 5

Thread: Method of Gauss

  1. #1
    Sega is offline Newbie
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Method of Gauss

    Hello
    Help to correct the program, defines roots correctly, but in a wrong order
    Code:
    #include <iostream>
    #include <cmath>
    
    
    using namespace std;
    
    void Gauss(double *a, double *b, double *x,int n) {
     int i,j,k,t;
     double kof,s;
     double temp;
     for (i=n-1; i>0; --i) 
     {
      for (t=i, j=i-1; j>=0; --j) 
      {
       if (fabs(a[i*n+t])<fabs(a[i*n+j]))
       {
        t=j;
       }
      }
      if (a[i*n+t]==0.0)
      {
       return;
      }
      if (t!=i) 
      {
       for (k=n-1; k>=0; --k) 
       {
        temp=a[k*n+t];
        a[k*n+t]=a[k*n+i];
        a[k*n+i]=temp;
       }
      }
      for (j=i-1; j>=0; --j) 
      {
       kof=a[i+j*n]/a[i*n+i]; // kof=a[j][i]/a[i][i];
       for (a[i+j*n]=0.0, b[j]-=b[i]*kof, k=i-1; k>=0; --k) 
       {
        a[k+j*n]-=a[k+i*n]*kof; // a[k][j]-=a[k][i]*kof
       }
      }
     }
     x=new double [n];
     if (x)
     {
      for (i=0; i<n; ++i)
      {
       for (s=0.0, j=i-1; j>=0; --j)
       {
        s+=a[j+i*n]*x[j]; //s+=a[j][i]*x[j]
       }
       x[i]=(b[i]-s)/a[i*n+i]; // x[i]=(b[i]-s)/a[i][i];
       //cout << x[i] << "\n";
      }
     }
     cout << "\n";
     for (i=0; i<n; ++i)
    cout << "x " << x[i] << endl;
    }
    
    
    main()
    {
    
          int n = 6;
         double gauss2[6] ={1,5,3,7,6,3};
          double M[36]={-3, 2, 1,2, 3, 1,5, 3, 3, -1, 3, 4,0, 80,46, 34, 3, 4,6, 7, 3, 6, 6,7,57, 6, 5, 5, 3, 67,1, 55, 5, 7, 3, 7 };
            
          double *y = new double[n];
    
    	/*  for(int i=0; i<n; i++)
    		{
    			for(int j=0; j<n; j++)
    				{
    					//cin >> M[i+j*n];
    					 M[i+j*n] = rand()/100;
    //cout << "gauss1["<< i << "][" << j << "]=";
    //cin >> gauss1[i][j];
    				}
    		}
    */
    for(int i=0; i<n; i++)
    		{
    			for(int j=0; j<n; j++)
    				{
    					 cout << M[i*n+j] << "  ";
    				}
    				cout << gauss2[i] << endl;
    		}	
          Gauss(M,gauss2,y,n);
           cin.get();
    }
    My answer: 0.63, -0.12, 0.0249, 1.16, -0.49, -0.0558
    Right answer: 0.63, 0.06, 0.02, -0.12, 1.16, -0.49

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Method of Gauss

    Why not just sort your array before outputting it? Gauss's method is not guaranteed to generate roots in any particular order.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Sega is offline Newbie
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Re: Method of Gauss

    Quote Originally Posted by WingedPanther View Post
    Gauss's method is not guaranteed to generate roots in any particular order.
    Why does not guaranteed? In fact, after finding the roots, of their substitute in a certain order, and not in an arbitrary, is not it?

  5. #4
    Sega is offline Newbie
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Re: Method of Gauss

    If Substituting: 0.63, -0.12, 0.0249, 1.16, -0.49, -0.0558 , then the right decision not succeed
    so that the sequence of roots is important

  6. #5
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Method of Gauss

    Again, why not just sort the array before printing it?
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. GET method
    By Flying Dutchman in forum PHP Development
    Replies: 15
    Last Post: 03-04-2011, 09:16 PM
  2. Using an inherited method in another class' method
    By ZipOnTrousers in forum Java Help
    Replies: 1
    Last Post: 11-13-2009, 09:39 AM
  3. Encrypt method (from decrypt method)
    By mmmmmm in forum C# Programming
    Replies: 2
    Last Post: 09-19-2009, 06:18 AM
  4. GUI and Method Help
    By asharp03 in forum Java Help
    Replies: 14
    Last Post: 06-11-2009, 09:44 PM
  5. Gauss Algorithm
    By dirkfirst in forum PHP Development
    Replies: 6
    Last Post: 08-23-2006, 05:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

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