MATLAB Answers

Array indices must be positive integers or logicsal values

3 views (last 30 days)
Ethan Squibb
Ethan Squibb on 24 Feb 2020
Edited: Matt J on 24 Feb 2020
I've been trying to code a min and max calculator for a function named Ey. However, it says that my array indices must be positive integers or logical values. I've kept trying new things but cannot get anything to work. Can anyone solve this for me? Thanks, probably something stupid, very new to matlab.
clear; clc;
%input data
r=0.35; %length of crank OA in m
a=0.65; %length of the distnace BA in m
b=0.5; %length of the distnace AC in m
m=0.8; %ength of the link CD in m
d=1.3; %distance(m) of vertical axis of third slider from O
t=0.75; %length of the length DE
w=1.5; %angular velocity of crank in rad/s
%===============computaion===================
%p=alpha
%q=theta
k=0;
for q=0:1:6283
k=k+1;
p=asin((7*sin(q))/13);
v(k)=(a+b)*sin(p);
end
p=asin((7*sin(q))/13);
v=(a+b)*sin(p);
DF=sqrt((m^2)-(v.^2));
OF=(a+b)*cos(p)-(13*sin(pi-q-p)/20.*sin(q));
OD=OF+DF;
Ey=sqrt(t^2-(d-OD).^2);
%NOTE ===== try and divide 2pi by 1000, then change step to 1, then just
%divide at the end.
k=0; step=1;
for q=0:step:6283
k=k+1;
x(k)=q;
y(k)=Ey(q);
end
min=y(1);indx1=1;
for i=2:k
if y(i)<min;
min=y(i);
indx1=i;
end
end
max=y(1); indx2=1;
for i=2:k
if y(i)>max;
max=y(i);
indx2=i;
end
end
disp('Min of Ey = ');
disp(min);
disp('obtained at q = ');
disp(q_min);
plot(x,y);
for q=0:step:2*pi
k=k+1;
y(k)=Ey(q);
x(k)=q;
if(Ey(k)<min)
min=Ey(k);
q_min=q(k);
end
end

  0 Comments

Sign in to comment.

Answers (1)

Matt J
Matt J on 24 Feb 2020
Edited: Matt J on 24 Feb 2020
The intention of this part of the code is unclear
k=0; step=1;
for q=0:step:6283
k=k+1;
x(k)=q;
y(k)=Ey(q);
end
Aside from q=0 being an invalid index, Ey is a scalar, so indexing at q>1 would also be invalid.
K>> Ey
Ey =
0.4466

  0 Comments

Sign in to comment.

Sign in to answer this question.

Products


Release

R2019a