Index exceeding number of array elements
2 views (last 30 days)
Show older comments
My code is attached below. I am unable to code for this as it says indexing error in the for loop (in bold font) . Please help!! Thank you
clear
close all
clc
% Code up fixed parameters.
a = 0.075; %Distance of centre of gravity OA [m]
b = 0.42; %Distance to the weight held OB [m]
u = 0.15; %Distance of upperarm muscle attachment [m]
f = 0.03; %Distance of forearm muscle attachment [m]
m_arm = 3; %Mass of arm [kg]
g = 9.81; %The univeral gravity constant [m/s^2]
t = linspace(0,30,200); %Time scale for bicep curl [s]
%Ask for user inputs
user1 = "Please input a value for theta [rad]:\n";
theta = input(user1); %Defines the range of motion in radians
while theta < 0 || theta > pi/3 %ensures that the limit of motion is respected and limits the value of theta
fprintf("The bicep can't curl at that angle. Please input a new value:\n")
theta = input(user1);
end
reps = input('Please input the number of bicep curl reps per minute:\n'); %Asks the user to input reps [min^(-1)]
T = 6*reps; %Defines the period of the curl
m_weight = input('Please input the mass of the carried weight [kg]:\n'); %Asks the user to input the weight [kg]
%Defining the variables from part (b) and moment of inertia and centre of mass
theta_t = pi/3*cos((2*pi/3).*t); %angular displacement with theta in rad and t in seconds
omega_t = -pi/2*sin((2*pi/3).*t); %angular velocity with omega in rad/s and t in s
alpha_t = -pi/3*cos((2*pi/3).*t); %angular acceleration with alpha in rad/s^2 and t in s
I_O = 0.03525 + 0.1804*m_weight; %Moment of inertia at O
r_com = (0.225+0.42*m_weight)/(3+m_weight); %centre of mass of the system
%Defining the force of the muslce and reaction muscles (given)
F_muscle_a = sqrt(u^2 + f^2 - 2*u*f.*sin(theta_t) );
F_muscle_b = u*f.*cos(theta_t);
F_muscle_c = I_O.*alpha_t + (a*m_arm + b*m_weight)*g*cos(theta_t);
F_muscle = (F_muscle_a./F_muscle_b).*F_muscle_c; %Equation for force of muscle [N]
R_par_a = F_muscle.*((f-u.*sin(theta_t))./F_muscle_a);
R_par_b = (m_arm + m_weight).*(g.*sin(theta_t))-((omega_t).^2*r_com);
R_par = R_par_a + R_par_b; %Equation for the parallel reaction force [N]
R_perp_a = (m_arm + m_weight).*(g.*cos(theta_t))+(alpha_t.*r_com);
R_perp_b = F_muscle.*(u.*cos(theta_t)./F_muscle_a);
R_perp = R_perp_a - R_perp_b; %Equation for the perp
%Defining the position of the arm
arm_ux = a*sin(theta); %defines the x-position of the upper arm
arm_uy = a*cos(theta); %defines the y-position of the upper arm
arm_fx = b*sin(theta); %defines the x-position of the forearm
arm_fy = b*cos(theta); %defines the y-position of the forearm
% Sets up subplotting scheme and relevant animatedline objects.
subplot(5,2,[1,3,5,7,9]) % LARGE subplot on the left for bicep curl animation
% 3 separate animatedline objects are set up, corresponding to the upper
% arm, forearm, and the weight carried
arm_u = animatedline('Color','k','LineWidth',1.5); %upperarm animation
arm_f = animatedline('Color','r','LineWidth',2.5); %forearm animation
weight = animatedline('Marker','o','MarkerSize',20,'MarkerFaceColor','y','MarkerEdgeColor','k','LineWidth',1.5);
title('Bicep curl Animation')
xlabel('x-displacement')
ylabel('y-displacement')
axis equal % sets axis to equal unit sizes so animation does not look weird
axis([-a b -a 0])
subplot(4,2,2) % first subplot on the right for animated plot of theta
thetaline=animatedline('Color','b','LineWidth',1.5);
title('Angular Displacement')
xlabel('t [s]')
ylabel('Theta [rad]')
axis([0 30 -theta theta])
subplot(4,2,4) % second subplot on the right for animated plot of parallel reaction force
R_parline=animatedline('Color','r','LineWidth',1.5);
title('Parallel reaction force')
xlabel('t [s]')
ylabel('Force [N]')
axis([0 30 min(R_par) max(R_par)])
subplot(4,2,6) % third subplot on the right for animated plot of perpendicular reaction force
R_perpline=animatedline('Color','g','LineWidth',1.5);
title('Perpendicular reaction force')
xlabel('t [s]')
ylabel('Force [N]')
axis([0 30 min(R_perp) max(R_perp])
subplot(4,2,8) % fourth subplot on the right for animated plot of Force of the muscle
forceline=animatedline('Color','c','LineWidth',1.5);
title('Force of mucle')
xlabel('t [s]')
ylabel('Force [N]')
axis([0 30 min(F_muscle) max(F_muscle)])
n=length(t); % determines number of points in t
for i=1:n
subplot(4,2,[1,3,5,7])
clearpoints(arm_u) % clears previous points in upperarm
clearpoints(arm_f) % clears previous points in forearm
clearpoints(weight) % clears previous points in weight
addpoints(arm_u,[0,arm_ux(i)],[0,arm_uy(i)]); % adds points (2 points that connect a line) to arm
addpoints(arm_f,[0,arm_fx(i)],[0,arm_fy(i)]); % adds points (2 points that connect a line) to forearm
addpoints(weight,arm_fx(i),arm_fy(i)) % adds points (only x-y position of forearm weight is relevant) to weight
drawnow
% animates plot of angular displacement
subplot(4,2,2)
addpoints(thetaline,t(i),theta_t(i))
drawnow
% animates plot of Parallel reaction force
subplot(4,2,4)
addpoints(R_parline,t(i),R_par(i))
drawnow
% animates plot of Perpendicular recation force
subplot(4,2,6)
addpoints(R_perpline,t(i),R_perp(i))
drawnow
% animates plot of Force of the muscle
subplot(4,2,8)
addpoints(forceline,t(i),F_muscle(i))
drawnow
end
0 Comments
Answers (1)
cdawg
on 29 Mar 2023
Edited: cdawg
on 29 Mar 2023
So you're getting that error because you're looping through i and the size of arm_ux is 1x1 (it's only one number). So when you try and get to arm_ux(2) it doesn't exist.
Should arm_ux = a*sin(theta) be arm_ux = a*sin(theta_t)? This would solve your problem.
0 Comments
See Also
Categories
Find more on Animation 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!