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 sparse 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 three questions:
- How to avoid the division in the elements of L that are 0?
- 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?.
- Am I supposed to use
.*
or .\
anywhere?
For the first question, I tried this but didn't work, because of the the matrix dimensions:
x_new = (speye(size(x_old,2))- dt * lambda * L(L~=0)) \ x_old;
For example, for these matrices:
full(L) = [4.6188 1.1547 1.1547 1.1547 1.1547;
1.1547 2.3094 0.5774 0 0.5774;
1.1547 0.5774 2.3094 0.5774 0;
1.1547 0 0.5774 2.3094 0.5774;
1.1547 0.5774 0 0.5774 2.3094]
x_old = [-0.4000 0 0.5000;
0.1000 -0.5000 0.5000;
0.1000 0 1.0000;
0.1000 0.5000 0.5000;
0.1000 0 0]
the result is like this:
x_new = [-0.407106857205753 8.52491160610484e-19 0.523921051079662;
0.0993707696612247 -0.505840945396493 0.511891327878594;
0.0993707696612247 3.20963195216710e-19 1.01773227327509;
0.0993707696612247 0.505840945396493 0.511891327878594;
0.0993707696612247 5.22659462097013e-19 0.00605038248210024]
which in command window looks like this:
x_new =
-0.4071 0.0000 0.5239
0.0994 -0.5058 0.5119
0.0994 0.0000 1.0177
0.0994 0.5058 0.5119
0.0994 0.0000 0.0061
More...