2019年5月28日 星期二

C語言 例題6-8 LU分解法求線性代數解

C語言 例題6-8 LU分解法求線性代數解

Solve the following system of equations using LU Decomposition method:
   \begin{equation*} x_1 + x_2 + x_3 = 1 \end{equation*} \begin{equation*} 4x_1 + 3x_2 - x_3 = 6  \end{equation*} \begin{equation*} 3x_1 + 5x_2 + 3x_3 = 4 \end{equation*}


Solution: Here, we have
A = \begin{bmatrix}   1 & 1 & 1 \\   4 & 3 & -1 \\   3 & 5 & 3  \end{bmatrix} , X = \begin{bmatrix}   x_1 \\   x_2 \\   x_3  \end{bmatrix}  and  C = \begin{bmatrix}   1 \\   6 \\   4  \end{bmatrix} such that A X = C.


Mathematics | L U Decomposition of a System of Linear Equations

源自於 https://www.geeksforgeeks.org/l-u-decomposition-system-linear-equations/
Example:
Solve the following system of equations using LU Decomposition method:
   \begin{equation*} x_1 + x_2 + x_3 = 1 \end{equation*} \begin{equation*} 4x_1 + 3x_2 - x_3 = 6  \end{equation*} \begin{equation*} 3x_1 + 5x_2 + 3x_3 = 4 \end{equation*}


Solution: Here, we have
A = \begin{bmatrix}   1 & 1 & 1 \\   4 & 3 & -1 \\   3 & 5 & 3  \end{bmatrix} , X = \begin{bmatrix}   x_1 \\   x_2 \\   x_3  \end{bmatrix}  and  C = \begin{bmatrix}   1 \\   6 \\   4  \end{bmatrix} such that A X = C.
Now, we first consider \begin{bmatrix}   1 & 1 & 1 \\   4 & 3 & -1 \\   3 & 5 & 3  \end{bmatrix} and convert it to row echelon form using Gauss Elimination Method.
So, by doing
(1)  \begin{equation*} R_2 \to R_2 - 4R_1  \end{equation*}
(2)  \begin{equation*} R_3 \to R_3 - 3R_1   \end{equation*}
we get
\begin{bmatrix}   1 & 1 & 1 \\   4 & 3 & -1 \\   3 & 5 & 3  \end{bmatrix} \sim  \begin{bmatrix}  1 & 1 & 1 \\   0 & -1 & -5 \\   0 & 2 & 0  \end{bmatrix}
Now, by doing
(3)  \begin{equation*} R_3 \to R_3 - (-2)R_2 \end{equation*}


we get
 \sim \begin{bmatrix}   1 & 1 & 1 \\   0 & -1 & -5 \\   0 & 0 & -10  \end{bmatrix}
(Remember to always keep ‘ – ‘ sign in between, replace ‘ + ‘ sign by two ‘ – ‘ signs)
Hence, we get L =  \begin{bmatrix}   1 & 0 & 0 \\   4 & 1 & 0 \\   3 & -2 & 1  \end{bmatrix} and U =  \begin{bmatrix}   1 & 1 & 1 \\   0 & -1 & -5 \\   0 & 0 & -10  \end{bmatrix}
(notice that in L matrix,  l_{21} = 4  is from (1),  l_{31} = 3  is from (2) and  l_{32} = -2  is from (3))
Now, we assume Z  = \begin{bmatrix}   z_1 \\   z_2 \\   z_3   \end{bmatrix} and solve L Z = C.
 \begin{bmatrix}   1 & 0 & 0 \\   4 & 1 & 0 \\   3 & -2 & 1  \end{bmatrix}  \begin{bmatrix}   z_1 \\   z_2 \\   z_3   \end{bmatrix}  = \begin{bmatrix}   1 \\   6 \\   4  \end{bmatrix}
So, we have  z_1 = 1 ,   4z_1 + z_2 = 6 ,   3z_1 - 2z_2 + z_3 = 4  .
Solving, we get  z_1 =  1  z_2 = 2  and  z_3 = 5  .
Now, we solve U X = Z


 \begin{bmatrix}   1 & 1 & 1 \\   0 & -1 & -5 \\   0 & 0 & -10  \end{bmatrix} \begin{bmatrix}   x_1 \\   x_2 \\   x_3  \end{bmatrix}  = \begin{bmatrix}   1 \\   2 \\   5  \end{bmatrix}
