C#--The Jacobi Iterative Method for Solving Equations

Jacobi's method for solving equations

Iteration process
First , decompose the coefficient matrix A in the system of equations into three parts, namely: A = L+D+U , as shown in Figure 1, where D is a diagonal matrix, L is a lower triangular matrix , and U is an upper triangular matrix.
Then determine the iteration format, X^(k+1) = B *X^(k) + f , (where ^ represents the superscript, and the number in brackets is the number of iterations), as shown in Figure 2, where B is called Iterative matrix, generally denoted as J in the Jacobian iteration method . (k=0,1,...)
Then select the initial iteration vector X^(0) to start successive iterations.
The core part, iterative implementation:

public void Calcu5()
        {
            int count1 = 0, count2 = 0;
            while(true)
            {
                for(int i=0;i<n;i++)
                {
                    double sum = 0;
                    for(int j=0;j<n;j++)
                    {
                        if(j!=i)
                        {
                            sum += a[i, j] * x[j];
                        }
                    }
                    x2[i] = (a[i, n] - sum) / a[i, i];
                    if (Math.Abs(x2[i] - x[i]) < e)
                        count2++;
                }
                count1++;
                if(count1>10000)
                { Console.WriteLine("Iterative divergence!!!");break; }
                if(count2==n)
                { Console.WriteLine("Number of iterations: {0}", count2);break; }
                for(int i=0;i<n;i++)
                { x[i] = x2[i]; }
            }
        }

whole:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Jacobi iteration
{
    class Jacobi
    {
        int n;
        public int N
        {
            get { return n; }
            set { n = value; }
        }
        double[,] a;
        public double[,] A
        {
            get { return a; }
            set { a = value; }
        }
        double[] x;
        public double[] X
        {
            get { return x; }
            set { x = value; }
        }
        double e = 0.00001;
        public double E
        {
            get { return e; }
            set { e = value; }
        }
        private double[] x2;
        public double[] X2
        {
            get { return x2; }
            set { x2 = value; }
        }
        public void Input()
        {
            Console.WriteLine("Please enter the order: ");
            n = Convert.ToInt32(Console.ReadLine());
            a = new double[n, n + 1];
            x = new double[n];
            x2 = new double[N + 1];
            for (int i = 0; i < N; i++)
            {
                X[i] = 0;
            }
            Console.WriteLine("Please enter the coefficients of each line (separated by ',' or ' '): ");
            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 Calcu5()
        {
            int count1 = 0, count2 = 0;
            while(true)
            {
                for(int i=0;i<n;i++)
                {
                    double sum = 0;
                    for(int j=0;j<n;j++)
                    {
                        if(j!=i)
                        {
                            sum += a[i, j] * x[j];
                        }
                    }
                    x2[i] = (a[i, n] - sum) / a[i, i];
                    if (Math.Abs(x2[i] - x[i]) < e)
                        count2++;
                }
                count1++;
                if(count1>10000)
                { Console.WriteLine("Iterative divergence!!!");break; }
                if(count2==n)
                { Console.WriteLine("Number of iterations: {0}", count2);break; }
                for(int i=0;i<n;i++)
                { x[i] = x2[i]; }
            }
        }
        public void Output()
        {
            Console.WriteLine("The equation coefficient is: ");
            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 OutputX()
        {
            Console.WriteLine("\nThe solution to the system of equations is: ");
            for (int i = 0; i < n; i++)
            {
                Console.WriteLine("x{0}={1}", i + 1, x[i]);
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Jacobi abc = new Jacobi();
            abc.Input();
            abc.Output();
            abc.Calcu5();
            abc.OutputX();
        }
    }
}

operation result:
Please enter the order:
4Please
enter the coefficients for each row (separated by ',' or ' '):
10 -1 2 0 6
-1 11 -1 3 25
2 -1 10 -1 -11
0 3 -1 8 15
Equation coefficients For:
   10.00 -1.00 2.00 0.00 6.00
   -1.00 11.00 -1.00 3.00 25.00
    2.00 -1.00 10.00 -1.00 -11.00
    0.00 3.00 -1.00 8.00 15.00
Iterative Divergence! ! !
The solution to the system of equations is:
x1=1
x2=2
x3=-1
x4=1
Press any key to continue. . .


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325631956&siteId=291194637