Having trouble with "Index exceeds the number of array elements (1)" error
4 views (last 30 days)
Show older comments
Jordan Hamilton Morris
on 14 Dec 2018
Answered: per isakson
on 14 Dec 2018
If someone could help me to get this code working without error it would be greatly appreciated!
%Question 5: Robot Arm
ang01=input('Starting angle 1: ');
ang02=input('Starting angle 2: ');
angt1=input('Final angle 1: ');
angt2=input('Final angle 2: ');
tf=input('Final time (tf): ');
L1=input('Length 1 (L1): ');
L2=input('Length 2 (L2): ');
ang1=angt1-ang01;
ang2=angt2-ang02;
tmat=[tf^3,tf^4,tf^5];
a=pinv(tmat).*ang1;
b=pinv(tmat).*ang2;
disp(['a1: ',num2str(a(1))]);
disp(['a2: ',num2str(a(2))]);
disp(['a3: ',num2str(a(3))]);
disp(['b1: ',num2str(b(1))]);
disp(['b2: ',num2str(b(2))]);
disp(['b3: ',num2str(b(3))]);
angt1r=(pi/180)*angt1;
angt2r=(pi/180)*angt2;
x=L1*cosd(angt1r)+L2*cosd(angt1r+angt2r);
y=L1*sind(angt1r)+L2*sind(angt1r+angt2r);
t=0:0.01:2;
ax=gca;
for k=1:numel(t)
M=line([0,L1*cosd(angt1r(k)),x(k)],[0,L1*sind(angt1r(k)),y(k)]);
T=line(x(1:k),y(1:k));
pause(0.05)
if k~=numel(t)
delete(M)
delete(T)
end
end
0 Comments
Accepted Answer
per isakson
on 14 Dec 2018
The error occur in the line
M=line([0,L1*cosd(angt1r(k)),x(k)],[0,L1*sind(angt1r(k)),y(k)]);
Here the variables angt1r, x and y are scalars.
>> whos angt1r x y
Name Size Bytes Class Attributes
angt1r 1x1 8 double
x 1x1 8 double
y 1x1 8 double
Thus for k>=2 the error will occur.
0 Comments
More Answers (0)
See Also
Categories
Find more on Language Fundamentals in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!