How to plot multiple lines (with a varying slope coefficient) in the same graph?
15 views (last 30 days)
Show older comments
Hi.
I devised a script that runs a rudimentary dynamical simulation of a cornering (hybrid) vehicle, calculating lateral velocity and total time required to perform a full rotation around a circle(hypothesizing that the engine will be producing the exact amount to offset all the longitudinal forces), setting a specific radius of the turn and the steering angle of the front wheel. (I had to initialize lift and drag forces as 1 to make the code run the first iteration of the while circle. I know it gives a wrong result on the first millisecond but i haven't found a solution to that either, for now, help with that would also be appreciated!)
It runs smoothly when i give it a specific value on the steering angle, for example:
delta = deg2rad(5)
My code produces this graph:
However, i want to plot on the same graph multiple curves of space/time that are produced with different values of the steering angle.
I know that i'd be expecting something like this: (Forgive my terrible microsoft paint skills)
Here is the part of the code that works.
clear;
close all;
clc;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PARAMETERS
rho = 1.225; % Air density standard conditions: kg/m^3
g = 9.81; % Acceleration of gravity: m/s^2
la = 0.85; % Distance center of gravity - front axis: m
lb = 0.7; % Distance center of gravity - rear axis: m
wb = la + lb; % Wheel base: m
h = 0.25; % Height center of gravity: m
laero = 1; % Distance aerodynamic center - front axis: m
haero = 0.35; % Height aerodynamic center: m
SLift = 1.54; % Lift surface: m^2
SDrag = 0.55; % Drag surface: m^2
cL_close= 2.4; % Lift coefficient, DRS closed: /
cD_close= 1.65; % Drag coefficient, DRS closed: /
W_ICE = 60000; % Internal combustion engine power: W
W_EL = 20000; % Electric motor power: W
f = 1.2; % Friction coefficeint: /
m = 290; % Total vehicle mass: kg
dt = 0.001; % Discretization time: s
radius = 8.375; % Curvature Radius: m
kLc = 0.5*cL_close*rho*SLift; % Lift constant DRS closed: kg/m
kDc = 0.5*cD_close*rho*SDrag; % Drag constant DRS closed: kg/m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% INITIALIZATION
t_3(1) = 0; % Time: s
x_3(1) = 0; % Space: m
front_acc_3(1) = 0; % Frontal Acceleration: m/(s^2)
Nr_3(1) = m*g*la/wb; % Vertical reaction rear axis: N
LD_3(1) = la/wb; % Load distribution wrt rear axis: /
delta = deg2rad(5); % Steering Angle: radians
Lift_3(1) = 1; % -Lift force: N
Drag_3(1)= 1; % Drag force: N
i = 1; % Generic index
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
while x_3 <= 2 * pi * radius
Torque(i) = (2 * sin(delta)) * f * (1-LD_3(i))*(m * g + Lift_3(i))+ Drag_3(i) ;
Front_force(i) = (Torque(i) - Drag_3(i)) / sin(delta) ;
Rear_force(i) = (Front_force(i) * cos(delta))/lb;
vel_3(i) = sqrt((radius*(Rear_force(i) + cos(delta) * Front_force(i)))/m);
Lift_3(i+1) = kLc * vel_3(i)^2;
Drag_3(i+1) = kDc * vel_3(i)^2;
lat_acc_3(i+1) = vel_3(i)^2 / radius;
Nr_3(i) = (m * g * la + haero * Drag_3(i) + laero * Lift_3(i)) / wb;
LD_3(i+1) = Nr_3(i) / (m*g + Lift_3(i));
x_3(i+1) = x_3(i) + vel_3(i)*dt;
t_3(i+1) = t_3(i) + dt;
i = i + 1;
end
plot(t_3,x_3);
legend("Location","eastoutside")
ylim([0,55]);
yline(2 * pi * radius)
grid on
xlabel("Time")
ylabel("Distance")
title("Distance over Time")
I thought of using:
delta = deg2rad(5:1:20)
Which would give me a vector of the range of angles i was planning to study in the Script.
I then planned to replace delta in the code with delta(index).
How can i make it so the code runs with delta(1), produces a graph, holds on the result, then graphs delta(2), and so on until delta(15), all in the same graph?
Thanks in advance.
0 Comments
Answers (1)
ANKUR KUMAR
on 16 Nov 2021
Edited: ANKUR KUMAR
on 16 Nov 2021
You code is producing the same results for all iteration.
You can simply use a for loop, and use hold on to plot multiple plots in a plot.
clear;
close all;
clc;
delta_raw = deg2rad(5:1:20)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PARAMETERS
rho = 1.225; % Air density standard conditions: kg/m^3
g = 9.81; % Acceleration of gravity: m/s^2
la = 0.85; % Distance center of gravity - front axis: m
lb = 0.7; % Distance center of gravity - rear axis: m
wb = la + lb; % Wheel base: m
h = 0.25; % Height center of gravity: m
laero = 1; % Distance aerodynamic center - front axis: m
haero = 0.35; % Height aerodynamic center: m
SLift = 1.54; % Lift surface: m^2
SDrag = 0.55; % Drag surface: m^2
cL_close= 2.4; % Lift coefficient, DRS closed: /
cD_close= 1.65; % Drag coefficient, DRS closed: /
W_ICE = 60000; % Internal combustion engine power: W
W_EL = 20000; % Electric motor power: W
f = 1.2; % Friction coefficeint: /
m = 290; % Total vehicle mass: kg
dt = 0.001; % Discretization time: s
radius = 8.375; % Curvature Radius: m
kLc = 0.5*cL_close*rho*SLift; % Lift constant DRS closed: kg/m
kDc = 0.5*cD_close*rho*SDrag; % Drag constant DRS closed: kg/m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for index = 1:size(delta_raw,2)
% INITIALIZATION
t_3(1) = 0; % Time: s
x_3(1) = 0; % Space: m
front_acc_3(1) = 0; % Frontal Acceleration: m/(s^2)
Nr_3(1) = m*g*la/wb; % Vertical reaction rear axis: N
LD_3(1) = la/wb; % Load distribution wrt rear axis: /
delta = delta_raw(index); % Steering Angle: radians
Lift_3(1) = 1; % -Lift force: N
Drag_3(1)= 1; % Drag force: N
i = 1; % Generic index
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
while x_3 <= 2 * pi * radius
Torque(i) = (2 * sin(delta)) * f * (1-LD_3(i))*(m * g + Lift_3(i))+ Drag_3(i) ;
Front_force(i) = (Torque(i) - Drag_3(i)) / sin(delta) ;
Rear_force(i) = (Front_force(i) * cos(delta))/lb;
vel_3(i) = sqrt((radius*(Rear_force(i) + cos(delta) * Front_force(i)))/m);
Lift_3(i+1) = kLc * vel_3(i)^2;
Drag_3(i+1) = kDc * vel_3(i)^2;
lat_acc_3(i+1) = vel_3(i)^2 / radius;
Nr_3(i) = (m * g * la + haero * Drag_3(i) + laero * Lift_3(i)) / wb;
LD_3(i+1) = Nr_3(i) / (m*g + Lift_3(i));
x_3(i+1) = x_3(i) + vel_3(i)*dt;
t_3(i+1) = t_3(i) + dt;
i = i + 1;
end
plot(t_3,x_3);
hold on
end
legend("Location","eastoutside")
ylim([0,55]);
yline(2 * pi * radius)
grid on
xlabel("Time")
ylabel("Distance")
title("Distance over Time")
0 Comments
See Also
Categories
Find more on Geographic Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!