Вы находитесь на странице: 1из 4

Assignment-I (ME 502)

Problem 1: Write a code for Matrix -Vector multiplication (may


 follow
 the following
 pseudo

1 2 3  1 
code). Verify the code using some numerical value, e.g., A = 4 5 6and b = 1 .
7 8 9 -1
 
Pseudocode:
Read matrix Aij and vector bi from user
For i = 1 to n
For j = 1 to n
ci = ci + Aij × bj
End (For j)
End (For i)

Problem 2: Write a code to interchange the pth and q th rows of a matrix (Am×n ). Following
pseudo code will help you in writing the code. Using the code, interchange the 1st and 3rd row
of A matrix of the earlier problem.
Pseudocode:
For i = 1 : n
For j = 1 : n
temp = apj
apj = aqj
aqj = temp
End (For j)
End (For i)

Gauss Elimination:
A set of linear equations can be written in the matrix form as Ax=b, i.e.,
    
a11 a12 · · · a1n   x1    b1 
 a21 a22 · · · a2n   x2    b2 
=
 
 .. .. ... .
..  
 .
..  .
.. 
 . .   
 

x  b 
 
a
n1 an2 ··· a nn n n

In Gauss elimination technique the augmented matrix is first converted into upper triangular
form using elementary row operations, leading Ax=b to Ux=b0 :
   
a11 a12 · · · a1n b1 a011 a012 · · · a01n b01
 a21 a22 · · · a2n b2   0 a0 · · · a0 0 
22 2n b2 
..  .
  
 .. .. .. .. .. elimination . .. . . ..
.  −−−−→  ..
 
 . . . . . . . . 
an1 an2 · · · ann bn 0 0 · · · ann b0n
0

1
Then following back substitution the unknowns can be found as
( n
)
1 0
X
0
x i = 0 bi − aij xj
aii j=i+1

Problem 3: Write a code to carry out Gauss elimination and solve the set of linear equations:
9x + 3y + 4z = 7, 4x + 3y + 4z = 8 and x + y + z = 3. Following pseudo code will help you for
the same.
Input Data Reading:
Read the augumented matrix [ A | b ]
For i =1 to 3
For j =1 to 4
Read aij
End For (j )
End For (i )
Elimination phase:
For j =1 to 3
For i =1 to 3
If i > j, then c = aij /ajj
For k =1 to 4
aik = aik − c× ajk
End For (k )
End For (i )
End For (j )
Back substitution phase:
Find x3 = a34 /a33
For i =2 to 1
SUM = 0
For j =i+1 to 3
SUM = SUM + aij xj
End For (j )
xi = (ai4 − SUM)/aii
End For (i )
Print the result:
For i =1 to 3
print xi
End For (i )

Problem 4: Using the above code determine the solution for the set of linear equations
3x1 + 3x2 − 4x3 = 4, 2x1 + 2x2 + 3x3 = 14 and 5x1 − 3x2 + x3 = 14.
( NOTE: After row operations, when the pivot element aii = 0, an error occurs in the execution
of above written code. The following algorithm must be implemented to overcome this error.)

Algorithm:

2
Before each elimination step
If aii is 0
For j=i+1 to n
If aji 6= 0, then exchange ith row and j th row and Break the for loop
Else (i.e., if aji = 0)
If j < n , then j=j+1 and continue the loop
Else (i.e., if j = n), print Matrix is singular and exit
End For (j )
End If

Partial Pivoting: We have seen that problem occurs when a pivot element turns out
to be zero. Similarly, problem may also occur if the pivot element is close to zero, i.e., is
very small compared to the other elements in that column; which eventually leads to round-off
errors. Hence, it is advantageous to find the row below the pivot containing largest element
in that column, and perform the row exchange. This is called partial pivoting. The following
example illustrates partial pivoting.

Example:

0.02x1 + 0.01x2 = 0.02


x1 + 2x2 + x3 =1
x2 + 2x3 + x4 =4
100x3 + 200x4 = 800
Augmented Matrix
Here, there are order of magnitude differences among elements in the different rows. Partial
pivoting is illustrated thorugh the following figures.

−→

Step 1: Switch R1 with R2 Step 2: Do elimination

3
−→

Step 4: Switch R2 with R3 Step 5: Do elimination

−→

Step 7: Switch R3 with R4 Step 8: Do elimination

Problem 5: Incorporate partial pivoting technique (which basically comprises of following two
nested loops) in your code. Then verify with the above problem.

1. The first loop is to compare the pivot element with the elements below it; to find if any
of these is larger than the pivot element.

2. The second loop switches the original pivot row with the one having the largest element
(if any).

Problem 6: Finally, modify your code for solving a system of n (will be specified by an user)
linear equations. The system matrix and the right hand side have to be read from a file.

Вам также может понравиться