# Index exceeds the number of array elements (1).

4 views (last 30 days)
Gabriel Maiz on 22 Oct 2020
Answered: Ameer Hamza on 22 Oct 2020
i keep getting this error from my code:
FuerzaMotor = 7475 %Dame la fuerza del motor
AceleracionInicial = 5 %Dame la aceleración inicial
CoeficienteResistencia = 0.183 %Dame la constante de fricción de aire
MasaVehiculo = 1495 %Dame la masa del vehículo
Delta = 0.01;
%Inicialización de vectores
vt = [0:Delta:10];
va= [0:Delta:10];
vdx= [0:Delta:10];
vdy= [0:Delta:10];
vv= [0:Delta:10];
vFM=[ 0:Delta:10];
vFR= [0:Delta:10];
%Damos los valores iniciales a la primera posición del vector
vv(1) = 0;
va(1) = AceleracionInicial;
vdx(1) = 0;
vdy(1) = 0;
vFR(1)= 0;
VFM(1)= FuerzaMotor;
angulo= 15;
for i = 2:1001
vv(i)= vv(i-1) + va(i-1)*Delta;
vFR(i)= CoeficienteResistencia*vv(i)^2;
va(i)=(FuerzaMotor - vFR(i))/MasaVehiculo;
angulo(i)= angulo(i) - 3*Delta
vFR(i)= FuerzaMotor;
vdx(i)= vdx(i-1) + vv(i-1)*cosd(angulo)*Delta;
vdy(i)= vdy(i-1) + vv(i-1)*sind(angulo)*Delta;
end
figure ("Name","X contra Y");
plot(vdx,vdy);

#### 1 Comment

Stephen Cobeldick on 22 Oct 2020
The square brackets are completely superfluous (you are not concatenating anything), get rid of them.
The only purpose they serve is to mislead beginners into thinking they are creating a "list" (which they are not).

Stephen Cobeldick on 22 Oct 2020
Edited: Stephen Cobeldick on 22 Oct 2020
Your define angulo as a scalar (i.e. size 1x1):
angulo= 15;
and then a few lines later with i=2 you try to access its 2nd element (which does not exist):
angulo(i)= angulo(i) - 3*Delta
whic of course throws an error. Possibly what you want is this:
angulo = angulo - 3*Delta;

Ameer Hamza on 22 Oct 2020
There are issues with your indexing. Try the following code, and check its difference with your code
for i = 2:1001
vv(i)= vv(i-1) + va(i-1)*Delta;
vFR(i)= CoeficienteResistencia*vv(i)^2;
va(i)=(FuerzaMotor - vFR(i))/MasaVehiculo;
angulo(i)= angulo(i-1) - 3*Delta;
vFR(i)= FuerzaMotor;
vdx(i)= vdx(i-1) + vv(i-1)*cosd(angulo(i))*Delta;
vdy(i)= vdy(i-1) + vv(i-1)*sind(angulo(i))*Delta;
end

### Community Treasure Hunt

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

Start Hunting!