Linear System Solve in MATLAB
Mostrar comentarios más antiguos
Dear all,
Thanks in advance for your help!
I am dealing with a di-banded lower triangular system where the full right hand side vector is not available to begin with. For instance, I would solve the first equation and the right hand side of the second equation will depend on the solution from the first equation.
I understand that we could use for-loop to loop over this but it seems like the computational expense would be much worse, when compares with the vectorized version...
Do you think the performance will suffer greatly if we do for-loop in this case?
Thanks for your help once again!
Sincerely,
Tae
4 comentarios
John D'Errico
el 21 de Mzo. de 2022
Not totally clear.
You have a banded matrix A. Is A VERY large? Is it stored in sparse form? How large is the matrix A? How much time was required to perform the initial solve?
You solve the problem A*x = b1, where b1 is known. Given the solutino to this problem, you create a new right hand side vector, call it b2, and then solve the problem, with the same matrix A, where A*y = b2.
Is that correct?
Taehun Kim
el 21 de Mzo. de 2022
Editada: Taehun Kim
el 21 de Mzo. de 2022
John D'Errico
el 21 de Mzo. de 2022
But b_n is a nonlinear function of x_(n-1). So regardless of whether it might be slow or fast, I don't see you having any choice. The looping time is almost irrelevant. If you are worried about that, use the profiling tool to look at where the time is spent. I would conjecture it will be mostly in the nonlinear solve when you need to compute bn at each iteration.
Unless you are doing this entire operation millions of times, I would just say to get a cup of coffee and enjoy the few seconds of relaxation.
Taehun Kim
el 21 de Mzo. de 2022
Respuesta aceptada
Más respuestas (1)
If you're trying to some the same linear system repeatedly for different right hand side vectors consider using decomposition on the coefficient matrix (so MATLAB performs the analysis and preparation work for your system once) and solve it repeatedly.
b = rand(200, 1e4);
A = randi([-10 10], 200);
x1 = zeros(size(b));
x2 = zeros(size(b));
tic
for k = 1:width(b)
x1(:, k) = A\b(:, k);
end
toc
tic
D = decomposition(A);
for k = 1:width(b)
x2(:, k) = D\b(:, k);
end
toc
11 comentarios
Taehun Kim
el 21 de Mzo. de 2022
Bruno Luong
el 21 de Mzo. de 2022
Editada: Bruno Luong
el 21 de Mzo. de 2022
Humm I must miss the context but reading Steve's code I though "why not just invert directly?"
b = rand(200, 1e4);
A = randi([-10 10], 200);
x1 = zeros(size(b));
x2 = zeros(size(b));
tic
for k = 1:width(b)
x1(:, k) = A\b(:, k);
end
toc
tic
x3 = A \ b;
toc
norm(x3-x1,'fro')/norm(x1,'fro')
Taehun Kim
el 21 de Mzo. de 2022
But then why not just use inv() v(despite all the warning I think it's just a bullshit, sorry TMW)
b = rand(200, 1e4);
A = randi([-10 10], 200);
x1 = zeros(size(b));
x2 = zeros(size(b));
tic
for k = 1:width(b)
x1(:, k) = A\b(:, k);
end
toc
tic
D = decomposition(A);
for k = 1:width(b)
x2(:, k) = D\b(:, k);
end
toc
x4 = zeros(size(b));
tic
E = inv(A);
for k = 1:width(b)
x4(:, k) = E*b(:, k);
end
toc
Taehun Kim
el 21 de Mzo. de 2022
Matt J
el 21 de Mzo. de 2022
I do not see how inv() helps, since the equation supposedly has the nonlinear form A*x=b(x).
Taehun Kim
el 21 de Mzo. de 2022
Taehun Kim
el 21 de Mzo. de 2022
Bruno Luong
el 21 de Mzo. de 2022
I simply comment on Steve for-loop answer with repeat A \ b for the same A, so I replace that statement by inv(A)*b which is strictly equivalent to his code and fatest so far. I don't know the context of non-linear and how the equation is discretized.
Steven Lord
el 22 de Mzo. de 2022
In my example I knew all the b vectors from the start, but if you were solving for one vector and using that solution to create the next right hand side vector you could use decomposition.
Bruno Luong
el 22 de Mzo. de 2022
Editada: Bruno Luong
el 22 de Mzo. de 2022
But inv is twice faster than decomposition. At the end when using "\" on decomposition it numerically close to applied multipy to inv. If someone disagree they ough to explain the math to me.
If for some reason inv using worse algorithm then just call
E = A \ eye(size(A); % = inv(A)
The warning of MATLAB on using inv is jist non-sense to me.
Categorías
Más información sobre Linear Algebra en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



