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 commentaire

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.

Connectez-vous pour commenter.

 Réponse acceptée

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)

Plus de réponses (0)

Question posée :

le 16 Juil 2022

Commenté :

le 16 Juil 2022

Community Treasure Hunt

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

Start Hunting!

Translated by