Math II midterm - Prum Soriya

Hot

Post Top Ad

Sunday, 23 October 2022

Math II midterm

 I. ចូរសរសេរកូដ (ក្រៅពី Matlab) ដើម្បីគណនានិងដោះស្រាយ៖

     ១) ដេទែមីណង់ (Determinant)

  • Code 
#include <stdio.h>
#include <stdlib.h>

int m, n;
int det(int B[m][n]);
int main() {
    int determinant, row, column;
    printf("Enter rows and column: ");
    scanf("%d %d",&m, &n);
    int A[m][n];
    printf("Enter matrix elements: \n");
    for(row = 0; row < m; row++)
        for(column = 0; column < n; column++)
            scanf("%d",&A[row][column]);  
    determinant = det(A);
    printf("determinant = %d \n",determinant);
    return 0;
}
int det(int B[m][n]) {
    int row_size = m;
    int column_size = n;
    if (row_size != column_size) {
        printf("DimensionError: Operation Not Permitted \n");
        exit(1);
    }
    else if (row_size == 1)
        return (B[0][0]);
    else if (row_size == 2)
        return (B[0][0]*B[1][1] - B[1][0]*B[0][1]);
    else {
        int minor[row_size-1][column_size-1];
        int row_minor, column_minor;
        int firstrow_columnindex;
        int sum = 0;
        int row,column;
        for(firstrow_columnindex = 0; firstrow_columnindex < row_size;
                firstrow_columnindex++) {
            row_minor = 0;
            for(row = 1; row < row_size; row++) {
                column_minor = 0;
                for(column = 0; column < column_size; column++) {
                    if (column == firstrow_columnindex)
                        continue;
                    else
                        minor[row_minor][column_minor] = B[row][column];
                    column_minor++;
                }
                row_minor++;
            }
            m = row_minor;
            n = column_minor;
            if (firstrow_columnindex % 2 == 0)
                sum += B[0][firstrow_columnindex] * det(minor);
            else
                sum -= B[0][firstrow_columnindex] * det(minor);

        }
        return sum;
    }
}  
  • Result  

     
       
    ២) ម៉ាទ្រីសច្រាស (Inverse matrix)
  •   Code

 #include<stdio.h>
#include<math.h>

void cofactor(float [][25], float);
float determinant(float [][25], float);
void transpose(float [][25], float [][25], float);
int main()
{
  float a[25][25], n, d;
  int i, j;
 
  printf("Enter the order of the Matrix: ");
  scanf("%f", &n);
  printf("Enter the elements of a matrix: \n");
  for (i = 0;i < n; i++)
    {
     for (j = 0;j < n; j++)
       {
        scanf("%f", &a[i][j]);
        }
    }
   
  d = determinant(a, n);
  if (d == 0)
   printf("Since the determinant is zerp (0), therefor inverse is not possible.");
  else
   cofactor(a, n);
}
// function for the calculation of determinant
float determinant(float a[25][25], float k)
{
  float s = 1, det = 0, b[25][25];
  int i, j, m, n, c;
  if (k == 1)
    {
     return (a[0][0]);
    }
  else
    {
     det = 0;
     for (c = 0; c < k; c++)
       {
        m = 0;
        n = 0;
        for (i = 0;i < k; i++)
          {
            for (j = 0 ;j < k; j++)
              {
                b[i][j] = 0;
                if (i != 0 && j != c)
                 {
                   b[m][n] = a[i][j];
                   if (n < (k - 2))
                    n++;
                   else
                    {
                     n = 0;
                     m++;
                     }
                   }
               }
             }
          det = det + s * (a[0][c] * determinant(b, k - 1));
          s = -1 * s;
          }
    }
 
    return (det);
}
// function for cofactor calculation
void cofactor(float num[25][25], float f)
{
 float b[25][25], fac[25][25];
 int p, q, m, n, i, j;
 for (q = 0;q < f; q++)
 {
   for (p = 0;p < f; p++)
    {
     m = 0;
     n = 0;
     for (i = 0;i < f; i++)
     {
       for (j = 0;j < f; j++)
        {
          if (i != q && j != p)
          {
            b[m][n] = num[i][j];
            if (n < (f - 2))
             n++;
            else
             {
               n = 0;
               m++;
               }
            }
        }
      }
      fac[q][p] = pow(-1, q + p) * determinant(b, f - 1);
    }
  }
  transpose(num, fac, f);
}
///function to find the transpose of a matrix
void transpose(float num[25][25], float fac[25][25], float r)
{
  int i, j;
  float b[25][25], inverse[25][25], d;
  for (i = 0;i < r; i++)
  {
     for (j = 0;j < r; j++)
       {
           b[i][j] = fac[j][i];
       }
  }
  d = determinant(num, r);
  for (i = 0;i < r; i++)
  {
     for (j = 0;j < r; j++)
     {
        inverse[i][j] = b[i][j] / d;
     }
  }
  printf("The inverse of matrix: \n");
  for (i = 0;i < r; i++)
  {
     for (j = 0;j < r; j++)
     {
        printf("\t %f", inverse[i][j]);
     }
     printf("\n");
  }
}
  • Result



    ៣) Gaussian elimination

  •     Code
