Array indices must be positive integers or logical values.
조회 수: 2 (최근 30일)
이전 댓글 표시
I am trying to plot a v-t graph which pwm(t, T, d) wil be set to 1 if kT ≤ t < (k + d)T and to 0 if (k + d)T ≤ t < (k + 1)T. The below is my script:
T = 2*pi;
d = 0.5;
syms v;
for t = [0:1:10]
v(t) = pwmmode (t,T,d);
end
plot (t,v);
function y = pwmmode (t,T,d)
k = 1;
if ((k*T <= t) & (t <= (k+d)*T)) y =1;
elseif (((k+d)*T <= t) & (t<(k+1)*T)) y=0;
else y = 2;
end
end
When I run my script above, I receive errors in the window. Below are the errors displayed on my window:
댓글 수: 1
답변 (1개)
KSSV
2021년 1월 28일
In MATLAB arrays indices cannot be negative/ zeros. Your code:
for t = [0:1:10]
v(t) = pwmmode (t,T,d);
end
Should be like:
t = 0:1:10 ;
v = zeros(size(t)) ; % intialize accordingly
for i = 1:length(t)
v(i) = pwmmode (t(i),T,d);
end
댓글 수: 1
Walter Roberson
2021년 1월 28일
Or in this particular case you could abbreviate to
for t = [0:1:10]
v(t+1) = pwmmode (t,T,d);
end
However, KSSV's suggstion of creating the list of values ahead of time and looping through indices is a more general approach that you should learn how to use. For example if you were to use
for t = [0:1/3:10]
v(3*t+1) = pwmmode (t,T,d);
end
then you would fail because the t values would not be exact multiples of 1/3
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!