Column Principal Component Method for Solving the equation C ++

Source: Internet
Author: User
// Program: Zhang jianbo
# Include "iostream. H"

Double N [3] [4] = {
{0.0001, 0.5402, 0.3425, 0.8828 },
{1.235, 2.567, 0.9750, 4.777 },
{1.024, 2.001, 4.555, 7.580 }};

Void print ()
{
// Output matrix
Cout <"/n ----------------------------------/N ";
For (INT I = 0; I <3; I ++)
{
For (int K = 0; k <4; k ++)
{Cout <n [I] [k] <"";
If (N [I] [k] = 0) cout <"";
}
Cout <"/N ";
}
Cout <"/n ----------------------------------/N ";

}

Void F1 ()
{// Adjust the matrix in ascending order
Double TMP;
Int K = 0;
Int p = 0, q = 0;
// Bubble Method
For (INT I = P; I <3; I ++)
For (Int J = P; j <3; j ++)
{
If (N [I] [Q] = N [J] [Q] & N [I] [Q] = 0)
{
// If the value of 1st elements in each row is 0, the values are sorted by 2nd elements.
// And so on. If the value of element 2nd is 0, the elements are sorted by 3rd.
Q ++;
}
Else
If (N [I] [Q]> N [J] [Q])
{// Sort rows by key elements
For (k = 0; k <4; k ++)
{
TMP = N [I] [k];
N [I] [k] = N [J] [k];
N [J] [k] = TMP;
}

}
}
}
Void F2 ()
{
// Cancel the RMB
// Change the coefficient of 1st to 1.

Int I;
Double A, T [4];

// By default, the elimination starts from line 1 and starts from line 1.
Int r = 1, L = 0; // R = row L = Column
Int q = 0;
Int h = 0;
Int n = 1;

Loop:

For (H = r; H <3; H ++)
{

A =-N [H] [Q]/n [Q] [Q];
For (I = Q; I <4; I ++)
{
T [I] = N [Q] [I] *;
N [H] [I] + = T [I];
}
Cout <"/n no." <n ++ <"sub-offset/N"; print ();

If (h> 0 & H <3 & N [h-1] [Q] = 0 & N [h-1] [Q] = N [H] [Q])
{
F1 (); // sort
Q ++;
R ++;
Print ();
Goto loop;
}

}
}

Void F3 ()
{
// Iteration Root
Int n = 0;

Double X2 = (N [2] [3]-N [2] [1]-N [2] [0])/n [2] [2];

Double X1 = (N [1] [3]-N [1] [2] * x2-N [1] [0])/n [1] [1];

Double X0 = (N [0] [3]-N [0] [2] * x2-N [0] [1] * x2)/n [0] [0];

Cout <"X0 =" <x0 <"X1 =" <X1 <"X2 =" <X2 <Endl;

}

Void main ()
{
Cout <"original matrix:/N ";
Print ();
Cout <"sorting:/N ";
F1 (); // sort
Print ();
F2 (); // consumer
Print ();

F3 ();

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.