Changing for loop to backslash
3 ビュー (過去 30 日間)
古いコメントを表示
How do I change this code to not have a for loop. Hint: one backslash with multiple right-hand sides, solved simultaneously.
% Create a matrix of the coefficients
A = [6 -1 0 0 0; -3 3 0 0 0; 0 -1 9 0 0; 0 -1 -8 11 -2; -3 -1 0 0 4];
% Compute the LU Factorization
[L,U,P] = lu(A);
% Loop for each value of M from 10 to 100 with increments of 10
for M = 10 : 10 : 100
% Create a column vector of right hand side values
b = [M; 0; 160; 0; 0];
% Use the factors from LU factorization to solve
% two triangular linear systems
y = L\(P*b);
c = U\y;
% Print the solution of system of equation for current value of M
fprintf('\nSolution of system of equation for M = %d is\n', M);
disp(c);
end
0 件のコメント
採用された回答
Torsten
2022 年 9 月 14 日
編集済み: Torsten
2022 年 9 月 14 日
A = [6 -1 0 0 0; -3 3 0 0 0; 0 -1 9 0 0; 0 -1 -8 11 -2; -3 -1 0 0 4];
b = [10:10:100;repmat([0; 160; 0; 0],1,10)];
dA = decomposition(A);
tic
for i=1:100000
c = dA\b;
end
toc
or
A = [6 -1 0 0 0; -3 3 0 0 0; 0 -1 9 0 0; 0 -1 -8 11 -2; -3 -1 0 0 4];
b = [10:10:100;repmat([0; 160; 0; 0],1,10)];
[L,U,P] = lu(A);
tic
for i=1:100000
y = L\(P*b);
c = U\y;
end
toc
0 件のコメント
その他の回答 (0 件)
参考
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!