ERROR: Attempted to access dfi(3); index out of bounds because numel(dfi)=2.

1 次查看(过去 30 天)
Hello, this is the program I am talking about:
clc;
clear all;
% Vhodni podatki
Lz=0.5; %[m]
k=30; %[N/m]
g=9.81; %[m/s^2]
m=0.2; %[kg]
Tc=100; %[s] Čas celotnega opazovanega intervala
dt=0.001; %[s] Dolžina časovnega intervala od katerega je tudi odvisna napaka pri integriranju po Eulerju
B=0.05; %[N/(m*s)]
% Vodilni enačbi
ar=@(r,dr,fi,dfi) 1/m*(m*r*dfi^2-k*(r-Lz)+m*g*cos(fi)-B*dr);
af=@(r,dr,fi,dfi) 1/(m*r)*(-m*2*dr*dfi-m*g*sin(fi)-B*dfi*r);
% Začetni pogoji
rz=Lz;
drz=0;
fiz=pi/4;
dfiz=0;
% Stanje prve točke
t(1)=0;
r(1)=rz;
fi(1)=fiz;
dr(1)=drz;
dfi(1)=dfiz;
Fur(1)=B*dr(1);
Fuf(1)=B*r*dfi(1);
nihaj(1)=0;
ddr(1)=ar(r(1),fi(1),dr(1),dfi(1));
ddfi(1)=af(r(1),fi(1),dr(1),dfi(1));
i=2;
while nihaj<8;
t(i)=t(i-1)+dt;
r(i)=r(i-1)+dt*dr(i-1);
fi(i)=fi(i-1)+dt*dfi(i-1);
dfi(i)=dfi(i-1)+dt*ddfi(i-1);
dr(i)=dr(i-1)+dt*ddr(i-1);
ddr(i-1)=ar(r(i-1),dr(i-1),fi(i-1),dfi(i-1));
ddfi(i-1)=af(r(i-1),dr(i-1),fi(i-1),dfi(i-1));
i=i+1;
if sign(dfi(i))~=sign(dfi(i-1));
nihaj=nihaj+1;
end
end
T=t(end); % Čas 3.5 nihajev
Any1 has a clue where could be the problem? I keep getting the error above...
Regards
  5 个评论
Image Analyst
Image Analyst 2013-10-19
What few comments do exist are in a language that I don't understand. The rest of it is a confusing alphabet soup of variables that I have almost no hope of following. Why don't you ask the author of the code?
mcluka
mcluka 2013-10-20
I am the author of the code, the code just doesnt work. The program counts swings of a mass connected to a spring, the spring is then connected to a line, the total length of the spring+line equals Lz, B is the coefficient of wind resistance, so there is also wind resistance force, the rest is just physics. What I wanted to do is to count swings in a time lapse of Tc, the program would count the swings and stop at 3.5 swings, then i would see the time of 3.5 swings.
A little slovenian dictionary: nihaj=swing, stanje prve tocke=information for the first point, začetni pogoji=starting properties, vhodni podatki=main information
regards

请先登录,再进行评论。

采纳的回答

mcluka
mcluka 2013-10-20
Never mind, fixed the problem :)
regards

更多回答(1 个)

Anthony
Anthony 2013-10-20
change
i=i+1;
if sign(dfi(i))~=sign(dfi(i-1));
nihaj=nihaj+1;
end
to
if sign(dfi(i))~=sign(dfi(i-1));
nihaj=nihaj+1;
end
i=i+1;

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by