Linear System Solve in MATLAB
3 views (last 30 days)
Show older comments
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 Comments
John D'Errico
on 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.
Accepted Answer
Matt J
on 21 Mar 2022
Edited: Matt J
on 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 Comments
Matt J
on 21 Mar 2022
Edited: Matt J
on 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.
More Answers (1)
Steven Lord
on 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
tic
D = decomposition(A);
for k = 1:width(b)
x2(:, k) = D\b(:, k);
end
toc
11 Comments
Steven Lord
on 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
on 22 Mar 2022
Edited: Bruno Luong
on 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.
See Also
Categories
Find more on Linear Algebra in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!