Updating Sparse Matrices every Time-Step Efficiently
5 views (last 30 days)
Show older comments
I am working on a numerical scheme to solve a fourth-order 2D partial differential equation. As such, my scheme boils down to solving the following linear system of equations at each time-step: ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1027750/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1027750/image.png)
After solving this, I then need to update the coefficients in
for the next time-step. For most simulations, I am using
points in x and
points in y which make the matrices quite large and so my code is quite slow at the moment. In trying to figure out how to optimize it, I came across this article: Creating Sparse Matrices.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1027755/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1027760/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1027765/image.png)
However, I do not want to create a new matrix but rather only edit a certain number of entries. I set up my
vectors so that
. Now my question is how to make these changes without creating a new matrix and wiping out the non-zero elements that don't need to be updated. The command I tried was:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1027770/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1027775/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1027780/image.png)
but this didn't work as the two sides were of different dimensions. I'm kind of at a loss for how else this could be accomplished without recreating the matrix from scratch each time step. For reference, at the above values of
, the matrix
contains
non-zero elements. Ideally, I would like to increase those even more so building the matrix every time step seems like it wouldn't really speed things up.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1027785/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1027790/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1027795/image.png)
My matrix does have a fairly basic structure to it, however, where the five main diagonals contain the majority of the information and then there are diagonals
elements away in both directions with non-zero information as well.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1027800/image.png)
0 Comments
Answers (1)
James Tursa
on 10 Jun 2022
Edited: James Tursa
on 10 Jun 2022
To specify the sparse matrix size when creating it, use the following syntax:
sparse(I,J,K,m,n)
where m is the number of rows and n is the number of columns.
That being said, this will of course create the sparse matrix from scratch each iteration using the I, J, K variables, so you will incur that overhead.
Changing the existing sparse matrix directly instead will also incur some overhead since the existing elements will have to be moved in memory. You will probably have to experiment to see which method is faster for you based on your particular matrix structure.
Question: When you update Q, do you simply modify existing non-zero values to different non-zero values, or do you insert/delete other non-zero values into the matrix? I.e., do you change the sparse matrix pattern at each iteration or does this sparse matrix pattern remain the same at each iteration?
See Also
Categories
Find more on Operating on Diagonal Matrices 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!