Matlab hw
ME202 Dr. Charles Negus
Computer Programming Applications for Mechanical Engineers Head GSI: Timmy Siauw H
HW06: Linear Algebra
1. (5 points) Do Problem 12.8 (myNumSols) in Siauw and Bayen
2. (10 points) Write a function with the header [U, b2, L] = myForwardSweep(A,b) which
performs systematic linear transformation on the augmented matrix [A|b]. Note that this function
should return not only the transformed A and b, but also a matrix containing -m(i, j) used in the
transformation but also with 1’s on its diagonal. Recall that m(i, j) = -A(i, j) / A(j, j)
Since m will only populate with elements below the diagonal, you should first initialize m with nxn
zeros.
Test Cases: >> format short
>> A = [815 98 158 142 656;
906 279 971 422 36;
127 547 958 916 850;
914 958 486 793 934;
633 965 801 960 679];
>> b = [5333; 6245; 12009; 12130; 12201];
>> [U, b2, L] = myForwardSweep(A,b)
U =
1.0e+03 *
0.8150 0.0980 0.1580 0.1420 0.6560
0 0.1701 0.7954 0.2641 -0.6932
0.0000 0 -1.5535 0.0680 2.9154
0 0 0 -0.8436 -3.2086
0 0 0 0 -0.1374
b2 =
1.0e+04 *
0.5333
0.0317
1.0188
-1.9418
-0.0687
ME202 Dr. Charles Negus
Computer Programming Applications for Mechanical Engineers Head GSI: Timmy Siauw H
L =
1.0000 0 0 0 0
1.1117 1.0000 0 0 0
0.1558 3.1268 1.0000 0 0
1.1215 4.9871 2.3545 1.0000 0
0.7767 5.2270 2.2395 0.8098 1.0000
>> L*U
ans =
815.0000 98.0000 158.0000 142.0000 656.0000
906.0000 279.0000 971.0000 422.0000 36.0000
127.0000 547.0000 958.0000 916.0000 850.0000
914.0000 958.0000 486.0000 793.0000 934.0000
633.0000 965.0000 801.0000 960.0000 679.0000
3. (10 points) An upper triangular matrix is a square matrix with all zeros BELOW the diagonal elements.
Write a function with header [x] = myBackSub(U, b) which solves Ax = b for x given an nxn
upper triangular matrix A and an nx1 vector b. Use nested for loops, do not use built in Matlab
functions inv, pinv, \.
Test Case: >> x = myBackSub(U, b2)
x =
1.0000
2.0000
3.0000
4.0000
5.0000
4. (10 points) Write a function with the header [y] = mySub(L, b) which solves Ly=b for y given an
nxn lower triangular matrix L and an nx1 vector b. Use nested for loops, do not use built in Matlab
functions, inv, pinv, \.
Test Case: >> L
L =
1.0000 0 0 0 0
1.1117 1.0000 0 0 0
0.1558 3.1268 1.0000 0 0
1.1215 4.9871 2.3545 1.0000 0
0.7767 5.2270 2.2395 0.8098 1.0000
ME202 Dr. Charles Negus
Computer Programming Applications for Mechanical Engineers Head GSI: Timmy Siauw H
>> bNew = [10666; 12490; 24018; 24260; 24402];
>> y = mySub(L, bNew)
y =
1.0e+04 *
1.0666
0.0633
2.0376
-3.8835
-0.1374
5. (5 points) Write a function with the header [x] = myLU(L, U, b) which solves LUx=b for a
given lower triangular matrix L, upper triangular matrix U, and right hand side vector b. The function
body should only have two lines of code consisting of calls to two of the functions you wrote earlier.
Test Case: >> x = myLU(L, U, bNew)
x =
2.0000
4.0000
6.0000
8.0000
10.0000
>> x = myLU(L, U, b)
x =
1.0000
2.0000
3.0000
4.0000
5.0000
Deliverables: Submit the above m-files (separately, not zipped) onto Blackboard. • Be sure that the functions are named exactly as specified, including spelling and case. • You will get zero credit for a function if it is named wrong, even if it works.