Trouble calculating Acceleration from given velocity vector.

16 vues (au cours des 30 derniers jours)
Connor Watkins
Connor Watkins le 7 Avr 2020
Commenté : Saad Hussain le 6 Mai 2021
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.
  3 commentaires
Connor Watkins
Connor Watkins le 7 Avr 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
Saad Hussain
Saad Hussain le 6 Mai 2021
Hey i was wondering if you still have your code file saved to this assignment and you can send me? ive got a similar one

Connectez-vous pour commenter.

Réponses (1)

darova
darova le 7 Avr 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 commentaires
darova
darova le 7 Avr 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 le 7 Avr 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

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by