I have a very quick question. I am working through a finite difference problem (1D steady-state heat transfer) and am having trouble getting the coefficient matrix A, shown below:
n = length(dL);
A = diag(ones(n-1),-1)-2*eye(n)+diag(ones(n-1),1);
I am getting the error:
Error using -
Matrix dimensions must agree.
Error in final (line 85)
A = diag(ones(n-1),-1)-2*eye(n)+diag(ones(n-1),1);
Can someone please tell me where my error is? I feel like this is extremely straight-forward and should be working.

 채택된 답변

Star Strider
Star Strider 2015년 12월 11일

0 개 추천

You need to create a vector of ones for the off-diagonals, not a matrix of them, so ones has to have 2 arguments in the diag calls:
A = diag(ones(n-1,1),-1)-2*eye(n)+diag(ones(n-1,1),1);

댓글 수: 2

lsutiger1
lsutiger1 2015년 12월 11일
Yep, just figured that out a couple of minutes ago! Thank you for the response.
Star Strider
Star Strider 2015년 12월 11일
My pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

질문:

2015년 12월 11일

댓글:

2015년 12월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by