error: In an assignment A(I) = B, the number of elements in B and I must be the same.

1 次查看(过去 30 天)
hi I have got a problem with the following code. When it's excuted, i receive the error "In an assignment A(I) = B, the number of elements in B and I must be the same." sadly i couldn't find so far the problem in the code. Can somebody find the mistake?
if true
function [re]=elementlasttensor(p,El,NG)
q=[0 0;10 7.5;7.5 5;0 0;0 0]; E=[210 210 210 70 210]*10^9; A=[0.05 0.05 0.05 0.05 0.05]; L=[1 1 1 2 1]; Knotenlast=[0 0 0 1 0]; Element=[1 2 3 4 5];
re=zeros(p+1);
if NG==1 alpha=2; psi=0;
elseif NG==2
alpha=[1 1];
psi=[-1/sqrt(3) 1/sqrt(3)];
elseif NG==3
alpha=[(5/9) (8/9) (5/9)];
psi=[-sqrt(3/5) 0 sqrt(3/5)];
elseif NG==4
alpha=[0.34785 0.65241 0.65241 0.34785];
psi=[-0.86114 -0.33998 0.33998 0.86114];
else
end
%for Element=1:El
for g=1:NG [N]=Ansatzfunktion(p,psi(g));
b=0;
for j=1:p+1
b=b+N(j)*q(Element,j);
end
for i=1:p+1
re(i)=re(i)+N(i)*b*(L/2)*alpha(g);
end
end
end
%end
end
the variables are EL=5, NG=4 and p=1

回答(2 个)

Paul
Paul 2014-1-13
编辑:Paul 2014-1-13
Probably this line:
b=b+N(j)*q(Element,j);
Element is a vector itself. It should be a scalar but i dont know exactly what you want to do so you got to figure it out yourself what it should be. There may more problems with your script, though. Also, matlab outputs the exact line where the error happens, so you should be able to debug yourself by reviewing that specific line.

Amit
Amit 2014-1-13
The problem is in
re(i)=re(i)+N(i)*b*(L/2)*alpha(g);
b is (n*1) matrix and L is a (1,n) matrix. If you are doing, what I think you are doing (matrix multiplication), the correct code should be:
re(i)=re(i)+N(i)*((L/2)*b)*alpha(g);
Your version of re(i) creates a n*n matrix and that why the error comes.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by