Write the Gauss elimination method by yourself

Source: Internet
Author: User

Yesterday, xiaowu asked me to help him write a program about the Gauss elimination method. Before that, he seldom wrote such a program related to mathematics. So he decided to give it a try, here I want to demonstrate a programming method. I used to think about a method when I didn't know how to effectively write a program. It took a long time, I have seen on the Internet about the driver test programming, there is a kind of "Suddenly Looking back, that person is in the dark" feeling, So now do not learn what design patterns and so on, you can better understand it by writing it one step at a time.
First, the process of the Gauss elimination method is to use the Elementary Line Transformation to convert the originally difficult-to-solve equations into easy-to-solve equations, that is, to form a so-called triangular array, I don't know what it means.
At the beginning, I first sorted out some general ideas. When solving the problem, I first sorted out the ideas to solve more than half of the problems ,:
1. When the main program starts from the first line, it performs an elementary transformation on each of the remaining lines so that the first element of all rows is changed to 0 except the first line, followed by the second line .... In this way, it becomes a triangular array.
So I think the main cycle is like this:
For (int I = 0; I! = ROW-1; I ++) // this layer of loop is used to "generate" each ROW to be used as the principal element.
{
For (int j = I + 1; j! = ROW; j ++) // perform Elementary Transformation on each ROW after the principal component
//................ Do some things
}
2. I think we need to write a print function. printing this matrix not only facilitates the display of results, but also facilitates the display of errors when writing programs,
3. Two macros are used to define rows and columns of the matrix.
4. Use a two-dimensional matrix to store this matrix and use the float type.
5. During the freshman year's calculation, we also need to perform an operation on the triangle array, so that we can directly calculate the values of x, but now we can use a for loop to bring back the values of x.
Start writing programs now ,,,,,,,
[Cpp]
# Include <stdio. h>
# Define ROW 4
# Define LIST 5
 
Float a [ROW] [LIST] = {, 0,-}, {-,-2}, {, 5,-4, -4}, {0, 1, 2,-1,-2 }};
 
/* Write a print function to test the array elements */
Void print ();
Int main ()
{
Print ();/* First test whether our array elements are correct */
 
/* Start to delete */
For (int I = 0; I! = ROW-1; I ++)
{
For (int j = I + 1; j! = ROW; j ++)
{
Float p = a [j] [I]/a [I] [I];
For (int k = 0; k! = LIST; k ++)
{
A [j] [k] = a [j] [k]-p * a [I] [k];
}
}
 
}
 
Print ();
 
/* Start proxy */
Float x [ROW] = {0, 0, 0 };
 
For (int w = ROW-1; w> = 0; w --)
{/*
X [w] = (a [w] [LIST-1]-(a [w] [0] * x [0] + a [w] [1] * x [1] + a [w] [2] * x [2] + a [w] [3] * x [3]) /a [w] [w];
*/
Float temp = a [w] [LIST-1];
For (int c = 0; c! = LIST-1; c ++)
Temp = temp-a [w] [c] * x [c];
X [w] = temp/a [w] [w];
 
Printf ("x % d = %-8.4f \ n", w + 1, x [w]);
}
 
Return 0;
}
 
Void print ()
{
For (int I = 0; I! = ROW; I ++)
{
For (int j = 0; j! = LIST; j ++)
Printf ("%-8.4f", a [I] [j]);
Printf ("\ n ");
}
Printf ("----------------------- \ n ");
 
}

It should be noted that I first wrote such a "hard encoding" when writing back the tape ".
X [w] = (a [w] [LIST-1]-(a [w] [0] * x [0] + a [w] [1] * x [1] + a [w] [2] * x [2] + a [w] [3] * x [3]) /a [w] [w];
This is because it is right to know your own ideas .. After that, change the program
Float temp = a [w] [LIST-1];
For (int c = 0; c! = LIST-1; c ++)
Temp = temp-a [w] [c] * x [c];
X [w] = temp/a [w] [w];
Success !!!!

 


From the domineering grass

Related Article

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.