Subscript dimension mismatch problem
이전 댓글 표시
I keep getting the error: Subscripted assignment dimension mismatch. Can some help me on how to fix this please
Error in untitled4 (line 39)
u(:,it+1:4)=((c*p(:,ip+1))+(d*p(:,ip))+(a1*MM*u(:,it))+(e*MM*v(:,it))+(f*KM*u(:,it)))/((a*MM)+(b*KM));
%Time
ti=0;
dt=0.02;
tf=5.98;
t=ti:dt:tf
nt=length(t)
%force
pi=0;
dp=2649.64;
pf=792242.36;
p=pi:dp:pf
np=length(p)
%Constants
alpha=0;
beta=0;
a1=alpha+(1/(0.5*dt));
b=beta+(0.5*dt);
c=0.5*dt;
d=(1-0.5)*dt;
e=1/0.5;
f=beta-((1-0.5)*dt);
g=1/(0.5*dt);
h=(1-0.5)/0.5;
%Initial conditions
u(:,1)=zeros;
v(:,1)=zeros;
a(:,1)=zeros;
p(:,1)=zeros;
MM=[1044,0,0,0;0,733.46,0,0;0,0,457.8,0;0,0,0,44.67];
KM=[9900000000,-9893000000,0,0;0,10000000000,-439000000,0;0,0,1370000000,-932200000;0,0,-932200000,932200000];
%time step
for it=1:nt-1;
for ip=1:np-1;
u(:,it+1)=((c*p(:,ip+1))+(d*p(:,ip))+(a1*MM*u(:,it))+(e*MM*v(:,it))+(f*KM*u(:,it)))/((a*MM)+(b*KM));
v(:,it+1)=g*(u(:,it+1)-u(:,it))-(h*v(:,it));
a(:,it+1)=g*(v(:,it+1)-v(:,it))-(h*a(:,it));
end
end
댓글 수: 2
Adam
2015년 3월 6일
Your code does not contain the line specified in the error message. Where does the it+1:4 part come from?
Vivek Bhadouria
2015년 3월 6일
Can you share the snnipet of the equation which you are trying to implement on Line#39?
답변 (1개)
Star Strider
2015년 3월 6일
The right-hand-side of ‘u’ is a (4x4) matrix of identical rows. What do you want to do in that assignment?
Put this statement just before the ‘u(:,it+1)’ assignment to see the result of that calculation:
qu =((c*p(:,ip+1))+(d*p(:,ip))+(a1*MM*u(:,it))+(e*MM*v(:,it))+(f*KM*u(:,it)))/((a*MM)+(b*KM));
댓글 수: 4
Vivek Bhadouria
2015년 3월 6일
Yeah, RHS is a 4X4 matrix but it's really unclear what kind of assignment in LHS is supposed to make!
laura dolan
2015년 3월 7일
편집: Star Strider
2015년 3월 7일
laura dolan
2015년 3월 7일
Star Strider
2015년 3월 7일
You have to format your code yourself so it shows up the way you want. Highlight the code in your posts, and then use the [{}Code] button at the top of the Comment window to format code.
You need to look closely at the calculations you’re doing. In the ‘u’ assignment, for instance, break the line down into its component calcualtions to see where the problems are:
%time step
for it=1:nt;
for t=ti:dt:tf;
P=2649.64*[0;0;0;1]*t;
P1=(2649.64*[0;0;0;1]*t)+(2649.624*[0;0;0;1]);
uq1 = (c*P1)+(d*P);
uq2 = (a1*MM*u(:,it));
uq3 = (e*MM*v(:,it));
uq4 = (f*KM*u(:,it));
uq5 = ((MM*a(:,it)));
uq6 = (b*KM);
u(:,it+1)=((c*P1)+(d*P)+(a1*MM*u(:,it))+(e*MM*v(:,it))+(f*KM*u(:,it)))/((a*MM)+(b*KM));
v(:,it+1)=g*(u(:,it+1)-u(:,it))-(h*v(:,it));
a(:,it+1)=g*(v(:,it+1)-v(:,it))-(h*a(:,it));
end
end
Note that I changed the reference in ‘uq5’ to ‘a(:,it)’ to make it a (4x1) vector. (I believe that is what you wanted.) The ‘uq6’ term remains a (4x4) matrix, and that is causing problems in your code. I will leave it to you to sort that.
I have outlined some debugging strategies that you can expand upon to figure out the reason your code is not working, then correct your code to get it to work. Since I don’t understand what you’re doing, that’s the most I can do.
카테고리
도움말 센터 및 File Exchange에서 Language Fundamentals에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!