0
votes

I have the next question, how to build a matrix with specific values knowing that the size of the matrix is NxN.

Here is my question

I've being trying with the next code:

  a = (1+2*Du*dt/dx^2);  
  b = -Du*dt/dx^2;        
  main = a*sparse(ones(Nx,1));
  off  = b*sparse(ones(Nx-1,1));
  Bu = diag(main) + diag(off,1) + diag(off,-1);

But as you can see there is not the needed value in (1,1) and (N,N), so how can I build this specific matrices? How would be the code for this in MATLAB?

1

1 Answers

0
votes

spdiags is the way to go,

A = sparse(Nx);
A = spdiags(b*ones(Nx-1,1), -1, A);
A = spdiags(a*ones(Nx,1), 0, A);
A = spdiags(b*ones(Nx-1,1), 1, A);
A(1, 1:2) = [1,1];
A(N, N-1:N) = [1,1];