Subscripted assignment dimension mismatch.
정보
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
이전 댓글 표시
Whats going wrong and how can I fix this?
Getting "error Subscripted assignment dimension mismatch.
Error in Untitled (line 10)"
Code:
x1=linspace(0,1,100);
t=linspace(0,4,400);
for i=1:length(x1);
for j=1:length(t);
u(i,j)=0;
cn=0;
for n=1:400;
cn=2/(pi*n)*(1-2*(1-2*cos(pi*n))/((pi*n)^2+4));
u(i,j)=u(i,j)+(cn*exp(-(pi*n)^2*t)+2/(pi*n)*(1-2*cos(pi*n))/((pi*n)^2+4)*(2*cos(2*t)-(pi*n)^2*sin(2*t)))*sin(pi*n*x1(i));
end
end
end
댓글 수: 0
답변 (2개)
Adam
2014년 11월 24일
Try splitting your maths into multiple statements to check each part more easily when you get problems like this.
You set
u(i,j) = 0;
in your for loop, but first time round that means u is just a scalar, but all the stuff you add to it later on is a 1x400 array. You can't add a 1x400 array to u(i,j) like that. I'm not sure what you actually intended, but t is vectorised so looping round it also with j doesn't seem right.
댓글 수: 0
Star Strider
2014년 11월 24일
Not really certain what you intend, but since ‘t’ has 400 elements and your ‘n’ loop the same, I assume you intend to have a value of ‘t’ for each iteration. You may need to subscript ‘t’ as ‘t(n)’.
See if changing to this line improves things:
u(i,j)=u(i,j)+(cn*exp(-(pi*n)^2*t(n))+2/(pi*n)*(1-2*cos(pi*n))/((pi*n)^2+4)*(2*cos(2*t(n))-(pi*n)^2*sin(2*t(n))))*sin(pi*n*x1(i));
댓글 수: 0
이 질문은 마감되었습니다.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!