1 / 7

实例 1. 解线性代数方程组

实例 1. 解线性代数方程组. /////////////////////////////////////////////////////////////////////////////////////////////////// // 题目 : Gauss 消元法 — 选主元 , Ax = b 求 x ///////////////////////////////////////////////////////////////////////////////////////////////////

yardley
Télécharger la présentation

实例 1. 解线性代数方程组

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 实例1. 解线性代数方程组 /////////////////////////////////////////////////////////////////////////////////////////////////// // 题目: Gauss 消元法 — 选主元, Ax = b 求x /////////////////////////////////////////////////////////////////////////////////////////////////// #include <stdio.h> // 所需的头文件 #include <conio.h> #include <math.h> #define MAX_n 100 // 本程序能处理的方程最大阶数 #define PRECISION 0.0000001 // 主元是否小于这个数

  2. // 功能: 输入m X n 阶阵 到二元数组 A[][] 中 // 注意: 行列下标都从1开始, 下标0未用 voidMatrixInput( float A[][MAX_n], int m, int n ) { int i , j; float ftmp; printf( "\n===Begin input Matrix elements===\n" ); for( i = 1; i <= m; ++i ) { printf( "Input_Line %d : ", i ); for( j = 1; j <= n; ++j ) { scanf( "%f", &ftmp ); A[i][j] = ftmp; } } }

  3. // 功能: 输出 n X m 阶矩阵A中的第k列 // 注释: 因为是输出列, 故m不用作为参数传过来 voidMatrixOneColumnOutput(float A[][MAX_n],int n,int k ) { int i; for( i = 1; i <= n; ++i ) { printf( "\nx[%d]=%f", i, A[i][k] ); } } // 功能: a,b二个变量中的值交换 void Swap( float *a, float *b ) { float ftmp; ftmp = *a; *a = *b; *b = ftmp; }

  4. // 功能: 解n阶上三解方程组 // 输入: U[][1] - U[][n] - 上三角阵 // U[][n+1] - 方程组的右端向量 // 输出: U[][n+1] - 方程组的解 // 返回: 0 - 成功 // 1 - 失败 intUpTriangle( float U[][MAX_n], int n ) { int i, j; for( i = n; i > 0;--i ) { if(fabs( U[i][i] ) < PRECISION )return 1; for( j = i + 1; j <= n; ++j ) { U[i][n+1] -= U[i][j] * U[j][n+1]; } U[i][n+1] /= U[i][i]; } return 0; }

  5. // 功能: Gauss列选主元消去法 // 输入: A[][1] - A[][n] - 系数矩阵 // A[][n+1] - 方程组的右端向量 // 输入: A[][n+1] - 方程的解 // 返回: 0 - 有解 // 1 - 无解 // 注意: 行列下标都从1开始, 下标0未用 int GaussElimination_column_select( float A[][MAX_n], int n ) { int i, j, k; float fTmp; for( i = 1; i < n; ++i ) { // 消元,最后成了上三角方程组 if( fabs( A[i][i] ) < PRECISION ) return 1; //主元太小,消元失败,失败近回1 for( j = i + 1; j <= n; ++j ) { for( k = i + 1; k <= n+1; ++k ) A[j][k] -= A[i][k] * A[j][i] / A[i][i]; } } // 解上三角方程组 UpTriangle( A, n ); return 0; // 成功近回0 }

  6. void main() { int n; float A[MAX_n][MAX_n]; // 输入阶数到n printf( "\nInput n =" ); scanf( "%d", &n ); if( n >= MAX_n - 1 ) { printf( "\n\007n must < %d!", MAX_n ); exit( 0 ); } // 输入系数矩阵和右端向量,即方程组的增广矩阵的逐行输入 MatrixInput( A, n, n + 1 ); // 消元 if( GaussElimination_column_select( A, n ) ) printf( "\nGauss Failed!" ); // 失败 else { printf( "\nOutput Solution:" ); MatrixOneColumnOutput( A, n, n + 1 ); // 输出解向量 } }

  7. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // 运行实例 /* Input n = 4 ===Begin input Matrix elements=== Input_Line 1 : 0.4096 0.1234 0.3678 0.2943 0.4043 Input_Line 2 : 0.2246 0.3872 0.4015 0.1129 0.155 Input_Line 3 : 0.3645 0.192 0.3781 0.0643 0.424 Input_Line 4 : 0.1784 0.4002 0.2786 0.3927 -0.2557 Output Sulution: x[1]=-0.181918 x[2]=-1.663031 x[3]=2.217229 x[4]=-0.446704 */

More Related