MATLAB Answers

Trouble calculating Acceleration from given velocity vector.

13 views (last 30 days)
Connor Watkins
Connor Watkins on 7 Apr 2020
Commented: Connor Watkins on 7 Apr 2020
I want to create a for loop to use the iterative formula for acceleration using given velocity values:
ai = (vi+1 - vi) / dt
I have tried repeatedly but am new to MATLAB. I used the following:
a = diff(V)/diff(T)
where V is velocity and T is time
but the acceleration vector is then 1 smaller than I want to be able to plot the graph a,T
forgive my poor syntax, I am very new to MATLAB
In summary, I need to calculate Acceleration and keep it in the same vector size as Time in order to plot these, not one value smaller.

  2 Comments

Connor Watkins
Connor Watkins on 7 Apr 2020
%Data was imported as a table using the import wizard, found in the home section.
A = readtable('Log_U0006387_160311_740d0(1).csv');
%% Electric Vehicle Performance
%Starting by extracting each data set from the table
%Converting where necessary immediately to avoid mess
Time = A{:,1}; %Time in seconds
Speed = A{:,3}*1.60934; %Speed in KpH
Gear = A{:,142}; %Gear used
Elevation = A{:,2}/3.281; %Elevation in metres
SOC = A{:,5}/10000; %State of Charge in '%'
PackVolts = A{:,7}; %Battery Voltage in Volts
PackAmps = A{:,8}; %Battery Current in Amps
%Plotting the graphs
%First I create my subplot figure, with 6 rows and 1 column
subplot(6,1,1)
plot(Time, Speed, 'r'), xlabel('Time (s)'), ylabel('Speed (kph)') %I then plot Speed against Time in pos. 1
%Then I create the second subplot, being Gear over Time
subplot(6,1,2), plot(Time, Gear, 'b'), xlabel('Time (s)'), ylabel('Gear')
%Third subplot - Elevation over Time
subplot(6,1,3), plot(Time, Elevation, 'magenta'), xlabel('Time (s)'), ylabel('Elevation (m)')
%Fourth subplot - State Of Charge over Time
subplot(6,1,4), plot(Time, SOC, 'Yellow'), xlabel('Time (s)'), ylabel('SOC (%)')
%Fifth subplot - Battery voltage over Time
subplot(6,1,5), plot(Time, PackVolts, 'g'), xlabel('Time (s)'), ylabel('Voltage (Volts)')
%Sixth subplot - Battery Current over Time
subplot(6,1,6), plot(Time, PackAmps, 'cyan'), xlabel('Time (s)'), ylabel('Current (Amps)')
%%Power Consumption
%Data Extraction
MotorPwr = A{:,133}/10; %Motor Power in kW
AuxPwr = A{:,134}/10; %Auxillary Equipment Power in kW
ACPwr = A{:,135}/5; %Air Conditioning in kW
%Battery Power Calculation
BatPwr = PackVolts.*PackAmps/ 1000; %Power in Watts
%Powers against Time Plot
figure (2)
plot(Time, MotorPwr, '-.r')
grid on
title('Motor, Auxillary, A/C and Battery Power over Time')
hold on
plot(Time, AuxPwr, ':k')
plot(Time, ACPwr, '--b')
plot(Time, BatPwr, 'g')
hold off
xlabel('Time (s)')
ylabel('Power (kW)')
legend('MotorPwr','AuxPwr','ACPwr','BatPwr')
%% Acceleration and Driving Styles
%Converting speed into metres per second
V = Speed.*1000/3600 ;
a0=; %Similarly to given equation, Delta V/ Delta T = A in m/s^2
if a <0
a=inv(a); %To receive positive values for acceleration
end
%If statements for the Driving Style Mapping
if a >= 0.7
ds_mapping = 1;
elseif a <= 2.81
ds_mapping = 2;
elseif a <= 3.65
ds_mapping = 3;
else
ds_mapping = 0;
end
The bottom section (Acceleration and Driving Styles section) is the area which is giving me trouble

Sign in to comment.

Answers (1)

darova
darova on 7 Apr 2020
So what about this formula?
a = diff(V)/diff(T)
Just forgot about ./ element-wise operator
a = diff(V)./diff(Time);
Then use for loop
ds_mapping = a*0;
for i = 1:length(a)
if a(i) >= 0.7
ds_mapping(i) = 1;
elseif a <= 2.81
ds_mapping(i) = 2;
elseif a <= 3.65
ds_mapping(i) = 3;
end
end
What are trying to do here? You want absolute value ?
if a <0
a=inv(a); %To receive positive values for acceleration
end
% a = abs(a);
Note: once you used diff you have one element less (because it calculates difference v(i+1)-v(i))

  3 Comments

Connor Watkins
Connor Watkins on 7 Apr 2020
I'm trying to plot a graph of acceleration over time, which means I need to increase the size of the accceleration vector back to the Time vector size. I then want the if statement to deduce values for driving style (either 1, 2, 3 or 0) based on acceleration so that I can plot that over the top of the acceleration.
At the minute, the ds_mapping only receives one value which is 0.
Thank you for you help so far by the way, appreciate the response :)
darova
darova on 7 Apr 2020
  • I need to increase the size of the accceleration
Or you can plot without first value
plot(Time(2:end),a)
a(i) can be greater 0.7 and less than 2.81. What then?
if a(i) >= 0.7
elseif a <= 2.81
Connor Watkins
Connor Watkins on 7 Apr 2020
a(i) can be greater 0.7 and less than 2.81. What then?
the idea was that the other 'elseif's would cover what would happen in those circumstances. So if the value was above 2.81 but below 3.64 then its ds_mapping = 2, higher than 3.64 but lower than 6.5: ds_mapping = 3 and if it doesnt apply to any of these, the ds_mapping is zero.
I tried using && to allow me to have multiple conditions for each if statement "if a <=0.7 && a >= 2.80" for instance, but it didnt seem to like the use of && or ||.
thank you for the suggestion of plotting Time from 2:end instead of the whole vector. I didn't think of that

Sign in to comment.

Sign in to answer this question.

Products


Release

R2020a

Translated by