C#--Gauss消元之第k次消元法

Gauss消元之第k次消元法

主要步骤:

1,判断|a[i,k]|=Max|a[i,k]|!=0  (k<=i<=n)
2.如果ir!=k,矩阵进行行交换;ir行和k行交换;
3,进行消元,消元方法和Gauss直接消元法一样;(不知道的可以看关于Gauss直接消元法的文章)

核心部分换行:
 public void Exchang()
        {
            double max = 0;
            int ik = 0;
            for (int k = 0; k < n - 1; k++)
            {
                for (int i = k; i < n; i++)
                {
                    if (Math.Abs(a[i, k]) > max)
                    {
                        max = Math.Abs(a[i, k]);
                        ik = i;
                        if (ik != k) //换行
                        {
                            for (int j = k; j < n + 1; j++)
                            {
                                double t = a[k, j];
                                a[k, j] = a[ik, j];
                                a[ik, j] = t;
                            }
                        }
                        else break;
                    }
                }
            }
        }

完整部分:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Guass2
{
    public class Guass2
    {
        private int n;
        public int N
        {
            get { return n; }
            set { n = value; }
        }
       private  double [,]a;
       public double[,] A
        {
            get { return a; }
            set { a = value; }
        }
       private  double[] x;
       public double []X
        {
            get { return x; }
            set { x = value; }
        }
       
        //构造函数
        public void Input()
        {
            
            Console.WriteLine("请输入阶数:");
            n =Convert .ToInt32 ( Console.ReadLine());
            a = new double[n, n + 1];
            x = new double[n];
            Console.WriteLine("请输入各行系数:");
            for (int i=0;i<n;i++)
            {
                string s = Console.ReadLine();
                string[] ss = s.Split(' ', ',');
                for(int j=0;j<n+1;j++)
                {
                    a[i, j] = Convert.ToDouble(ss[j]);
                }
            }
        }

        public void Exchang()
        {
            double max = 0;
            int ik = 0;
            for (int k = 0; k < n - 1; k++)
            {
                for (int i = k; i < n; i++)
                {
                    if (Math.Abs(a[i, k]) > max)
                    {
                        max = Math.Abs(a[i, k]);
                        ik = i;
                        if (ik != k) //换行
                        {
                            for (int j = k; j < n + 1; j++)
                            {
                                double t = a[k, j];
                                a[k, j] = a[ik, j];
                                a[ik, j] = t;
                            }
                        }
                        else break;
                    }
                }
            }
        }
        public void Xiaoyuan1()
        {
            for (int k = 0; k < n - 1; k++)
            {
                Exchang();
                for (int i=k+1;i<n;i++)
                {
                    double suma = a[i, k] / a[k, k];                  
                        for (int j = k; j < n + 1; j++)
                            a[i, j] = a[i, j] - suma * a[k, j];                  
                }
                Console.WriteLine("\n第{0}次消元,系数为:", k + 1);
                for (int i=0;i<n;i++)
                {
                    string s = null;
                    for (int j=0;j<n+1;j++)
                    {
                        s += string.Format("{0,8:f2}", a[i, j]);
                    }
                    Console.WriteLine(s);
                }
            }
        }
        public void huidai()
        {
            for (int i=n-1;i>=0;i--)
            {
                double sum = 0;
                for (int j=i+1;j<n;j++)
                {
                    sum = a[i, j] * x[j];
                }
                x[i] = (a[i, n] - sum) / a[i, i];
            }
            Console.WriteLine("方程组的解为:");
            for (int i=0;i<n;i++)
            {
                Console.WriteLine("x{0}={1,4:f2};", i + 1, x[i]);
            }
        } 

    }
    class Program
    {
        static void Main(string[] args)
        {
            Guass2 abc = new Guass2 ();
            abc.Input();
            abc.Xiaoyuan1();
            abc.huidai();
        }
    }
}

结果:


谢谢!

猜你喜欢

转载自blog.csdn.net/qq_40953393/article/details/80200824