i am getting error in the plot function it says array indices must be +ve int or logical value

v1(1)=2;
v2(1)=4;
v3(1)=-1;
for n = 1:50
a1(n)=v1(n)-v2(n);
v1(n+1)=v1(n)+a1(n)*0.1;
a2(n)=v3(n)-3;
v2(n+1)=v2(n)+a2(n)*0.1;
a3(n)=a1(n)-a2(n);
v3(n+1)=v3(n)+a3(n)*0.1;
end
t= 0:0.1:5;
plot(t,v1(t*10+1))
hold all
plot(t,v2(t*10+1))
plot(t,v3(t*10+1))

댓글 수: 1

Why you want tp index v with t? you need not to do it while plotting.
The error is due to indexing. Array indices should be posititive integers in MATLAB. If you use ractions, you will get error.
A = rand(1,3) ;
A(0.5) % error
Array indices must be positive integers or logical values.

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

 채택된 답변

t= 0:0.1:5;
nt = length(t) ;
v1 = zeros(size(t)) ;
v2 = zeros(size(t)) ;
v3 = zeros(size(t)) ;
v1(1)=2;
v2(1)=4;
v3(1)=-1;
for n = 1:50
a1=v1(n)-v2(n);
v1(n+1)=v1(n)+a1*0.1;
a2=v3(n)-3;
v2(n+1)=v2(n)+a2*0.1;
a3=a1-a2;
v3(n+1)=v3(n)+a3*0.1;
end
plot(t,v1)
hold all
plot(t,v2)
plot(t,v3)

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2022년 7월 16일

댓글:

2022년 7월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by