乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      高斯列主元素消去法解多元一次方程組

       昵稱10504424 2015-09-11
      復(fù)制代碼
        1 /********************************
        2 高斯列主元素消去法解多元一次方程組
        3  * 
        4  * 例如 方程組
        5  * x+y+z=7
        6  * 2x+y-z=6
        7  * x-y-2z=4
        8  * 
        9  * 矩陣階數(shù)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;//所要計(jì)算的矩陣的階數(shù)
       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         //輸入線性方程組對(duì)應(yīng)的矩陣
       45         private void ReadMatrix()
       46         {
       47             Console.Write("請(qǐng)輸入矩陣的階數(shù):");
       48             while (!int.TryParse(Console.ReadLine(),out N))
       49             {
       50                 Console.WriteLine("請(qǐng)輸入正確的階數(shù)!");
       51             }
       52 
       53             Console.WriteLine("請(qǐng)輸入矩陣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("請(qǐng)正確輸入!");
       60 
       61                 }
       62             }
       63 
       64             for (int i = 0; i < N; i++)
       65             {
       66                 Console.Write("請(qǐng)輸入矩陣b[{0}]:", i);
       67                 while (!float.TryParse(Console.ReadLine(), out b[i]))
       68                     Console.WriteLine("請(qǐng)正確輸入!");
       69             }
       70             Console.WriteLine("\n==========================================");
       71         }
       72 
       73         //顯示矩陣對(duì)應(yīng)的方程組
       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]);//對(duì)角線上
      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                     //移動(dòng)主元
      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("結(jié)果為:");
      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 }
      復(fù)制代碼

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多