Fix "index exceeds number of array elements"

1 回表示 (過去 30 日間)
JAKE WISNIEWSKI
JAKE WISNIEWSKI 2021 年 9 月 20 日
コメント済み: JAKE WISNIEWSKI 2021 年 9 月 20 日
%% THIS IS MY CODE TO Solve HW 4
%% Clear memory
close all
clear all
clc
%%Given length of link
%L1=ground L2=crank L3=coupler L4=output
L1=[6 7 3 8 8 5 6]
L1 = 1×7
6 7 3 8 8 5 6
L2=[2 9 10 5 5 8 8]
L2 = 1×7
2 9 10 5 5 8 8
L3=[7 3 6 7 8 8 8]
L3 = 1×7
7 3 6 7 8 8 8
L4=[9 8 8 6 6 9 9]
L4 = 1×7
9 8 8 6 6 9 9
theta_x=[30 85 45 25 75 15 25]
theta_x = 1×7
30 85 45 25 75 15 25
xsize=7; %size of array
for i=1:xsize
theta_2(i)=theta_x(i)
end
theta_2 = 30
theta_2 = 1×2
30 85
theta_2 = 1×3
30 85 45
theta_2 = 1×4
30 85 45 25
theta_2 = 1×5
30 85 45 25 75
theta_2 = 1×6
30 85 45 25 75 15
theta_2 = 1×7
30 85 45 25 75 15 25
%%find theta angle
maxangle=90
maxangle = 90
for j=theta_x
s(j)=L1(j)^2+L2(j)^2-2*L1(j)*L2(j)*cos(theta_2(j))
mu(j)=acos((s(j)-L3(j)^2-L4(j)^2)/(-2*L3(j)*L4(j)));
if (mu(j)>maxangle)
mu(j)=mu(j)-90
end
end
Index exceeds the number of array elements (7).

採用された回答

Simon Chan
Simon Chan 2021 年 9 月 20 日
j is going to be the index and hence theta_x should not be use
Try the following:
for j=1:length(theta_x)
s(j)=L1(j)^2+L2(j)^2-2*L1(j)*L2(j)*cos(theta_2(j))
mu(j)=acos((s(j)-L3(j)^2-L4(j)^2)/(-2*L3(j)*L4(j)));
if (mu(j)>maxangle)
mu(j)=mu(j)-90
end
end
  1 件のコメント
JAKE WISNIEWSKI
JAKE WISNIEWSKI 2021 年 9 月 20 日
That was correct. Thank you very much.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by