Why does my plot go blank when I run my program?

1 view (last 30 days)
The code is fairly fine syntaxwise but when I run it there is no plot or simulation appearing
clear all; %#ok<CLALL>
close all;
L_0=7;
L_1=3;
L_2=7.5;
L_3=4.5;
L_PA=4;
alpha=35;
w2=5;
theta2=0:2:360;
figure(1);
for i=1:length(theta2(i))
theta2(i)=0:2:360;
AC(i)= sqrtL_0^2+L_1^2-2*L_0*L_1*cosd(theta2(i));
beta(i)=acosd(L_0^2+AC(i)^2-L_1^2)/ 2*L_0*AC(i);
psi(i)=acosd(L_2^2+AC(i)^2-L3^2)/ 2*L_2*AC(i);
lambda(i)=acosd(L_3^2+AC(i)^2-L_2^2)/ 2*L_3*AC(i); %#ok<*SAGROW>
theta3(i)= psi(i)-beta(i);
theta4(i)= 180-lambda(i)-beta(i);
if theta2(i)> 180
theta3(i)=psi(i)+beta(i);
theta4(i)=180-lambda(i)+beta(i);
end
%Define joint positions
Ox(i)=0;
Oy(i)=0;
Ax(i)=Ox(i)+L_1*cosd(theta2(i));
Ay(i)=Ox(i)+L_1*sind(theta2(i));
Bx(i)=Ox(i)+Ax(i)+L_2*cosd(theta3(i));
By(i)=Oy(i)+Ay(i)+l_2*sind(theta3(i));
Cx(i)=L_0;
Cy(i)=0;
Px(i)=Ax(i)+L_PA*cosd(alpha+theta3);
Py(i)=Ay(i)+L_PA*sind(aplha+theta3);
plot ( [Ox(i) Ax(i)], [Oy(i) Ay(i)], [Ax(i) Bx(i)], [Ay(i) By(i)],...
[Bx(i) Cx(i)], [By(i) Cy(i)],'LineWidth',3);
plot([Ax(i) Px(i)], [Ay(i) Py(i)], [Px(i) Bx(i)], [Py(i) By(i)], 'LineWidth',3);
hold on;
grid on;
axis equal;
axis( [-5 15 -5 10] );
drawnow;
hold off;
end
Array indices must be positive integers or logical values.

Answers (2)

Voss
Voss on 6 May 2022
There is an error that occurs before the loop is entered:
clear all; %#ok<CLALL>
close all;
L_0=7;
L_1=3;
L_2=7.5;
L_3=4.5;
L_PA=4;
alpha=35;
w2=5;
theta2=0:2:360;
figure(1);
for i=1:length(theta2(i))
% ...
% stuff
% ...
end
Array indices must be positive integers or logical values.
i is not defined before the for loop, so MATLAB uses the imaginary unit 1i in
theta2(i)
which causes an error because 1i is not a valid index. I imagine that was a typo - clearly length(theta) was intended there.
If you make that change, next you'll run into an error on the first line inside the for loop:
theta2(i)=0:2:360;
because theta2(i) is one element and 0:2:360 is a vector with more than one element. That line can be removed.
Then you'll run into an error because sqrtL_0 is not defined. That one I don't know how to fix.

Walter Roberson
Walter Roberson on 6 May 2022
for i=1:length(theta2(i))
At the point in your code where you are constructing the bounds of the for loop, the bounds to the right of the = are evaluated before any assignment is done to the loop control variable. Therefore, your theta2(i) needs to be evaluated before anything gets assigned to i because of the for loop. But you have not assigned any value to i before that point, so i is going to have its default value, which is sqrt(-1) ... which is not a valid index.
If i were a valid scalar index at that point, then theta2(i) would refer to a single location, and length() of that would be 1. length(theta2(i)) would always be the same as length(i) provided that all values in i are within the bounds of theta2 . So you might as well code the clearer
for i = 1 : length(i)
after having assigned something to i
... Or perhaps what you wanted was
for i = 1 : length(theta2)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!