### Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

## Pulse and Glide Looping Problem

Alex Deegan

### Alex Deegan (view profile)

さんによって質問されました 2019 年 2 月 19 日 16:08

### Alex Deegan (view profile)

さんによって コメントされました 2019 年 2 月 20 日 15:00
Geoff Hayes

### Geoff Hayes (view profile)

さんの 回答が採用されました
I have this script which is being used for a project, the for and while loops run succesfully, however i wish to be able to carry the final velocity and displacement values from one for loop into the beginning of the next every 2090m for continuity as it currently resets to zero at the beginning of each loop. Is there anyone who might be able to help me please.
%Driving Parameters
clear vars;
close all;
Incremental_Distance=2090;%distance per section of the designated route [m]
v_max=16; %Maximum pulse speed [m/s]
v_min=7; %Minimum coast speed [m/s]
%
%resistance
Cd=0.09; %Coefficient of aero drag
RRt=0.0014; %Tyre rolling resistance
RRh=0.0014; %Hub rolling resistance
Af=0.9; %Frontal area of the vehicle [m^2]
%
%Pressure and Temp
P_Air=101325; %Air Pressure [Pa]
T_AirC=25; %Air temperature [Degree Celcius]
T_AirK=T_AirC+273.15; %Air temperature [Kelvin]
%
%Driving forces
Torque=450; %Engine torque(rated constant) [Nm]
Gr=1; %Effective gear reduction
Eft=0.9; %Transmission Efficiency
RPM=2500; %Engine RPM (rated constant) [rpm]
r=0.2; %Diameter of driving wheel [m]
theta = [0.0378;0.0072;0.0092];
%
%Physical Constants
g=9.812; %Acceleration due to gravity[m/s^2]
R=286.9; %Gas constant [J/Kg*k]
rho=(P_Air/(R*T_AirK)); %Density of air based on pressure and temperature [Kg/m^3]
Mc=200; %Mass of Car [Kg]
Md=65; %Mass of Driver [Kg]
Mt=Mc+Md; %Total mass of vehicle [Kg]
%
%Fuel Consumption
Mass_flow=2.164211E-05; % fuel flow rate from injector [Kg/s]
Fuel_Density=0.755; %density of fuel [Kg/l]
Pulse_width=5E-04; % [s]
%
%Coefficient Calculations
RR=((RRt*4)*(Mt/4)*g)+(4*RRh*Mt*g); %total rolling resistance of wheel assembly [N]
%
%initial conditions
x=zeros(length(theta)),1; %initial dispacement [m]
v=zeros(length(theta)),1; %initial Velocity [m/s]
Pulse_mark=1; %initial pulse mark
%
%incremental values
dt=0.1; %time step
Ft=(Torque*Gr*Eft)/r;
%
%Mathematical model0
k=1; %solution step
while x(i,k)<=Incremental_Distance
Daero(i,k) = 0.5*rho*Cd*Af*v(k)^2;
if Pulse_mark==1
else
end
v(i,k+1) = v(i,k) + a(i,k)*dt;
if v(i,k+1) >= v_max
Pulse_mark = 0;
v(i,k+1) = v_max;
elseif v(i,k+1) <= v_min
Pulse_mark = 1;
end
x(i,k+1) = x(i,k)+v(i,k+1)*dt;
k=k+1;
end
if i < length(theta)
v(i+1,1) = v(i,end);
x(i+1,1) = x(i,end);
k_steps(i+1,1) = k_steps(i,end);
end
%
% time(i) = k_steps(i,k)*dt;
%
end
xx=transpose(x);
vv=transpose(v);
v_column = [];
for ii=1:size(vv,2)
v_column = [v_column; vv(:,ii)];
end
x_column = [];
for ii=1:size(xx,2)
x_column = [x_column; xx(:,ii)];
end
%Fuel Consumption calculations
Total_time=time; %[s]
Total_Dispacement=x(i,k); %[m]
Final_Velocity=v(i,k); %[m/s]
% Average_Velocity=mean(v); %[m/s]
% Mileage_per_L=Total_Distance/Fuel_Consumed; %[m/L]
% Mileage=Mileage_per_L/1000; %[Km/l]
%Plots
% figure(4); plot(x_column(:),v_column(:),'.-'); title('pulse and glide'); xlabel('meters'); ylabel('m/s'); grid minor;
% figure(1); plot(k_steps,x_column); title('position'); xlabel('seconds'); ylabel('meters'); grid minor;
% figure(2); plot(k_steps,v_column); title('velocity'); xlabel('seconds'); ylabel('m/s'); grid minor;
figure(3); plot(x_column,v_column); title('pulse and glide'); xlabel('meters'); ylabel('m/s'); grid minor;

