Error with spdiags size dimensions in pentadiagonal matrixes

I'm trying to create a sparse pentadiagonal matrix for a derivation discretization problem I'm working on. This matrix sould have the diagonals [1, -8, 0, 8, -1], with the value 0 being the one centered on the main diagonal.
I'm getting the error
Error using spdiags
For the syntax spdiags(B,d,m,n), the size of B must be min(m,n)-by-length(d).
Error in burnfile (line 15)
D1 = spdiags([1 -8 0 8 -1],-2:2,nx,nx);
It boggles me because if I try to paste in the tridiagonal example in the spdiagonal documentation page, it gives me the same error instead of working (having cleared all variables and on a clean notebook).
I'm working with Matlab R2022b, in case that's relevant.
The code I'm using is as follows:
nx = 1000;
D1 = spdiags([1 -8 0 8 1],-2:2,nx,nx);
full(D1)
ans = 1000x1000
0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

4 Comments

Works in R2024a, and we can't test it in your release (see above).
Sorry, but why can't it be tested in a previous version? Also I don't know what you mean by "see above"
Does anyone know if spdiags has undergone any significant updates between versions these 2 years? because that could be an answer
Sorry, but why can't it be tested in a previous version? Also I don't know what you mean by "see above"
Your code has been run in your post using the forum's Matlab engine. That's what has produced the output there. However, only R2024a is available for this:
@Matt J answered your first question. With "see above", I mean "look at your question: your code can be executed and does not throw an error in R2024a".

Sign in to comment.

 Accepted Answer

Does anyone know if spdiags has undergone any significant updates between versions these 2 years?
Yes, support for the syntax you are using is relatively new. The old way is,
nx = 1000;
e=ones(nx,1);
D1 = spdiags(e*[1 -8 0 8 1],-2:2,nx,nx);
full(D1)
ans = 1000x1000
0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

6 Comments

Thank you! That was exactly it
You should always look for help into the documentation of your own MATLAB version, not into the newest one.
Oh sorry, is this not it? Given it allowed me to ask the question for the previous version, I thought it was. What would the correct venue be, then?
It seems you looked up how to call the function "spdiags" somewhere in the documentation for R2024a instead of your release R2022b because you tried D1 = spdiags([1 -8 0 8 -1],-2:2,nx,nx). If you had looked up how to call the function in the documentation of your release, you would have avoided the error.
Of course, you are free to ask in the forum and you were lucky to find @Matt J who could remember how the function was called earlier.
FYI there's an entry in the Release Notes for this functionality in spdiags. It was introduced in release R2024a.
To see older documentation, click on the Support link at the top of many MathWorks webpages (including this one.) On the right side of the page, under the release number of the current release (indicating the Help Center is showing the documentation for the current release) click on Other Releases. You can select from one of last 5 years worth of releases (10 releases.) For example this is the documentation page for spdiags in release R2022b and this is the documentation page for spdiags in the current release. They look at a quick skim to be very similar until you get to the section for the Bin input argument and the Version History section.
Ahh! That makes a lot of sense, thanks for the detailed explanation, Steven. I did try to find the previous documentation as Torsten pointed out I should have done, of course, but I just sticked to Google and it just wouldn't show up. Thanks! I will always check on the appropiate documentation from now on!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2022b

Asked:

Pol
on 9 May 2024

Commented:

Pol
on 13 May 2024

Community Treasure Hunt

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

Start Hunting!