#include<stdio.h>

int main()
{
    int i, j, k, n;
    float A[20][20], c, x[10], sum=0.0;
    printf("Enter the order of matrix: ");
    scanf("%d",&n);
    printf("Enter the elements of augmented matrix row-wise: \n");
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=(n+1); j++)
        {
            printf("A[%d][%d] : ", i, j);
            scanf("%f",&A[i][j]);
        }
    }
    for(j=1; j<=n; j++)
    {
        for(i=1; i<=n; i++)
        {
            if(i>j)
            {
                c = A[i][j] / A[j][j];
                for(k=1; k<=n+1; k++)
                {
                    A[i][k] = A[i][k]- c*A[j][k];
                }
            }
        }
    }
    x[n] = A[n][n+1] / A[n][n];
    for(i=n-1; i>=1; i--)
    {
        sum = 0;
        for(j=i+1; j<=n; j++)
        {
            sum = sum+A[i][j] * x[j];
        }
        x[i] = (A[i][n+1]-sum) / A[i][i];
    }
    printf("The solution is: ");
    for(i=1; i<=n; i++)
    {
        printf("\t\n x%d=%f\t", i, x[i]);
    }
    return(0);
}

  • Result


    ៤) Iteration method

  • Code

#include<stdio.h>
#include<conio.h>
#include<math.h>
/* we are solving
   x + y + z = 7
   x - y + 2z = 9
   2x + y - z = 1
*/
/* Arranging given system of linear
   equations in diagonally dominant
   form:
   2x + y - z = 1
   x + y + z = 7
   2x + y - z = 9
*/
/* Equations:
   x = (1 - y + z)
   y = (7 - x - z)
   z = (-9 + 2x + y)
*/
#define f1(x,y,z)  (1 - y + z)
#define f2(x,y,z)  (7 - x - z)
#define f3(x,y,z)  (-9 - 2*x + y)
int main()
{
    float x0 = 0, y0 = 0, z0 = 0, x1, y1, z1, e1, e2, e3, e;
    int count=10;
    printf("Enter tolerable error:\n");
    scanf("%f", &e);
    printf("\nCount\tx\ty\tz\n");
    do
    {
    /* Calculation */
    x1 = f1(x0,y0,z0);
    y1 = f2(x0,y0,z0);
    z1 = f3(x0,y0,z0);
    printf("%d\t%0.4f\t%0.4f\t%0.4f\n",count, x1,y1,z1);
    /* Error */
    e1 = fabs(x0-x1);
    e2 = fabs(y0-y1);
    e3 = fabs(z0-z1);
    count++;
    /* Set value for next iteration */
    x0 = x1;
    y0 = y1;
    z0 = z1;
    }while(e1>e && e2>e && e3>e);
    printf("\nSolution: x=%0.3f, y=%0.3f and z = %0.3f\n",x1,y1,z1);
    getch();
    return 0;
}

  • Result


II. ចូរដោះស្រាយរកឬសប្រព័ន្ធសមីការខាងក្រោម តាមវិធាន៖
    ១) Cramer's rule

            
           

        ២) Inverse matrix


        ៣) Gaussian elimination






















































No comments:

Post a Comment

ISP Network Design Research Project

Post Top Ad