Good visualization in a car following model in MATLAB
9 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Please I am working on a matlab code for a car-following model and I used ode45 for the solution.
The positions(solution) for each car is good, but the visualization is not correct.
Please, I need help or assistance to make the visualization better.
The code has been attached below.
Thank you.
function xdot = f(t,x)
N = length(x); % number of vehicles
umax = 130/3.6; % maximum speed in m/s
L =1000;
xdot = zeros(N,1); % initialize derivative vector
for i = 1:N % loop over all vehicles
if i < N % use periodic boundary conditions
si = x(i+1) - x(i); % spacing between cars
else
si = (x(1) + L) - x(i);
end
if si > 100 % speed is maximum
xdot(i) = umax;
elseif si > 5 && si <= 100 % speed decreases linearly
xdot(i) = ((si - 5)/95)*umax;
else % speed is zero from 5 downward
xdot(i) = 0;
end
end
end
N = 50; % number of vehicles
T = 60; % simulation time in seconds
L = 1000;% Total length of the circular track in meters
R = L/(2*pi); %Radius of the circle to be created in meters
x0 = rand(N,1)*L; %Building a random starting point
x0 = sort(x0); %Looking for random starting point
[t,x] = ode45(@f,[0 T],x0); % solve ODEs using ode45
%figure; % create new figure
hold on; % keep previous plots
xlim([-R-5 R+5]) % Calibration of x-axis
ylim([-R-5 R+5]) % Calibration of y-axis
for k = 1:length(t) % loop over time steps
cla; % clear previous plot
plot(R*cos(x(k,:)),R*sin(x(k,:)),'.b','MarkerSize',30);% plot current positions
title(['Time: ' num2str(t(k)) ' s']); % display time
pause(0.1); % pause for animation speed
end
0 commentaires
Réponses (1)
albara
le 30 Avr 2023
Modifié(e) : albara
le 30 Avr 2023
I see that you are using a circular track and plotting the vehicles as points on the circle. One suggestion I have is to improve the visualization by displaying the cars as arrows instead of points. This would provide better visual cues about the car's direction and position on the track. Here's an updated version of your code to achieve this:
function xdot = f(t,x)
% ... (the code remains the same as in the original implementation)
end
N = 50; % number of vehicles
T = 60; % simulation time in seconds
L = 1000;% Total length of the circular track in meters
R = L/(2*pi); %Radius of the circle to be created in meters
x0 = rand(N,1)*L; %Building a random starting point
x0 = sort(x0); %Looking for random starting point
[t,x] = ode45(@f,[0 T],x0); % solve ODEs using ode45
%figure; % create new figure
hold on; % keep previous plots
xlim([-R-5 R+5]) % Calibration of x-axis
ylim([-R-5 R+5]) % Calibration of y-axis
% Plot the circular track
theta_track = linspace(0, 2*pi, 1000);
x_track = R * cos(theta_track);
y_track = R * sin(theta_track);
plot(x_track, y_track, 'k', 'LineWidth', 2);
for k = 1:length(t) % loop over time steps
cla; % clear previous plot
% Plot the circular track again
plot(x_track, y_track, 'k', 'LineWidth', 2);
% Plot the cars as arrows
for i = 1:N
xi = R * cos(x(k,i));
yi = R * sin(x(k,i));
dx = -R * sin(x(k,i));
dy = R * cos(x(k,i));
quiver(xi, yi, dx, dy, 0.05, 'b', 'LineWidth', 2, 'MaxHeadSize', 2);
end
title(['Time: ' num2str(t(k)) ' s']); % display time
pause(0.1); % pause for animation speed
end
This updated code plots the circular track itself and displays the cars as arrows to better represent their positions and directions. The length of the arrows is fixed, but you could potentially adjust them to reflect the speed of the cars if desired. This should make the visualization more informative and easier to understand.
Important: There may be some mistakes in this answer Experts can tell if there are any mistakes and you might find better answer
Voir également
Catégories
En savoir plus sur Axis Labels dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!