Therefore, we get  x_1 + x_2 + x_3 = 1  ,  -x_2 - 5x_3 = 2 , -10x_3 = 5 .
Thus, the solution to the given system of linear equations is  x_1 = 1   x_2 = 0.5   x_3 =  -0.5  and hence the matrix X = \begin{bmatrix}   1 \\   0.5 \\   -0.5  \end{bmatrix}

/************** LU Decomposition for solving linear equations ***********/
/*
[A] = [L][U]
where, L is the lower triangular matrix and
U is the upper triangular matrix.
The elements of these three matrices row-wise are:

[A] = { a11, a12, a13,
        a21, a22, a23,
        a31, a32, a33 }

[L] = {  1,   0,  0,
       l21,   1,  0,
       l31, l32,  1}
      
[U] = {u11, u12, u13,
       0  , u22, u23,
       0  ,   0, u33}


-1.00x1    1.00x2  2.00x3  = 2.00
 3.00x1   -1.00x2  1.00x3  = 6.00
-1.00x1    3.00x2  4.00x3  = 4.00

x3 = 2.000   x2 = -1.000   x1 = 1.000
*/

#include<stdio.h>
#include<math.h>
int main()
{
    int n,i,k,j,p;
    float l[10][10]={0},u[10][10]={0},sum,z[10]={0},x[10]={0};
    n=3;
  
    printf("The order of square matrix: %2d\n",n);
    float a[3][3]= { {  1,  1,  1 },
                     {  4,  3, -1 },
                     {  3,  5,  3 } };
                    
  
    float b[3]= {1,6,4};
  
    for(i=0;i<n;i++)
    {
        printf("\nRow%1d\t",i);
        for(j=0;j<n;j++)
            printf("%0.2lf\t\t",a[i][j]);

    }
  
    printf("\n\nelements of b matrix\n");
    for(i=0;i<n;i++)
        printf("%0.2lf\t\t",b[i]);
      
    //********** LU decomposition *****//
    for(k=0;k<n;k++)
    {
        u[k][k]=1;
        for(i=k;i<n;i++)
        {
            sum=0;
            for(p=0;p<k;p++)
                sum+=l[i][p]*u[p][k];
            l[i][k]=a[i][k]-sum;
        }

        for(j=k+1;j<n;j++)
        {
            sum=0;
            for(p=0;p<k;p++)
                sum+=l[k][p]*u[p][j];
            u[k][j]=(a[k][j]-sum)/l[k][k];
        }
    }
    
    //******** Displaying LU matrix**********//
    printf("\n\nLU matrix is : \n");
    //==================L matrix===============
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
            printf("%0.2lf\t",l[i][j]);
        printf("\n");
    }
    printf("\n\n");
    //==================U matrix===============
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
            printf("%0.2lf\t",u[i][j]);
        printf("\n");
    }

    //***** FINDING Z; LZ=b*********//

    for(i=0;i<n;i++)
    {                                        //forward subtitution method
        sum=0;
        for(p=0;p<i;p++)
        sum+=l[i][p]*z[p];
        z[i]=(b[i]-sum)/l[i][i];
    }
    //********** FINDING X; UX=Z***********//

    for(i=n-1;i>=0;i--)
    {
        sum=0;
        for(p=n-1;p>=i;p--)
            sum+=u[i][p]*x[p];
        x[i]=(z[i]-sum)/u[i][i];
    }
    //*********** DISPLAYING SOLUTION**************//
    printf("\n\nSet of solution is\n");
    for(i=0;i<n;i++)
        printf("%0.2lf\t",x[i]);

    return 0;
}

輸出畫面
The order of square matrix:  3

Row0 1.00 1.00 1.00
Row1 4.00 3.00 -1.00
Row2 3.00 5.00 3.00

elements of b matrix
1.00 6.00 4.00

LU matrix is : 
1.00 0.00 0.00
4.00 -1.00 0.00
3.00 2.00 -10.00


1.00 1.00 1.00
0.00 1.00 5.00
0.00 0.00 1.00


Set of solution is
1.00 0.50 -0.50

沒有留言:

張貼留言

Messaging API作為替代方案

  LINE超好用功能要沒了!LINE Notify明年3月底終止服務,有什麼替代方案? LINE Notify將於2025年3月31日結束服務,官方建議改用Messaging API作為替代方案。 //CHANNEL_ACCESS_TOKEN = 'Messaging ...