标签:
1、Gauss消去法及列主元素消去法算法介绍
2、c#源码
1 /******************************** 2 高斯列主元素消去法解多元一次方程组 3 * 4 * 例如 方程组 5 * x+y+z=7 6 * 2x+y-z=6 7 * x-y-2z=4 8 * 9 * 矩阵阶数N=3 10 * a[N,N]为1 1 1 11 * 2 1 -1 12 * 1 -1 -2 13 * b[N]为 7 14 * 6 15 * 4 16 *********************************/ 17 using System; 18 using System.Collections.Generic; 19 using System.Linq; 20 using System.Text; 21 using System.Threading.Tasks; 22 23 namespace Gauss 24 { 25 class Program 26 { 27 const int MaxN = 20; 28 private float[,] a = new float[MaxN, MaxN]; 29 private float[] b = new float[MaxN]; 30 private float[] x = new float[MaxN]; 31 32 private int N;//所要计算的矩阵的阶数 33 private float center;//中间变量 34 static void Main(string[] args) 35 { 36 Program p = new Program(); 37 p.ReadMatrix(); 38 p.ShowMatrix(); 39 p.Calculate(); 40 p.ShowResult(); 41 Console.ReadLine(); 42 } 43 44 //输入线性方程组对应的矩阵 45 private void ReadMatrix() 46 { 47 Console.Write("请输入矩阵的阶数:"); 48 while (!int.TryParse(Console.ReadLine(),out N)) 49 { 50 Console.WriteLine("请输入正确的阶数!"); 51 } 52 53 Console.WriteLine("请输入矩阵a[{0}][{0}]:",N); 54 for (int i = 0; i < N; i++) 55 { 56 for (int j = 0; j < N; j++) 57 { 58 while (!float.TryParse(Console.ReadLine(), out a[i,j])) 59 Console.WriteLine("请正确输入!"); 60 61 } 62 } 63 64 for (int i = 0; i < N; i++) 65 { 66 Console.Write("请输入矩阵b[{0}]:", i); 67 while (!float.TryParse(Console.ReadLine(), out b[i])) 68 Console.WriteLine("请正确输入!"); 69 } 70 Console.WriteLine("\n=========================================="); 71 } 72 73 //显示矩阵对应的方程组 74 private void ShowMatrix() 75 { 76 Console.WriteLine("要求的方程组为:"); 77 for (int i = 0; i < N; i++) 78 { 79 for (int j = 0; j < N; j++) 80 { 81 if (j == 0) 82 { 83 string s; 84 s = a[i, j] + "x" + (j + 1); 85 Console.Write(s); 86 } 87 88 else 89 { 90 string s = a[i, j] > 0 ? "+" + a[i, j] + "x" + (j+1) : a[i, j] + "x" + (j+1); 91 Console.Write(s); 92 } 93 } 94 Console.WriteLine(" = "+b[i]); 95 } 96 Console.WriteLine("\n=========================================="); 97 } 98 99 private void Calculate() 100 { 101 float sum; 102 int i, k, p; 103 for (k = 0; k < N-1; k++) 104 { 105 center = Math.Abs(a[k,k]);//对角线上 106 for (i = k + 1, p = k; i < N; i++) 107 { 108 //选取主元 109 if (center < Math.Abs(a[i, k])) 110 { 111 center = Math.Abs(a[i, k]); 112 p = i; 113 } 114 } 115 116 if (i > k) 117 { 118 //移动主元 119 for (int m = k; m < N; m++) 120 { 121 center = a[k, m]; 122 a[k, m] = a[p, m]; 123 a[p, m] = center; 124 } 125 center = b[k]; 126 b[k] = b[p]; 127 b[p] = center; 128 } 129 130 //消元 131 for (int m = k + 1; m < N; m++) 132 { 133 center = a[m, k]; 134 for (int n = k; n < N; n++) 135 { 136 a[m, n] -= center * a[k, n] / a[k, k]; 137 } 138 b[m] -= center * b[k] / a[k, k]; 139 } 140 141 142 } 143 //x[N]的值 144 x[N - 1] = b[N - 1] / a[N - 1, N - 1]; 145 for (int m = N - 2; m >= 0; m--) 146 { 147 sum = b[m]; 148 for (int j = N - 1; j > m; j--) 149 { 150 sum -= a[m, j] * x[j]; 151 x[m] = sum / a[m, m]; 152 } 153 } 154 } 155 156 157 private void ShowResult() 158 { 159 Console.WriteLine("结果为:"); 160 for (int i = 0; i < N; i++) 161 { 162 Console.Write("x{0}={1}\n",i+1,x[i]); 163 } 164 165 } 166 } 167 }
3、运行结果
标签:
原文地址:http://www.cnblogs.com/zhangbaochong/p/4799738.html