Index exceeds the number of array elements (1).

5 ビュー (過去 30 日間)
Murad Alzahrani
Murad Alzahrani 2019 年 7 月 16 日
コメント済み: Walter Roberson 2019 年 7 月 16 日
clc;clear;
tf=10;
n=1;
t(n)=0.1;
dt=0.1;
G1(n)=1; G2(n)=0; G3(n)=-1; G4(n)=0;
m(n,:)=[1 0 -1 0];
while t(n)<=tf
m(n+1,:)=m(n,:)+slope(t(n),m(n,:))*dt;
n=n+1;
end
It shows me like Index exceeds the number of array elements (1)
Error in cars (line 8)
while t(n)<=tf
and this is my fucntion.
function [out] = slope(t,G)
k1=10;k2=20;m1=2;m2=4;
v1=G(1); x1=G(2);v2=G(3);x2=G(4);
out(1)=(-k1*x1+k2*(x2-x1))/m1;
out(2)=v1;
out(3)=-k2*(x2-x1)/m2;
out(4)=v2;
end
why? how I can fix it?
Thank you

回答 (1 件)

Walter Roberson
Walter Roberson 2019 年 7 月 16 日
You only assign to t(n) when n is 1, so t(1) exists but no other t location. You then increase n to 2 and expect t(2) to exist.
  1 件のコメント
Walter Roberson
Walter Roberson 2019 年 7 月 16 日
t(n) = t(n-1)+dt

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by