2019年5月14日 星期二

C語言 例題6-1(d) 高斯消去法 Gauss Elimination

C語言 例題6-1(d) 高斯消去法 Gauss Elimination


 4.01  x1 + 1.23 x2   + 1.43 x3  -  0.73  x4 = 5.94
 1.23  x1 + 7.41 x2   + 2.41 x3  +  3.02 x4 = 14.07
 1.43  x1 + 2.41 x2   + 5.79  x3 -  1.11  x4 = 8.52
-0.73  x1 + 3.02 x2   -  1.11  x3 +  6.41 x4 = 7.59
/*
 Gauss Elimination Method in C.
 For this, let us first consider the following three equations:

 a1x + b1y + c1z = d1
 a2x + b2y + c2z = d2
 a3x + b3y + c3z = d3

 Assuming a1 ≠ 0, x is eliminated from the second equation
 by subtracting (a2/ a1) times the first equation
 from the second equation.

 In the same way, the C code presented here eliminates x
 from third equation by subtracting (a3/a1) times the first
 equation from the third equation.

 Then we get the new equations as:

 a1x + b1y + c1z = d1
 b’2y + c’2z = d’2
 c’’3z = d’’3

 The elimination procedure is continued until only one
 unknown remains in the last equation.

 After its value is determined, the procedure is stopped.
 Now, Gauss Elimination in C uses back substitution to get
 the values of x, y and z as:

 z=  d’’3 / c’’3
 y=(d’2 – c’2z) / b’2
 x=( d1- c1z- b1y)/ a1
*/

#include <stdio.h>
#include <math.h>
int main(void)
{
    int i,j,k,n;
    n=4;    //the order of matrix:
    float c,x[n+1],sum=0.0;

    //the elements of augmented matrix row-wise
    float A[4][5]= { { 4.01  , 1.23 ,  1.43 , -0.73 ,  5.94 } ,
                     { 1.23  , 7.41 ,  2.41 ,  3.02 , 14.07 } ,
                     { 1.43  , 2.41 ,  5.79 , -1.11 ,  8.52 } ,
                     { -0.73 , 3.02 , -1.11 ,  6.41 ,  7.59 } };
 
    for(j=0; j<n; j++) /* loop for the generation of upper triangular matrix*/
    {
        for(i=0; i<n; i++)
        {
            if(i>j)
            {
                c=(A[i][j])/ (A[j][j]);
                printf("%d,%d, %0.2lf   \t--> \t ",j,i, c);
                for(k=0; k<=n; k++)
                {
                    A[i][k]=(A[i][k]) -c* (A[j][k]);
                    printf("%0.2lf\t ", A[i][k]);
                }
                printf("\n");
            }
        }
    }
    x[n-1]=A[n-1][n]/A[n-1][n-1];
 
    printf("x[%d]=%0.2lf",n-1,x[n-1]);
 
    /* this loop is for backward substitution*/
    for(i=n-2; i>=0; i--)
    {
        sum=0;
        for(j=i; j<n; j++)
        {
            sum=sum+A[i][j]*x[j];
        }
        x[i]=(A[i][n]-sum)/A[i][i];
    }
    printf("\nThe solution is: \n");
    for(i=0; i<=n-1; i++)
    {
        printf("\nx%d=\t  %0.2lf\t",i,x[i]); /* x1, x2, x3 are the required solutions*/
    }
    return(0);
}


輸出畫面
0,1, 0.31    --> 0.00 7.03 1.97 3.24 12.25
0,2, 0.36    --> 0.00 1.97 5.28 -0.85 6.40
0,3, -0.18    --> 0.00 3.24 -0.85 6.28 8.67
1,2, 0.28    --> 0.00 0.00 4.73 -1.76 2.97
1,3, 0.46    --> 0.00 0.00 -1.76 4.78 3.02
2,3, -0.37    --> 0.00 0.00 -0.00 4.13 4.13
x[3]=1.00
The solution is:

x0=   -nan
x1=   1.00
x2=   1.00

x3=   1.00

沒有留言:

張貼留言

Messaging API作為替代方案

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