A brief introduction to the imshow function in MATLAB.

imshow(array，[low,high])

Among them, array is the input matrix, the elements with a value of < = low in array will be displayed as black in the figure window, while the elements with a value of > = high will be displayed as white, the elements between low and high will be displayed as gray in varying degrees, and the closer to low, the closer to black.

The visualization of NaSch single lane model written with MATLAB function relies on imshow function.

Another special value that must be said is Nan (Nan), which is the abbreviation of not a number. Nan has several particularities.

First, nan and any number involved in the operation result are still nan.

Second, nan is not a number or a number, that is, nan and nan are different, just as all real numbers and all even numbers are different, any two nan are different in MATLAB.

Third, Nan can also be used as a function. The use method of nan() is the same as that of zeros() or ones().

Because of the particularity of Nan, we can use nan to represent the cell car free state, and 0 to represent the stationary car, so that we can distinguish the car free from the stationary car.

In addition, the input parameters of the imshow function of nan are still special.

A simple example is given to illustrate the particularity of nan's parameters in the imshow function.

Do not set the output result of low and high:

Set the output result of low and high

It can be seen that in imshow (array,[low,high]), when nan is included in the array, nan will always be regarded as smaller than low (the actual nan and any number comparison return a logical value of 0). It has been said that nan is used to represent the cell no car state, so when nan is used as the input parameter of imshow, the lane no car position will always be black, which is obviously not the result we want.

The solution is to define a space matrix of 3 * space range (1,3, behavior roadside, 2 behavior lanes) in addition to V (the matrix storing vehicle speed information) to transform v. the following operations can be used as the input parameters of imshow, where space range is the length of the road.

So the basic idea of visualizing with imshow function is to store vehicle speed information with v, where nan represents no vehicle, 0 represents stationary vehicle, number represents vehicle speed, and then space is used as input parameter of imshow, where space is to set the value of nan element in v to 1, and then make all non nan elements in the second row (Lane) of space reverse (to meet the second characteristic of imshow), so In this way, since space must be 1 (nan will be converted to 1 to match imshow output) after inversion, no car is empty, and the color of the car tends to be black as the speed increases.

The following is a brief description of the operation process.

Create v and space matrices

v = NaN (1,space_range); %Establish v To store time Vehicle speed on cells of time step %% Create the space space=zeros(3,space_range); space([1,3],:) = 0.5; %Lane is gray

In this way, we can output the initial road conditions (vehicle release, which is included in the code after parameter setting, and will not be described here)

%% Show initial traffic flow chart figure('name','NaSch Single lane model','position',[241 132 560 420],'doublebuffer','on');%Window name, location,Double buffering space(2,:) = -1*space(2,:); H=imshow(space,[]); title('NaSch Single lane model','color','red');

It should be noted here that space should be reversed, otherwise, no car is black and no car is white, which is different from our general habits.

Finally, output the image in the last line of the time step cycle

%% Show traffic flow chart space(2,:) = v; %space Lane 2 space = -1*v; %because imshow Function properties, space Middle value reverse output space(isnan(space)) = 1; %White for no car set(H,'CData',space); %% Output picture transformation interval pause(time_span);

Notice here

First, the isnan (array) function can return a logical matrix of the same type as array, where the value at the place where the array element is nan is 1, otherwise it is 0.

Second, the call format of set function is:

Set (handle, property name 1, property value 1, property name 2, property value 2 )

That is, the set function can change the property value that the handle (which can be understood as advanced pointer) points to the object

Third, the pause (a) function is used to pause the program execution for a second

I post the full code

