Subscripted assignment dimension mismatch.
Info
This question is closed. Reopen it to edit or answer.
Show older comments
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
Answers (2)
Adam
on 24 Nov 2014
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.
Star Strider
on 24 Nov 2014
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));
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!