How to enforce element-wise condition in matrix operations?

2 visualizzazioni (ultimi 30 giorni)
I am trying to use \ for Euler's backward method:
where dt and lambda are constant numbers like:
dt = 0.01
lambda = 0.5
and L is a square matrix with the same size as x_old and x_new
here is what I have tried:
x_new = (speye(size(x_old,2))- dt * lambda * L) \ x_old;
I had two questions:
  1. How to avoid the division in the elements of L that are 0?
  2. Is it OK to move (speye(size(x_old,2))- dt * lambda * L) to the other side, or should I maintain the formula's structure?
For the first question, I tried this but didn't work:
x_new = (speye(size(x_old,2))- dt * lambda * L(L~=0)) \ x_old;
  2 Commenti
Guillaume
Guillaume il 24 Nov 2019
I'm unclear why you want to ignore the 0s in L and why you think they don't matter. Note that \ is not a division, it's a linear equation solver and 0s do matter for a linear equation.
In any case, it's not possible to remove the 0s. You have to pass a matrix to \. You can remove entire rows or colums but you can't have matrix cells with nothing in them.
Also, are you matrix so large that you have to use sparse matrix? Otherwise, I would be better defined as eye(size(x_old)). Even if using speye, I would use speye(size(x_old)) instead of speye(size(x_old, 2)). The two are equivalent iff x_old is square but the former is more consise (and probably inifinitesimally faster).
Hadi Ghahremannezhad
Hadi Ghahremannezhad il 24 Nov 2019
Thank you! this was very helpful. So I don't have to do anything with the zero elements. I also corrected the size(x_old) part.
Yes, the matrix is very large, so I need to use sparse as much as possible. Could you also check whether the operations are correct or should I use .* and .\ instead of * and \?

Accedi per commentare.

Risposta accettata

Guillaume
Guillaume il 24 Nov 2019
Reposting my comment as an answer and adding a bit more to it.
I'm unclear why you want to ignore the 0s in L and why you think they don't matter. Note that \ is not a division, it's a linear equation solver and 0s do matter for a linear equation.
In any case, it's not possible to remove the 0s. You have to pass a matrix to \. You can remove entire rows or colums but you can't have matrix cells with nothing in them.
Also, are you matrix so large that you have to use sparse matrix? Otherwise, I would be better defined as eye(size(x_old)). Even if using speye, I would use speye(size(x_old)) instead of speye(size(x_old, 2)). The two are equivalent iff x_old is square but the former is more consise (and probably inifinitesimally faster).
"should I use .*"
Your multiplications are with scalars, so using .* or * doesn't make a difference
"should I use .\"
That's a completely different operation. If you are trying to solve linear equations you have to use \

Più risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by