Index exceeds the number of array elements (1).

Hi guys, I try to solve a mechanisims but I always run into this problem.
%CLEAR: Variables and command window in MATLAB
clc,clear
% INPUT: PHYSICAL PARAMETERS of MECHANISM (cm)
l2=2.7;l3=6;CO4=4.6;O4F=2.3;FO2=11.3;l5=3.8;DO4=1.85;GE=2.9
% INPUT: Maximum Iteration Number Nmax
Nmax=100;
% INPUT: INITIAL GUESS VALUES for th2, th3, th5 and Sd respectively
x=[30*pi/180,60*pi/180,40*pi/180,10];
% INPUT: ERROR TOLERANCE
xe=0.001*abs(x);
% INPUT: SYSTEM INPUTS (th4,w4,a4,b,B)
b=(-25.44)*pi/180
dth=1*pi/360;
th4=30*pi/180:dth:220*pi/180;
w4=5*ones(1,length(th4));
a4=0*ones(1,length(th4));
%----------------------------------------------
xe=transpose(abs(xe));
kerr=1; %If kerr=1, results are not converged
for k=1:1:length(th4);
for n=1:Nmax
%----------------------------------------------
%Assign initial guess to unknowns
th2(k)=x(1);th3(k)=x(2);
th5(k)=x(3);Sd(k)=x(4);
% INPUT: JACOBIAN Matrix
J=zeros(4,4);
J(1,1)=-l2*sin(th2(k));J(1,2)=-l3*sin(th3(k));
J(2,1)=l2*cos(th2(k));J(2,2)=l3*cos(th3(k));
J(3,3)=-l5*sin(th5(k));;J(4,3)=-l5*cos(th5(k))
J(3,4)=1
% INPUT: Function f
f=zeros(4,1);
f(1,1)=-(CO4*cos(th4(k))+FO2+l2*cos(th2(k))+l3*cos(th3(k)));
f(2,1)=-(CO4*sin(th4(k))+O4F+l3*sin(th3(k))+l2*sin(th2(k)));
f(3,1)=-(l5*cos(th5(k))+DO4*cos(th4(k)+b)+Sd(k));
f(4,1)=-(l5*sin(th5(k))+DO4*sin(th4(k)+b)+GE);
%----------------------------------------------
eps=inv(J)*f;x=x+transpose(eps);
if abs(eps)<xe
kerr=0;break
if kerr==1
'Error nr'
end
end
end
th2(k)=x(1);th3(k)=x(2);
th3=x(3);Sd(k)=x(4);
%---velocity---------------------------
fv(1,1)=CO4*w4(k).*sin(th4(k));
fv(2,1)=-CO4*w4(k).*cos(th4(k));
fv(3,1)=DO4*w4(k).*sin(th4(k)+b);
fv(4,1)=-DO4*w4(k).*cos(th4(k)+b);
vel=inv(J)*fv;
w2(k)=vel(1);w3(k)=vel(2);
w5(k)=vel(3);Vd(k)=vel(4);
%---acceleration---------------------------
fa(1,1)=l2*(w2(k)^2)*cos(th2(k))+l3*(w3(k)^2)*cos(th3(k))+CO4*(w4(k)^2)*cos(th4(k))+CO4*a4(k)*sin(th4(k));
fa(2,1)=w2(k)^2.*l2*sin(th2(k))+w3(k)^2*l3*sin(th3(k))-CO4*a4(k)*cos(th4(k))+w4(k)^2*CO4*sin(th4(k));
fa(3,1)=w5(k)^2*l5*cos(th5(k))+DO4*a4(k)*sin(th4(k)+b)+w4(k)^2*DO4*cos(th4(k)+b);
fa(4,1)=l5*w5(k).^2*cos(th5(k))+DO4*a4(k)*sin(th4(k)+b)+DO4*w4(k)^2*cos(th4(k)+b);
acc=inv(J)*fa;
a2(k)=acc(1);a3(k)=acc(2);
a5(k)=acc(3);ad(k)=acc(4);
end
% Angle: radian --> degree
% Angle: radian --> degree
th2d=th2*180/pi;
th3d=th3*180/pi;
th4d=th4*180/pi
th5d=th5*180/pi;
%--------Plots---------------
figure(1),
subplot(4,3,1),plot(th4d,th2d,'r','linewidth',2),xlabel('\theta_4 (^o)'),ylabel('\theta_2(^o)'),grid on;
subplot(4,3,2),plot(th4d,w2,'r','linewidth',2),xlabel('\theta_4 (^o)'),ylabel('\omega_2(r/s)'),grid on;
subplot(4,3,3),plot(th4d,a2,'r','linewidth',2),xlabel('\theta_4 (^o)'),ylabel('\alpha_2(r/s^2)'),grid on;
subplot(4,3,4),plot(th4d,th3d,'r','linewidth',2),xlabel('\theta_4(^o)'),ylabel('\theta_3 (^o)'),grid on;
subplot(4,3,5),plot(th4d,w3,'r','linewidth',2),xlabel('\theta_4(^o)'),ylabel('\omega_3 (r/s)'),grid on;
subplot(4,3,6),plot(th4d,a3,'r','linewidth',2),xlabel('\theta_4 (^o)'),ylabel('\alpha_3(r/s^2)'),grid on;
subplot(4,3,7),plot(th4d,Sd,'r','linewidth',2),xlabel('\theta_4 (^o)'),ylabel('Sd(cm)'),grid on;
subplot(4,3,8),plot(th4d,Vd,'r','linewidth',2),xlabel('\theta_4 (^o)'),ylabel('V_d(cm/s)'),grid on;
subplot(4,3,9),plot(th4d,ad,'r','linewidth',2),xlabel('\theta_4 (^o)'),ylabel('a_d(cm/s^2)'),grid on;
subplot(4,3,10),plot(th4d,th5d,'r','linewidth',2),xlabel('\theta_4 (^o)'),ylabel('theta_5(^o)'),grid on;
subplot(4,3,11),plot(th4d,w5,'r','linewidth',2),xlabel('\theta_4 (^o)'),ylabel('omega_5(r/s)'),grid on;
subplot(4,3,12),plot(th4d,a5,'r','linewidth',2),xlabel('\theta_4 (^o)'),ylabel('alpha_5(r/s^2)'),grid on;
%%-------Writing position values to file ------------------
knm=[transpose(th2d) transpose(th3d) transpose(th5d) transpose(th4d) transpose(yy)];
str = 'th2 th3 th5 th4 Y '; %# A string
fName = 'konum.txt'; %# A file name
fid = fopen(fName,'w'); %# Open the file
if fid ~= -1
fprintf(fid,'%s\r\n',str); %# Print the string
fclose(fid); %# Close the file
end
dlmwrite(fName,knm,'-append',... %# Print the matrix
'delimiter','\t', 'newline','pc');
%%-------Writing velocity values to file ------------------
hz=[transpose(th2d) transpose(w3) transpose(w5) transpose(w4) transpose(vyy)];
str = 'th2 w3 w5 w4 VY '; %# A string
fName = 'hız.txt'; %# A file name
fid = fopen(fName,'w'); %# Open the file
if fid ~= -1
fprintf(fid,'%s\r\n',str); %# Print the string
fclose(fid); %# Close the file
end
dlmwrite(fName,hz,'-append',... %# Print the matrix
'delimiter','\t', 'newline','pc');
%%-------Writing acceleration values to file ------------------
ivme=[transpose(th2d) transpose(al3) transpose(al5) transpose(al4) transpose(alyy)];
str = 'th2 al3 al5 al4 aY '; %# A string
fName = 'ivme.txt'; %# A file name
fid = fopen(fName,'w'); %# Open the file
if fid ~= -1
fprintf(fid,'%s\r\n',str); %# Print the string
fclose(fid); %# Close the file
end
dlmwrite(fName,hz,'-append',... %# Print the matrix
'delimiter','\t',...
'newline','pc');
''fa(1,1)=l2*(w2(k)^2)*cos(th2(k))+l3*(w3(k)^2)*cos(th3(k))+CO4*(w4(k)^2)*cos(th4(k))+CO4*a4(k)*sin(th4(k));'' In this line program said to me Index exceeds the number of array elements (1).
Error in Untitled2 (line 65)
>>
Please help me, Thatt my midterm homework and I cant solve. Thanks.

2 Comments

Please edit your question so that your code is formatted properly and appears within a code block by using the Code Toggle tool [more info].
thank you much for your answer. I solved it.

Sign in to comment.

 Accepted Answer

Follow the steps provided in this answer to determine the issue.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!