필터 지우기
필터 지우기

Computing derivative using "for" loop--why am I getting this error?

조회 수: 9 (최근 30일)
Kelsey
Kelsey 2014년 6월 5일
댓글: dpb 2014년 6월 6일
Below is my code (note that in reality the vectors are much longer, but I simplified them to 5 entries of fake data...you get the picture). THIS code works, but when I use the actual data (each parameter is a 1x5036 matrix, as opposed to the 1x5 matrices that I present here for simplicity), I get a constant value of 0.75 for VehicleAccel for all t. Obviously, this is not the true value, as the data was for a real-world drive I did in a car around town (I have attached my actual m-file with the data). What is the problem?? Thanks in advance!
t = [1 2 3 4 5]; % sec
FLwhlRotation = [6.8 6.9 7.0 7.1 7.2]; % rad/s
FRwhlRotation = [6.8 6.9 7.0 7.1 7.2]; % rad/s
RLwhlRotation = [6.8 6.9 7.0 7.1 7.2]; % rad/s
RRwhlRotation = [6.8 6.9 7.0 7.1 7.2]; % rad/s
AvgWhlRotation = (FLwhlRotation + FRwhlRotation + RLwhlRotation + RRwhlRotation)./4; % rad/s
AvgWhlSpeed = AvgWhlRotation.*0.3683; % m/s (R = 0.3683 m)
VehicleSpeedMPH = AvgWhlSpeed.*2.2369; % mi/hr
VehicleSpeedKPH = AvgWhlSpeed.*3.6; % km/hr
% Instantaneous Acceleration (m/s^2)
VehicleAccel=0;
for i=1:4
VehicleAccel = (AvgWhlSpeed(i+1)-AvgWhlSpeed(i))./(t(i+1)-t(i));
end
%Plots
plot(t,AvgWhlSpeed,'b',t,VehicleAccel,'r')
legend('Speed (m/s)','Acceleration (m/s^2)')
xlabel('Time (s)')
  댓글 수: 3
Kelsey
Kelsey 2014년 6월 6일
When I run that, I get an error message saying that it couldn't plot because the vectors aren't the same lengths (i.e., VehicleAccel is 1x5035, while t is 1x5036). Any idea what the issue is?
dpb
dpb 2014년 6월 6일
Of course...when you take the first differences there's one less value left than you start with. You don't show how you actually did the plot shown but your looping solution has the same issue; your initial code shows
for i=1:4
accel(i)=(spd(i+1)-spd(i))./(t(i+1)-t(i));
which leaves you with four accel values calculated from a time vector of length five. I presume you probably augmented the accel vector w/ a leading zero.
The vectorized solution would be
VehicleAccel = [0 diff(AvgWhlSpd)./diff(t)];

댓글을 달려면 로그인하십시오.

답변 (1개)

Star Strider
Star Strider 2014년 6월 5일
Change this line to create VehicleAccel as a vector and see if that improves things:
VehicleAccel(i) = (AvgWhlSpeed(i+1)-AvgWhlSpeed(i))./(t(i+1)-t(i));
Also, it’s best not to use ‘i’ and ‘j’ as loop indices. MATLAB uses them for its imaginary operators, and could cause confusion if you use them also as variables.
  댓글 수: 2
Kelsey
Kelsey 2014년 6월 6일
편집: Kelsey 2014년 6월 6일
Thanks!! That did it, I think...how does this plot look?
Would you say the acceleration appears to accurately represent the derivative of the speed?
Star Strider
Star Strider 2014년 6월 6일
My pleasure!
I would. The derivative should be slightly lower than baseline for the areas where the speed is linearly decreasing, and it seems to be. I opened a larger version in a new tab in Firefox, and it looks good. The derivatives of the parabolas look appropriately linear.

댓글을 달려면 로그인하십시오.

카테고리

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