Advertisement
tampurus

Unit 2.1 Gauss Elemination Method

May 26th, 2022 (edited)
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.03 KB | None | 0 0
  1. //Mera takrika
  2.  
  3. #include <stdio.h>
  4. void p(){
  5.     printf("\n");
  6. }
  7. int main()
  8. {
  9.     int n,i,j,k;
  10.     float c,a[5][5],x[10];
  11.  
  12.     printf("Enter the value of n\n");
  13.     scanf("%d",&n);
  14.     for(i=1 ; i<=n ; i++){
  15.         for(j=1 ; j<=n+1 ; j++){
  16.             scanf("%f",&a[i][j]);
  17.         }
  18.     }
  19.     for(i=1 ; i<=n ; i++){
  20.         for(j=1 ; j<=n ; j++){
  21.             if(i<j){
  22.                 c = a[j][i]/a[i][i];
  23.                 for(k=1 ; k<=n+1 ; k++){
  24.                     a[j][k] = a[j][k] - (c*a[i][k]);
  25.                 }
  26.             }
  27.         }
  28.     }
  29.     p();
  30.     for(i=1 ; i<=n ; i++){
  31.         for(j=1 ; j<=n+1 ; j++){
  32.             printf("%f ",a[i][j]);
  33.         }
  34.         p();
  35.     }
  36.     // now doing back substitution
  37.    
  38.     x[3] = a[3][4]/a[3][3];
  39.     x[2] = (a[2][4]- (x[3] * a[2][3]))/a[2][2];
  40.     x[1] = (a[1][4] - ( (x[2]*a[1][2]) + (x[3]*a[1][3]) )) /a[1][1];
  41.    
  42.     for(i=1;i<=n;i++)
  43.     {
  44.         printf("\nx[%d] = %f",i,x[i]);
  45.     }
  46.     return 0;
  47. }
  48.  
  49. //Mam ka tarika
  50.  
  51. // AFTER CHANGES
  52.  
  53. // Algorithm
  54. start
  55. declare the variables and read the order of the matrix n
  56. take the cofficients of the linear equation as
  57.  
  58. Do for i=1 to n
  59.     Do for j=1 to n+1
  60.         read a[i][j]
  61.     end for j
  62. end for i
  63.  
  64. Do for i=1 to n-1
  65.     Do for j=i to n
  66.         Do for k=i+1 to n+1
  67.             a[j][k] = a[j][k]- ((a[j][k]/a[i][i]) * a[i][k])
  68.         End for k
  69.     End for j
  70. End for i
  71.  
  72. Compute x[n] = a[n][n+1]/a[n][n]
  73.  
  74. Do for i=n-1 to 1
  75.     sum =0
  76.     Do for j = i+1 to n
  77.         sum = sum + a[i][j] * x[j]
  78.     End for j
  79.     x[i] = 1/a[i][i] * (a[i][n+1]-sum)
  80. End for i
  81. Display the result x[k]
  82. Stop
  83.  
  84.  
  85. // Program
  86.  
  87. #include <stdio.h>
  88.  
  89. #include <stdio.h>
  90. int main()
  91. {
  92.     int n,i,j,k;
  93.     float sum=0,a[5][5],x[10];
  94.  
  95.     printf("Enter the value of n\n");
  96.     scanf("%d",&n);
  97.     for(i=1 ; i<=n ; i++){
  98.         for(j=1 ; j<=n+1 ; j++){
  99.             scanf("%f",&a[i][j]);
  100.         }
  101.     }
  102.  
  103.     for(i=1 ; i<n ; i++){
  104.         for(j=i+1 ; j<=n ; j++){
  105.             for(k=i+1 ; k<=n+1 ; k++){
  106.                 a[j][k] = a[j][k] - ((a[j][i]/a[i][i]) * a[i][k]);
  107.             }
  108.         }
  109.     }
  110.     x[n]=a[n][n+1]/a[n][n];
  111.     for(i=n-1;i>=1;i--)
  112.     {
  113.         sum=0;
  114.         for(j=i+1;j<=n;j++)
  115.         {
  116.             sum=sum+(a[i][j]*x[j]);
  117.         }
  118.         x[i]=(1/a[i][i])*((a[i][n+1]-sum));
  119.     }
  120.    
  121.     for(i=1;i<=n;i++)
  122.     {
  123.         printf("\nx[%d] = %f",i,x[i]);
  124.     }
  125.    
  126.     return 0;
  127. }
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134. // BEFORE CHANGES
  135.  
  136. // Algorithm
  137. start
  138. declare the variables and read the order of the matrix n
  139. take the cofficients of the linear equation as
  140.  
  141. Do for k=1 to n
  142.     Do for j=1 to n+1
  143.         read a[k][j]
  144.     end for j
  145. end for k
  146.  
  147. Do for k=1 to n-1
  148.     Do for i=k to n
  149.         Do for j=k+1 to n+1
  150.             a[i][j] = a[i][j]- ((a[i][j]/a[k][k]) * a[k][j])
  151.         End for j
  152.     End for i
  153. End for k
  154.  
  155. Compute x[n] = a[n][n+1]/a[n][n]
  156.  
  157. Do for k=n-1 to 1
  158.     sum =0
  159.     Do for j = k+1 to n
  160.         sum = sum + a[k][j] * x[j]
  161.     End for j
  162.     x[k] = 1/a[k][k] * (a[k][n+1]-sum)
  163. End for k
  164. Display the result x[k]
  165. Stop
  166.  
  167.  
  168. // Program
  169.  
  170. #include <stdio.h>
  171.  
  172. #include <stdio.h>
  173. int main()
  174. {
  175.     int n,i,j,k;
  176.     float sum=0,a[5][5],x[10];
  177.  
  178.     printf("Enter the value of n\n");
  179.     scanf("%d",&n);
  180.     for(k=1 ; k<=n ; k++){
  181.         for(j=1 ; j<=n+1 ; j++){
  182.             scanf("%f",&a[k][j]);
  183.         }
  184.     }
  185.  
  186.     for(k=1 ; k<n ; k++){
  187.         for(i=k+1 ; i<=n ; i++){
  188.             for(j=k+1 ; j<=n+1 ; j++){
  189.                 a[i][j] = a[i][j] - ((a[i][k]/a[k][k]) * a[k][j]);
  190.             }
  191.         }
  192.     }
  193.     x[n]=a[n][n+1]/a[n][n];
  194.     for(k=n-1;k>=1;k--)
  195.     {
  196.         sum=0;
  197.         for(j=k+1;j<=n;j++)
  198.         {
  199.             sum=sum+(a[k][j]*x[j]);
  200.         }
  201.         x[k]=(1/a[k][k])*((a[k][n+1]-sum));
  202.     }
  203.    
  204.     for(k=1;k<=n;k++)
  205.     {
  206.         printf("\nx[%d] = %f",k,x[k]);
  207.     }
  208.    
  209.     return 0;
  210. }
  211.  
  212. /*
  213. Enter the value of n
  214. 3
  215. 2 1 4 12
  216. 8 -3 2 23
  217. 4 11 -1 33
  218. x[1] = 3.357143
  219. x[2] = 1.857143
  220. x[3] = 0.857143
  221. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement