Write a function with the header [m, U, x] = mySweep(A,b) which performs systema
ID: 3699065 • Letter: W
Question
Write a function with the header [m, U, x] = mySweep(A,b) which performs systematic linear transformation on the system Ax=b (the forward sweep). Note that this function should return not only the transformed A and b, but also a matrix m 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];
>> [L, U, c] = mySweep(A,b)
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
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 c = 1.0e+04 * 0.5333 0.0317 1.0188 -1.9418 -0.0687
Explanation / Answer
ANS:-
code:
function [X,y,i] = mySweep(X,y)
Xy = [X y];
i = zeros(length(X));
for n = 1:length(X)-1
for m = n+1:length(X)
i(m, n) = -(Xy(m, m)/Xy(n, n));
Xy(m,:) = Xy(m,:)+(Xy(n, :)*i(m, n));
end
end
y = Xy(:,end);
X = Xy(:,1:end-1);
Xy
end
Output:
>> mySweep(13, 8)
Xy =
13 8
ans =
13
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.