I'm getting the error 'Subscripted assignment dimension mismatch.' for this piece of code (the last line: s(1,i)). Could anyone help me to figure out what is wrong and how to fix it? Thank you very much.

for i=1:n
suma1 = 0;
b=(nchoosek(q,1))*(sa1_func(t));
c=((s2_func(t)));
f=(nchoosek(q,2))*(sa2_func(t));
g=s1_func(t);
suma1 = suma1 + (b.*c)*z(1,i,1)+(f.*g)*z(1,i,2) ;
s(1,i) = (1/(h^q))*((s_func(t))*x(1,i)+(sa_func(t))*x(1,i+1)+ suma1) ;
end;

댓글 수: 5

You do not tell us the values of n, q, sal_func, t, s2_func, and maybe some others, so we cannot run your code. You also do not tell us the complete error message (this means all of the red text).
Would you like us to rely on our magical crystal balls to read your computer screen?
n=10;
eps = 10^(-15);
a=1;
q=3;
h=a/n;
t = linspace(0,a,n+2);
x = zeros(n+2,n+2);
z = zeros(n+2,n+2,n+2);
s = zeros(n+2,n+2);
for i=1:n+1
t(i)=(i-1)*h;
end;
for i=1:n+1
suma1 = 0;
for j=2:n+1
suma1 = suma1 + h_func(t(i),t(j-1)) * f_func(t(j-1), g_func(t(j-1)), g_func(fi_func(t(j-1)))) + h_func(t(i), t(j)) * f_func(t(j), g_func(t(j)), g_func(fi_func(t(j))));
end;
suma1 = suma1 * (a/(2*n));
x(1,i) = g_func(t(i)) + suma1 ;
end;
for i=1:n
for p=1:q-1
suma2 = 0;
for j=2:n+1
suma2 = suma2 + h_func(t(i)+(p*h)/q,t(j-1)) * f_func(t(j-1), g_func(t(j-1)), g_func(fi_func(t(j-1)))) + h_func(t(i)+(p*h)/q, t(j)) * f_func(t(j), g_func(t(j)), g_func(fi_func(t(j))));
end;
suma2 = suma2 * (a/(2*n));
z(1,i,p) = g_func(t(i)+(p*h)/q) + suma2 ;
end;
end;
for i=1:n
v = linspace(t(i),t(i+1),n);
suma11 = 0;
for j=1:q-1
b=(nchoosek(q,j))*((v-t(i)).^j);
c=(t(i+1)-v).^(q-j);
suma11 = suma11 + (b.*c)*z(1,i,j);%+(f.*g)*z(1,i,2) ;
end;
e= (1/(h^q))*((t(i+1)-v).^q)*x(1,i);
r=(1/(h^q))*((v-t(i)).^q)*x(1,i+1);
suma11=(1/(h^q))*suma11;
s(1,i) = e+r+ suma11 ; %here is the error
end;
m=2;
ok=1;
while ok==1
for i=1:n+1
suma1 = 0;
for j=2:n+1
suma1 = suma1 + h_func(t(i),t(j-1)) * f_func(t(j-1), x(m-1,j-1), s(m-1,j-1)) + h_func(t(i), t(j)) * f_func(t(j), x(m-1,j), s(m-1,j));
end;
suma1 = suma1 * (a/(2*n));
x(m,i) = g_func(t(i)) + suma1 ;
end;
for i=1:n
for p=1:q-1
suma2 = 0;
for j=2:n+1
suma2 = suma2 + h_func(t(i)+(p*h)/q,t(j-1)) * f_func(t(j-1), x(m-1,j-1), s(m-1,j-1)) + h_func(t(i)+(p*h)/q, t(j)) * f_func(t(j), x(m-1,j), s(m-1,j));
end;
suma2 = suma2 * a/(2*n);
z(m,i,p) = g_func(t(i)+(p*h)/q) + suma2 ;
end;
end;
for i=1:n
v = linspace(t(i),t(i+1),n);
suma11 = 0;
for j=1:q-1
b=(nchoosek(q,j))*((v-t(i)).^j);
c=(t(i+1)-v).^(q-j);
suma11 = suma11 + (b.*c)*z(m-1,i,j);%+(f.*g)*z(1,i,2) ;
end;
e= (1/(h^q))*((t(i+1)-v).^q)*x(m-1,i);
r=(1/(h^q))*((v-t(i)).^q)*x(m-1,i+1);
suma11=(1/(h^q))*suma11;
s(m-1,i) = e+r+ suma11 ; %here is the error
end;
ok=1;
for i=2:n
if abs(x(m,i) - x(m-1,i)) < eps
ok = 0;
break;
end;
end;
if ok==1
m=m+1;
end;
end;
fileID = fopen('rezultate.txt','w');
fprintf(fileID,'\r\n');
fprintf(fileID,'n = %5d \r\n',n);
fprintf(fileID,'m = %5d \r\n',m);
fprintf(fileID,'q = %5d \r\n',q);
%fprintf(fileID,'miu(%1d) = %1.6f \r\n',k,miu(k));
fprintf(fileID,'t[i] | x[t[i]] | x[m][t[i]] | x[t[i]] - x[m][t[i]] |\r\n');
for i=1:n+1
temp = exp(-t(i));
fprintf(fileID,'%1.16f | %1.16f | %2.16f | %1.2e |\r\n',t(i),temp,x(m,i),temp-x(m,i));
end;
No h_func or g function, no copy of error message...
copy of error message:
??? Subscripted assignment dimension mismatch.
Error in ==> Main at 89
s(1,i) = e+r+ suma11 ;
function y=g_func(t)
y = ((3*exp(1)-1)/(3*exp(1)))*exp(-t);
function y=h_func(t,s)
y=((1-s)*exp(-t))/3;
end
function y=fi_func(s)
y = s/2;
function y=f_func(t1,t2,t3)
y = t3*t3;
nchoosek(q,1)? Writing q is much easier.
Now the original code is hidden inside a comment, while the code in the question is outdated, as far as I understand. Please post the actual code inside the question, where readers expect it. Thanks.

답변 (2개)

As I understand, you assign vector to cell of matrix s.
You have
v = linspace(t(i),t(i+1),n);
so your v is a vector. Then
b=(nchoosek(q,j))*((v-t(i)).^j);
c=(t(i+1)-v).^(q-j);
Because v is a vector, b and c are going to be vectors.
suma11 = suma11 + (b.*c)*z(m-1,i,j);%+(f.*g)*z(1,i,2) ;
vector .* vector gives vector, so suma11 is going to be a vector.
e= (1/(h^q))*((t(i+1)-v).^q)*x(m-1,i);
r=(1/(h^q))*((v-t(i)).^q)*x(m-1,i+1);
v is a vector, so e and r are going to be vectors.
s(1,i) = e+r+ suma11 ; %here is the error
e and r and suma11 are vectors, so the right hand side is a vector. But the left hand side is one individual location. You cannot store a vector into an individual location. suma11=(1/(h^q))*suma11;
suma11 is already a vector and this just scales it, leaving it a vector.

이 질문은 마감되었습니다.

질문:

2017년 1월 19일

마감:

2021년 8월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by