利用雅可比(Jocabi)迭代法解线性方程组

类别:.NET开发 点击:0 评论:0 推荐:

///<summary>

///利用雅可比(Jocabi)迭代法解线性方程组

///<summary>

using System;

 

namespace Interetive

{

     /// <summary>

     /// Jacobi 的摘要说明。

     /// </summary>

     class Jacobi

     {

          private int n;

          private double[,] Parameters;

          private double[,] Value;

          private double Precision;

          private double[] MatrixB;

         /// <summary>

         /// 输入系数矩阵

         /// </summary>

          private void Initialize()

         {

              Console.WriteLine("请输入系数矩阵的维数:");

              n=int.Parse(Console.ReadLine());

              Value=new Double[2,n];

              Parameters=new double[n,n];

              MatrixB=new double[n];

              for(int i=0;i<n;i++)

              {

                   Console.WriteLine("请输入第"+(i+1)+"维系数");

                   for(int j=0;j<n;j++)

                   {

                        Console.Write("a("+(j+1)+")=");

                        Parameters[i,i]=double.Parse(Console.ReadLine());

                   }

              }

              Console.WriteLine("请输入B矩阵:");

              for(int i=0;i<n;i++)

              {

                   Console.Write("b("+(i+1)+")=");

                   MatrixB[i]=double.Parse(Console.ReadLine());

              }

         }

         /// <summary>

         /// 检查系数矩阵是否收敛

         /// </summary>

         /// <returns></returns>

          private bool IsConvergence()

         {

              for(int i=0;i<n;i++)

{

                   for(int j=0;j<n;j++)

{

                        if(Parameters[i,i]<=Parameters[i,j])

{

                            return false;

}

}

}

              return true;

         }

         /// <summary>

         /// 输入初值

         /// </summary>

          private void SetValue()

         {

              for(int i=0;i<n;i++)

              {

                   Console.Write("x("+i+")=");

                   Value[0,i]=double.Parse(Console.ReadLine());

                   Value[1,i]=0.0;

              }

              Console.WriteLine("请输入精度要求:");

              Precision=double.Parse(Console.ReadLine());

         }

         /// <summary>

         /// 检查迭代是否已满足精度要求

         /// </summary>

         /// <returns></returns>

          private bool IsPrecision()

         {

              for(int i=0;i<n;i++)

{

                   if(Math.Abs(Value[0,i]-Value[1,i])>Precision)

{

                       return false;

}

}

              return true;

         }

         /// <summary>

         /// 迭代过程

         /// </summary>

         /// <returns></returns>

          private void Iterate()

         {

              for(int i=0;i<n;i++)

              {

                   if(IsPrecision()==false)

                   {

                        for(int j=0;j<n;j++)

                       {

                            if(j!=i)

    {

                                Value[i%2,i]+=-Parameters[i,j]/Parameters[i,i]*Value[(i+1)%2,j];

    }

                       }

                        Value[i%2,i]+=MatrixB[i];

                   }

              }

         }

         /// <summary>

         /// 计算Jacobi迭代结果

         /// </summary>

         /// <returns></returns>

          private void calculate()

         {

              if(IsConvergence()==true)

{

                   Iterate();

}

              else

{

                   Console.WriteLine("您输入的方程组的Jacobi迭代式不收敛!");

}

         }

         /// <summary>

         /// 返回迭代结果

         /// </summary>

          private void Result()

         {

              if(IsConvergence()==true)

              {

                   for(int i=0;i<n;i++)

{

                        Console.WriteLine("x("+i+")="+Value[1,i]);

}

              }

         }

         /// <summary>

         /// 应用程序的主入口点。

         /// </summary>

          [STAThread]

         static void Main(string[] args)

         {

              Jacobi Jacobi1=new Jacobi();

              Jacobi1.Initialize();

              Jacobi1.calculate();

              Jacobi1.Result();

              Console.Read();

         }

     }

}

 

本文地址:http://com.8s8s.com/it/it44499.htm