# Unable to perform assignment because the left and right sides have a different number of elements

조회 수: 21(최근 30일)
I understand the concept behind the error, but not so much the reason as it applies to the code. This is a homework problem that I was given.
If there are other issues with this code, I would really appreciate any and all explanations!
t = (0:.2:50);
V = zeros(length(t));
for i=1:length(t)
if 0 <= t(i) <= 8
V(i) = 10*t.^2-5*t;
elseif 8 < t(i) <= 16
V(i) = 624-5*t;
elseif 16 < t(i) <= 26
V(i) = 36*t+12*(t-16)^2;
else t(i) > 26;
V(i) = 2136*exp(1)^(-0.1*(t-26));
end
end
plot(t,V)

댓글을 달려면 로그인하십시오.

### 채택된 답변

Walter Roberson 4 Mar 2021
if 0 <= t(i) && t(i) <= 8
V(i) = 10*t(i).^2-5*t(i);
Also
else t(i) > 26;
That calculates whether t(i) is greater than 26, and then throws away the result of the test because of the semi-colon. Notice that you did not use elseif
##### 댓글 수: 1표시숨기기 없음
Nathan Brannan 4 Mar 2021
That seemed to work well. Thanks!

댓글을 달려면 로그인하십시오.

### 추가 답변(1개)

David Hill 4 Mar 2021
t = (0:.2:50);
V = zeros(length(t));
for i=1:length(t)
if 0 <= t(i)&&t(i) <= 8
V(i) = 10*t(i)^2-5*t(i);
elseif 8 < t(i)&& t(i) <= 16
V(i) = 624-5*t(i);
elseif 16 < t(i)&&t(i) <= 26
V(i) = 36*t(i)+12*(t(i)-16)^2;
elseif t(i) > 26
V(i) = 2136*exp(1)^(-0.1*(t(i)-26));
end
end
plot(t,V)
##### 댓글 수: 1표시숨기기 없음
Nathan Brannan 4 Mar 2021
Thank you!

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!

Translated by