Geoff Hayes

### Geoff Hayes (view profile)

2019 年 2 月 19 日 17:07
Alex - by it currently resets to zero at the beginning of each loop do you mean that these values are reset when you call this script again? Or is there something in your above code that tells it to start again? If the former, then you could just change this script into a function that takes as input the velocity and displacement values that can be used to "seed" the current iteration (call) of this function. The function would then output the final velocity and displacement values that you would then use on a subsequent call to this function.
Alex Deegan

### Alex Deegan (view profile)

2019 年 2 月 19 日 17:18
I think its something wrong with my code that is causing this. This script is to model a vehicle undertaking a route split into 2090 m sections and rather than the velocity and displacement being maintained from one section (for loop) to the next, it is as if the vehicle starts from a stand still at the beginning of each loop.

サインイン to comment.

R2017b

## 1 件の回答

### Geoff Hayes (view profile)

2019 年 2 月 19 日 19:34
採用された回答

Alex - a couple of things. This code
x=zeros(length(theta)),1; %initial dispacement [m]
v=zeros(length(theta)),1; %initial Velocity [m/s]
Do you mean for these arrays to be 3x1 or 3x3? The placement of the ,1 is confusing. or should they be of some other dimension. Consider this code
while x(i,k)<=Incremental_Distance
As k increments on each iteration of the while loop, we keep updating the columns of x which suggests that the number of columns should be more than three (perhaps unknown or cannot be determined since velocity impacts the displacement which is conditioned on the Incremental_Distance for each "step".
And in order to pass on the last displacement and last velocity from the previous "step" I think that what you are doing is correct
if i < length(theta)
v(i+1,1) = v(i,end);
x(i+1,1) = x(i,end);
% k_steps(i+1,1) = k_steps(i,end);
end
I've commented out k_steps since that is previously undefined and throws an error. This should be fine except what does x really mean? It is the displacement on that step or leg of the journey which causes a problem with
while x(i,k)<=Incremental_Distance
Since (for example) x(2,1) will be at least 2090, then we will never enter the while loop for the second and all future iterations. Instead, you might want to try changing this condition to
while x(i,k) <= (i*Incremental_Distance)
And, it looks like that once you have finished, you convert the displacement and velocity matrices into column arrays with
xx=transpose(x);
vv=transpose(v);
v_column = [];
for ii=1:size(vv,2)
v_column = [v_column; vv(:,ii)];
end
x_column = [];
for ii=1:size(xx,2)
x_column = [x_column; xx(:,ii)];
end
You could possibly simplify this with reshape or with colon to get the column array that you want
v_column = v(:);
x_column = x(:);

Alex Deegan

### Alex Deegan (view profile)

2019 年 2 月 20 日 13:46
it is within the while loop, just at the very end
%Mathematical model0
for i=1:length(theta)
%coefficient calculations
%solution step
k=1;
while x(i,k)<=(i*Incremental_Distance)
Daero(i,k) = 0.5*rho*Cd*Af*v(k)^2;
if Pulse_mark==1
else
end
v(i,k+1) = v(i,k) + a(i,k)*dt;
if v(i,k+1) >= v_max
Pulse_mark = 0;
v(i,k+1) = v_max;
elseif v(i,k+1) <= v_min
Pulse_mark = 1;
end
x(i,k+1) = x(i,k)+v(i,k+1)*dt;
k_steps(i,k) = k;
k_steps(i,k+1) = k_steps(i,k)+dt;
k=k+1;
end
if i < length(theta)
v(i+1,1) = v(i,end);
x(i+1,1) = x(i,end);
k_steps(i+1,1) = k_steps(i,end);
end
%
% time(i) = k_steps(i,k)*dt;
%
end
Geoff Hayes

### Geoff Hayes (view profile)

2019 年 2 月 20 日 14:22
I don't understand
k_steps(i,k) = k;
k_steps(i,k+1) = k_steps(i,k)+dt;
if you initialize k_steps(i,k+1) on this iteration of the loop, then wouldn't you overwrite this update on the subsequent iteration of the loop with the k_steps(i,k) = k;?
Alex Deegan

### Alex Deegan (view profile)

2019 年 2 月 20 日 15:00
You are correct, removing k_steps(i,k) = k; resolves the problem, such a silly mistake.
Thank you so much, I have spent too long on this and the outside perspective has been a massive help!!

サインイン to comment.

Translated by