配列インデックスのエラーメッセージについて
    68 views (last 30 days)
  
       Show older comments
    
下記のようなプログラムで、エラーメッセージが出ます。どのように直せば良いですか。
>> x=2*pi*(0:0.001:1);
>> tic
>> for j=0:0.0001:1
y1(j)=cos(x(j));
end
配列インデックスは正の整数または logical 値でなければなりません。
>> time1=toc
0 Comments
Accepted Answer
  Yoshio
    
 on 27 Oct 2019
        
      Edited: Yoshio
    
 on 27 Oct 2019
  
      MATLABAの配列は、Cと異なりインデックスは1から始まりますので、配列xの最初の要素はx(1)です。
このため、for文でx(0)を参照したため、エラーとなっています。
以上を参考にコードを見直してみてください。
なお、MATLABの強みは、関数の入力として、ベクトルや行列を扱うことができることです。
forを使ったコードができたら、forを使わないで計算する方法も検討してみてください。
2 Comments
  Yoshio
    
 on 28 Oct 2019
				一点追加ですが、エラーメッセージの意味ですが、
配列インデックスは正の整数
ですので、j=0:0.0001:1でx(j)は「配列のインデックス指定が正の整数」ではないので、
x(0)を参照したエラーとx(0.0001)   の両方を回避する必要があります。x(0)だけではありませんのでご注意ください。
More Answers (1)
  Ganchimeg
 on 18 Jul 2023
        clear clc phi1_Link(1)=0; %一般化座標 phi1_dot_Link(1)=0; %一般化速度 phi1_ref(1)=-45/180*pi; %目標関節角度 phi_motor(1)=0; omega_motor(1)=0; phi1_dot_ref(1)=0; %目標関節各速度 phi1_dot_motor(1)=0; K_p=1.5949*10^3; % C_v=0.1546; % %K_f=54.83; %Pゲイン %C_f=1.5889; %Dゲイン K_f=54.83; %Pゲイン C_f=1.5889; %Dゲイン dTime=0.001; %シミュレーション刻み幅 TotalTime=2; %シミュレーション時間 Time=TotalTime/dTime; M_total=1.612; M=M_total/2; G=-9.8; l=0.3; l_a=0.0125; %各リンクの慣性テンソル r=0.02; %円柱の半径 I=1/4*M*r^2+1/12*M*0.3^2; I_m=(1/3)*M*(l_a^2+l_a*l+l^2); for i=1:dTime:TotalTime
tau_motor(i)=K_p*((phi1_ref(i)-phi1_Link(i)))-C_v*(phi1_dot_ref(i)-phi1_dot_Link(i)); tau_SEA(i)=K_f*(phi_motor(i)-phi1_Link(i))+C_f*(phi1_dot_motor(i)-phi1_dot_Link(i)); a_motor(i)=(tau_motor(i)-tau_SEA(i))/I_m; omega_motor(i+1)=omega_motor(i)+ a_motor(i)*dTime; phi_motor(i+1)=phi_motor(i)+omega_motor(i)*dTime; %リンクのダイナミクス tau_g(i)=M_total*G*(l/2)*sin(phi1_Link(i)); a_Link(i)=(tau_SEA(i)-tau_g(i))/I; phi1_dot_Link(i+1)=phi1_dot_Link(i)+ a_Link(i)*dTime; phi1_Link(i+1)=phi1_Link(i)+phi1_dot_Link(i)*dTime; end figure(1) plot(Time, phi1_Link) legend('theta_1') title('Link1の角度') figure(2) plot(Time, phi1_dot_Link) legend('theta_dot_1') title('Link1の角速度') figure(3) plot(Time, phi_motor) legend('theta_motor') title('Motorの角度') figure(4) plot(Time, omega_motor) legend('omega_motor') title('Motorの角速度')
1 Comment
  Ganchimeg
 on 18 Jul 2023
				これを実行させたら (配列インデックスは正の整数またはlogical値でなければなりません。) このようなエラーが出ましたが、どのように解決すれば宜しいでしょうか?
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

