Linear System Solve in MATLAB

3 vues (au cours des 30 derniers jours)
Taehun Kim
Taehun Kim le 21 Mar 2022
Modifié(e) : Bruno Luong le 22 Mar 2022
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 commentaires
John D'Errico
John D'Errico le 21 Mar 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
Taehun Kim le 21 Mar 2022
Thanks John for your insightful comment! I was worried about this but your comment really gave me some sense of relief and assurance! I did run some small test as below where I applied to one of the answers! It seems like the performance hit is there but not too much significant as I was worrying over about it! Many thanks for your help! :)

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 21 Mar 2022
Modifié(e) : Matt J le 21 Mar 2022
So, I have n (let's say around 200) equations...I am worried if the sequential solve would be very slow when comapred against the backslash?
The two are not comparable because backslash cannot handle non-linear equations, which is what you have. In any case, the performance impact due to a 200-iteration loop seems insiginificant. If the b_i() are very simple the loop will run very fast and 200 steps is nothing. If the b_i() are complex, then the overhead of the loop will be insignificant next to the nonlinear operations anyway.
  3 commentaires
Matt J
Matt J le 21 Mar 2022
Modifié(e) : Matt J le 21 Mar 2022
Does this sound like a "light computation"?
Hard to say, because your example doesn't illustrate the nonlinear part of your computation. But as I said also, it doesn't matter whether the computation is light or heavy. In either case, it won't be the for-loop that is slowing you down.
Taehun Kim
Taehun Kim le 21 Mar 2022
Ah, I see... Thanks Matt for the insightful answer and sharing your experience! I really appreciate it!

Connectez-vous pour commenter.

Plus de réponses (1)

Steven Lord
Steven Lord le 21 Mar 2022
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
Elapsed time is 3.731868 seconds.
tic
D = decomposition(A);
for k = 1:width(b)
x2(:, k) = D\b(:, k);
end
toc
Elapsed time is 0.216534 seconds.
  11 commentaires
Steven Lord
Steven Lord le 22 Mar 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
Bruno Luong le 22 Mar 2022
Modifié(e) : Bruno Luong le 22 Mar 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Linear Algebra dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by