clear;clc; %% Parameter setting space_range=100; %Space range (lane length) MinTime = 0; %Minimum time step MaxTime = 1000; %Maximum time step v_max = 5; %Maximum allowable speed pro_slowdown=0.3; %Slowing down probability time_newcar=1; %every other time_newcar Time step for a car time_span = 0.1; %Picture output interval %% Preparation space memory=[]; %Establish memory To store the data generated in each time step v = NaN (1,space_range); %Establish v To store time Vehicle speed on cells of time step %% Create the space space=zeros(3,space_range); space([1,3],:) = 0.5; %Lane is gray %% Show initial traffic flow chart figure('name','NaSch Single lane model','position',[241 132 560 420],'doublebuffer','on');%Window name, location,Double buffering space(2,:) = -1*space(2,:); %according to imshow Function characteristic space Take inverse H=imshow(space,[]);%Default parameters Wie[min(a),max(a)],This is because space The reverse must be the largest, so no car is empty, and the color of the car tends to be black as the speed increases title('NaSch Single lane model','color','red'); %% Along the driveway v_max For vehicles with a speed of half of the maximum speed at intervals for a=1:v_max:space_range v (a) = fix(v_max/2); end %% v_next = v; %v_next Changes used to store vehicles are time+1 Vehicle speed in time steps % Note: all speed changes in the following cycles are used v_next，The current time step state judgment is used v %% NaSch Four steps to create a vehicle model %% Time module for time = 1:1:MaxTime %% Store data memory = [memory;v]; %For each time step v All make memory A line %% Traverse the lane and carry out NaSch The four step for cell_i = 1:1:space_range if ~isnan( v(cell_i) ) %% accelerate v_next(cell_i) = min(v_next(cell_i)+1,v_max); %% Obtain cell_i The number of pre empty cells and whether it meets the cycle [cycle,empty]=get_empty(cell_i,v,v_max,space_range,v_next); %% judge cell_i Is the vehicle in get_empty Move has occurred in function if cycle %% Slow down v_next(cell_i) = min( v_next(cell_i) , empty ); %% Probabilistic slowing down if (rand(1) <= pro_slowdown) v_next(cell_i) = max(v_next(cell_i)-1,0); end %% Location update cell_next = cell_i + v_next(cell_i); v_next(cell_next) = v_next(cell_i); v_next(cell_i) = NaN; %Cell changes to no car before position update else %% The periodic boundary condition returns the first vehicle to the left of the lane at the original speed for cell_front = 1:fix(space_range/10) if isnan( v_next(cell_front) ) %The impact of vehicle changes here is time+1 Time steps, not time Time step v_next( cell_front ) = v_next( cell_i ); v_next(cell_i)=nan; end end end end end v =v_next; %v_next As the next step v Continue circulation %% Show traffic flow chart space(2,:) = v; %space Second act vehicle space = -1*v; %because imshow Function properties, space Middle value reverse output space(isnan(space)) = 1; %White for no car set(H,'CData',space); %% Output picture transformation interval pause(time_span); end %% %Used for finding out cell_i With the number of empty cells in front and the possibility of the next step space_range-1 Head car ...Move back to the leftmost first empty cell and return cycle，If the return value is 1, the head car will move; if it is 0, it will not move % output EmptyFront by cell_i(Cell number)Number of empty cells in front of the cells (more than v_max Press v_max Count) % v_max Maximum speed % space_range Space range (lane length) function [cycle,EmptyFront] = get_empty(cell_i,v,v_max,space_range,v_next) EmptyFront = 0; cycle=1; if (cell_i+v_max) < (space_range-v_max ) %Judge whether the cell reaches the boundary(space_range-v_max) %% reach cell_i Number of empty cells in front for front= ( cell_i + 1 ) : ( cell_i + v_max ) if ~isnan( v( front ) ) EmptyFront = front- (cell_i+1); break; end EmptyFront = v_max; end else %% Judge whether it is the first car first = 1; for front= min( ( cell_i + 1 ),space_range ) : min( ( cell_i + v_max ),space_range ) if ~isnan( v( front ) ) EmptyFront = front- (cell_i+1); %Not the first car EmptyFront first =0; break; end end if first %% Judge the next time step of the first car if possible to move to space_range-1 place if ( v_next(cell_i)+cell_i ) >= (space_range-1) cycle = 0; else EmptyFront =v_max; %As long as the first car doesn't arrive the next second space_range Number of empty cells in front by v_max remember end end end end

Finally, note that this program consists of two functions