Index exceeds the number of array elements. Please Help?

% Generates optimized trajectory for the robotics arm problem
function [] = generate_optimized_traj(x)
d1 = 66.04;
d3 = 14.91;
d4 = 43.31;
a2 = 43.18;
a3 = 2.03;
for i = 1:100
t = -pi + (i-1)*0.063;
theta1 = x(i);
theta2 = x(i+100);
theta3 = x(i+200);
c1 = cos(theta1);
c2 = cos(theta2);
s1 = sin(theta1);
s2 = sin(theta2);
c23 = cos(theta2+theta3);
s23 = sin(theta2+theta3);
f1(i) = c1*(a2*c2 + a3*c23 - d4*s23) - d3*s1;
f2(i) = s1*(a2*c2 + a3*c23 - d4*s23) + d3*c1;
f3(i) = d1 - a2*s2 - a3*s23 -d4*c23;
end
plot3(f1,f2,f3,'r*')
end
This is the code where the error is encountered. This appears on line 15 ( theta2=x(i+100) ).
The code above should generate the optimal trajectory for the arm of an industrial robot.
f1,f2 and f3 are kinetic equations.
Does anyone have any idea why i get the following error on line 15: Index exceeds the number of array elements (2).

 Réponse acceptée

KALYAN ACHARJYA
KALYAN ACHARJYA le 21 Oct 2019
Modifié(e) : KALYAN ACHARJYA le 21 Oct 2019
Read about array indexing, suppose you have x=[2 3 4], its having 3 length, if you want acess x(5), it will shows Index exceeds the number of array elements., because x have only three elements , hence
x(1) OK
x(2) OK
x(3) OK
..
x(4) Invalid ....
Beacuse , what you have pass the value of x, I have no idea, but you trying to access the more than 100 elements of x (i+100)..so on in for loop (100 iterration loop), is it?
Is this? Pass any 1 D array as x
function []=generate_optimized_traj(x)
d1 = 66.04;
d3 = 14.91;
d4 = 43.31;
a2 = 43.18;
a3 = 2.03;
for i=1:length(x)
t=-pi+(i-1)*0.063;
theta1=x(i);
theta2=x(i)+100;
theta3=x(i)+200;
c1 = cos(theta1);
c2 = cos(theta2);
s1 = sin(theta1);
s2 = sin(theta2);
c23 = cos(theta2+theta3);
s23 = sin(theta2+theta3);
f1(i) = c1*(a2*c2 + a3*c23 - d4*s23) - d3*s1;
f2(i) = s1*(a2*c2 + a3*c23 - d4*s23) + d3*c1;
f3(i) = d1 - a2*s2 - a3*s23 -d4*c23;
end
plot3(f1,f2,f3,'r*')
end

Plus de réponses (1)

Guillaume
Guillaume le 21 Oct 2019

0 votes

Does anyone have any idea why i get the following error on line 15
Because the x you passed has less than 300 elements. Since i goes up to 100 and you try to access x(i+200), x needs to have at least 300 elements.

Produits

Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by