├── README.md ├── UrbanCitySimu.m ├── anish.fig ├── anish.m ├── equal.m ├── evaluation.m └── out.xls /README.md: -------------------------------------------------------------------------------- 1 | # VANET-Simulation-in-MATLAB 2 | 3 | Originally reactive protocols were not design for the characteristic of highly mobility during route discovery. Due to dynamically modification to the VANET this changes very often due to breakdown which causing excessive broadcasting and flooding the entire network in order for new routes to be discovered. In additional, the initial of routing need some time and this latency can easily change everything. Due to these reasons, the typical reactive protocols, in their current format, do not totally appropriate for time critical applications such as cooperative collision avoidance (CCA). The Cooperative Collision Avoidance is an important class of safety applications in VANETs, which aims at offering earlier warning to drivers using vehicle-to-vehicle (V2V) communication [13]. 4 | Ad Hoc On Demand Distance Vector (AODV) is an reactive routing protocolwhich capable of both unicast and multicast. In AODV, like all reactive protocols,topology information is only transmitted by nodes on-demand. When source hassomething to send then initially it propagates RREQ message which is forwarded byintermediate node until destination is reached. A route reply message is unicastedback to the source if the receiver is either the node using the requested address, or ithas a valid route to the requested address. 5 | 6 | This repository provides a MATLAB simulaiton of VANET enviornment and rsults comparison in terms of throughput, packet drop ratio etc. More information can be reached at https://www.youtube.com/watch?v=2QeSYOgJo9s&t=32s 7 | 8 | [![VANET simulation in MATLAB](https://i9.ytimg.com/vi/2QeSYOgJo9s/mqdefault.jpg?sqp=CJy8sNoF&rs=AOn4CLBdwNz9Q_HfYpB82rEtML5H9CKI6Q&time=1531715269414)](https://www.youtube.com/watch?v=2QeSYOgJo9s&t=32s "Click to play on youtube.com") 9 | 10 | -------------------------------------------------------------------------------- /UrbanCitySimu.m: -------------------------------------------------------------------------------- 1 | function [distance,energy,nw_liftime,throughput]=UrbanCitySimu(NumOfNodes,src_node,dst_node) 2 | 3 | 4 | 5 | citysize=100; 6 | axis([0 citysize+1 0 citysize+1]); 7 | hold on 8 | blksiz=30; 9 | Eini=1;% in joules 10 | % Range=(3*(blksiz/2))/2; 11 | Range=20; 12 | breadth = 0; 13 | display_node_numbers = 1; 14 | % src_node=5; 15 | % src_node=round(1+(NumOfNodes-1).*rand); 16 | src_node1=src_node; 17 | % dst_node=round(1+(NumOfNodes-1).*rand); 18 | % dst_node=35; 19 | %% uicontrol 20 | H = uicontrol('Style', 'listbox', ... 21 | 'Units', 'normalized', ... 22 | 'Position', [0.6 0.2 0.3 0.68], ... 23 | 'String', {'Path Establishing...'}); 24 | drawnow; 25 | % pause(1.0); 26 | %% 27 | %%%%%%%%%%%%creating road network%%%%%%%%% 28 | for len = 0:citysize 29 | if(rem(len,10)~=0) 30 | for breadth = 0:citysize 31 | if(rem(breadth,10)~=0) 32 | h1 = plot(len,breadth,':g'); 33 | end 34 | end 35 | end 36 | breadth = breadth+1; 37 | end 38 | %%%%%%%%%%%%%%%%END1%%%%%%%%%%%%%%%%%%%%%%%%%% 39 | Node = zeros(NumOfNodes,6); % 1:X, 2:Y, 3:updatedX, 4:updatedY, 5:direction 40 | %%%%%%%%%%%%%%%%%%%%%get random nodes%%%%%%%%%%%%%%%%% 41 | for node_index = 1:NumOfNodes 42 | TempX = randi([0,citysize],1,1); 43 | if (rem(TempX,10)==0) 44 | %sprintf('TempX = %d\n',TempX); 45 | Node(node_index,1) = TempX; %X co-ordinate in 1st column 46 | Node(node_index,2) = randi([0,citysize],1,1); %Y co-ordinate in 2nd column 47 | %sprintf('%d IF: X=%d Y=%d',node_index, Node(node_index,1),Node(node_index,2)) 48 | else 49 | Node(node_index,2) = 10*(randi([0,citysize/10],1,1)); %Y co-ordinate in 2nd column 50 | Node(node_index,1) = randi([0,citysize],1,1); %X co-ordinate 51 | %sprintf('%d ELSE: X= %d Y= %d',node_index, Node(node_index,1),Node(node_index,2)) 52 | end 53 | end 54 | %% Assign Positions to RSUs 55 | m=1; 56 | temp=1472014; 57 | for ii=blksiz/2:2*blksiz:citysize 58 | n=1; 59 | for jj=blksiz/2:2*blksiz:citysize 60 | rsu.position{m,n}=[ii,jj]; % RSU's Position 61 | rsu.ID{m,n} = temp;% RSU's ID 62 | plot(ii,jj,'xr','Linewidth',2) 63 | text(ii+1,jj, num2str(rsu.ID{m,n})) 64 | n=n+1; 65 | temp=temp+1; 66 | end 67 | m=m+1; 68 | end 69 | m=round((citysize/(2*blksiz))+1); 70 | for ii=blksiz/2+blksiz:2*blksiz:citysize 71 | n=1; 72 | for jj=blksiz/2+blksiz:2*blksiz:citysize 73 | rsu.position{m,n}=[ii,jj];% RSU's Position 74 | rsu.ID{m,n} = temp;% RSU's ID 75 | plot(ii,jj,'xr','Linewidth',2) 76 | text(ii+1,jj, num2str(rsu.ID{m,n})) 77 | n=n+1; 78 | temp=temp+1; 79 | end 80 | m=m+1; 81 | end 82 | rsu.origID=rsu.ID; 83 | % combine nodes position and RSU positions in a single matrix 84 | temp=reshape(rsu.position,numel(rsu.position),1); 85 | temp=temp(~cellfun(@isempty, temp)); % delete empty cell in the matrix 86 | for ii=1:numel(temp) 87 | temp1(ii,:)=temp{ii}; 88 | end 89 | node_rsu=[Node;repmat(temp1,1,3)]; % combined matrix for rsu and nodes location 90 | clear temp temp1 91 | 92 | %% 93 | %sprintf('Number of Nodes %d',NumOfNodes) 94 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 95 | %labels = cell2str(num2str([1:NumOfNodes]')); 96 | %h1 = ones(NumOfNodes,1); 97 | h2 = ones(NumOfNodes,1); 98 | h4 = ones(1,1); 99 | counter=1; 100 | for n = 0:citysize 101 | for node_index = 1:NumOfNodes 102 | 103 | if(rem(Node(node_index,1),10)~=0) 104 | h2(node_index) = plot(Node(node_index,1)+n*(2*(rem(node_index,2))-1), Node(node_index,2),'.k'); 105 | 106 | node_rsu(node_index,3) = Node(node_index,1)+n*(2*(rem(node_index,2))-1); 107 | node_rsu(node_index,4) = Node(node_index,2); 108 | node_rsu(node_index,5) = rem(node_index,2)+2; 109 | if node_index==src_node1 110 | plot(node_rsu(node_index,3), node_rsu(node_index,4),'og'); 111 | h7=text(node_rsu(node_index,3), node_rsu(node_index,4)+1,num2str(src_node1)); 112 | % hold on 113 | end 114 | if node_index==dst_node 115 | plot(node_rsu(node_index,3), node_rsu(node_index,4),'dm'); 116 | h9=text(node_rsu(node_index,3), node_rsu(node_index,4)+1,num2str(dst_node)); 117 | % hold on 118 | end 119 | else 120 | h2(node_index) = plot(Node(node_index,1),Node(node_index,2)+n*(2*(rem(node_index,2))-1),'.k'); 121 | 122 | node_rsu(node_index,3) = Node(node_index,1); 123 | node_rsu(node_index,4) = Node(node_index,2)+n*(2*(rem(node_index,2))-1); 124 | node_rsu(node_index,5) = rem(node_index,2); 125 | if node_index==src_node1 126 | plot(node_rsu(node_index,3), node_rsu(node_index,4),'og'); 127 | h7=text(node_rsu(node_index,3), node_rsu(node_index,4)+1,num2str(src_node1)); 128 | % hold on 129 | end 130 | if node_index==dst_node 131 | plot(node_rsu(node_index,3), node_rsu(node_index,4),'dm'); 132 | h9=text(node_rsu(node_index,3), node_rsu(node_index,4)+1,num2str(dst_node)); 133 | % hold on 134 | end 135 | end 136 | end 137 | %%%%%%%%%AODV%%%%%%%%%%%%%%%5 138 | % find all nodes which are in range of each other 139 | for p = 1:size(node_rsu,1) 140 | for q = 1:size(node_rsu,1) 141 | dist=sqrt((node_rsu(p,3)-node_rsu(q,3))^2+(node_rsu(p,4)-node_rsu(q,4))^2); 142 | if dist<=Range 143 | inrange(p,q)=1; 144 | else 145 | inrange(p,q)=0; 146 | end 147 | 148 | end 149 | end 150 | src_node=src_node1; % to reset teh src_node to original source node after every iteration of n=1:citysize 151 | rtngtble=src_node;% initialise 152 | tble1=src_node;% initialise 153 | tble=src_node;% initialise 154 | cnt=1;% initialise 155 | cnt1=1;% initialise 156 | dimnsn(cnt)=numel(rtngtble); 157 | while rtngtble~=dst_node 158 | 159 | for ii=1:numel(tble1) 160 | src_node=tble1(ii); 161 | temp=find(inrange(src_node,:)); 162 | temp=temp(find(ismember(temp,tble)==0)); 163 | str{cnt1}=[src_node,temp]; 164 | tble=[tble, temp]; 165 | cnt1=cnt1+1; 166 | end 167 | tble1=tble(find(ismember(tble,rtngtble)==0));% seprate nodes which are not present in routing table 168 | rtngtble=[rtngtble,tble]; 169 | % remove the repeated node in table 170 | [any,index]=unique( rtngtble,'first'); 171 | rtngtble=rtngtble(sort(index)); 172 | 173 | if ismember(dst_node,rtngtble) 174 | dst_cell=find(cellfun(@equal, str,repmat({dst_node},1,length(str)))); % find out whihch structre cell has destination node 175 | dst=dst_cell; 176 | nodtble=dst_node; 177 | frst_node=dst; 178 | while frst_node~=src_node1 179 | frst_node=str{dst(1)}(1); 180 | dst=find(cellfun(@equal, str,repmat({frst_node},1,length(str)))); 181 | nodtble=[nodtble, frst_node]; 182 | end 183 | % msgbox('path found') 184 | nodtble=fliplr(nodtble) % final routing table 185 | %% uicontrol setting 186 | set(H, 'String', cat(1, get(H, 'String'), {['Path ' num2str(nodtble)]})); 187 | drawnow; 188 | pause(0.25); 189 | 190 | % set(H, 'String', cat(1, get(H, 'String'), {'End'})); 191 | % drawnow; 192 | % pause(1.0); 193 | %% 194 | route{counter}=nodtble; % save all AODV paths for each change in vehicle position into a structure 195 | h4= plot(node_rsu(nodtble,3),node_rsu(nodtble,4)); 196 | pause(0.01); 197 | set(h4,'Visible','off'); 198 | [E,pcktlossrate,total_dist,pcktloss,thrgput]=evaluation(nodtble,node_rsu); % parameters calculation 199 | energy(counter,:)=E; % energy consumption 200 | distance(counter)=total_dist;% Total Distance between hops in AODV path 201 | throughput(counter,:)=thrgput; % throughput 202 | counter=counter+1; 203 | end 204 | 205 | cnt=cnt+1; 206 | dimnsn(cnt)=numel(rtngtble); 207 | if numel(rtngtble)==1 208 | msgbox('1-No Node in range, Execute again') 209 | return 210 | end 211 | if cnt>=5 212 | % h8=msgbox('No path found'); 213 | break 214 | end 215 | 216 | end 217 | pause(0.0001); 218 | set(h2,'Visible','off'); 219 | set(h7,'Visible','off'); 220 | set(h9,'Visible','off'); 221 | end 222 | %% plot results 223 | figure(2) 224 | plot(distance,'r','linewidth',2) 225 | xlabel('Number of times path found during simulation of VANET') 226 | ylabel('Dsiatnce in a path for each source and destination vehicles position') 227 | title(['Total Distnace in each linked path with hops=', num2str(cellfun('ndims',route(1)))]) 228 | grid on 229 | 230 | figure(3) 231 | plot(energy,'Linewidth',1.5) 232 | xlabel('Number of times path found during simulation of VANET') 233 | ylabel('Energy in Joules') 234 | title('Energy Consumption') 235 | legend('Data Rate=4 pckts/sec','Data Rate=6 pckts/sec','Data Rate=8 pckts/sec','Data Rate=10 pckts/sec','Data Rate=12 pckts/sec','Data Rate=14 pckts/sec') 236 | grid on 237 | 238 | nw_liftime=Eini./energy; % netwrok life time 239 | figure(4) 240 | plot(nw_liftime,'Linewidth',1.5) 241 | xlabel('Number of times path found during simulation of VANET') 242 | ylabel('Netwrok Life time') 243 | title('Netwrok life time plot for different data rates') 244 | legend('Data Rate=4 pckts/sec','Data Rate=6 pckts/sec','Data Rate=8 pckts/sec','Data Rate=10 pckts/sec','Data Rate=12 pckts/sec','Data Rate=14 pckts/sec') 245 | grid on 246 | 247 | figure(5) 248 | plot(throughput/10e6,'Linewidth',1.5) 249 | xlabel('Number of times path found during simulation of VANET') 250 | ylabel('Throughput in MBps ') 251 | title('Throughput plot for different data rates') 252 | legend('Data Rate=4 pckts/sec','Data Rate=6 pckts/sec','Data Rate=8 pckts/sec','Data Rate=10 pckts/sec','Data Rate=12 pckts/sec','Data Rate=14 pckts/sec') 253 | grid on 254 | end 255 | 256 | 257 | 258 | -------------------------------------------------------------------------------- /anish.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earthat/VANET-Simulation-in-MATLAB/acd1417e17c8a7aed0239151f4fd4e97ca08d445/anish.fig -------------------------------------------------------------------------------- /anish.m: -------------------------------------------------------------------------------- 1 | function varargout = anish(varargin) 2 | % ANISH MATLAB code for anish.fig 3 | % ANISH, by itself, creates a new ANISH or raises the existing 4 | % singleton*. 5 | % 6 | % H = ANISH returns the handle to a new ANISH or the handle to 7 | % the existing singleton*. 8 | % 9 | % ANISH('CALLBACK',hObject,eventData,handles,...) calls the local 10 | % function named CALLBACK in ANISH.M with the given input arguments. 11 | % 12 | % ANISH('Property','Value',...) creates a new ANISH or raises the 13 | % existing singleton*. Starting from the left, property value pairs are 14 | % applied to the GUI before anish_OpeningFcn gets called. An 15 | % unrecognized property name or invalid value makes property application 16 | % stop. All inputs are passed to anish_OpeningFcn via varargin. 17 | % 18 | % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one 19 | % instance to run (singleton)". 20 | % 21 | % See also: GUIDE, GUIDATA, GUIHANDLES 22 | 23 | % Edit the above text to modify the response to help anish 24 | 25 | % Last Modified by GUIDE v2.5 23-Sep-2015 14:01:15 26 | 27 | % Begin initialization code - DO NOT EDIT 28 | gui_Singleton = 1; 29 | gui_State = struct('gui_Name', mfilename, ... 30 | 'gui_Singleton', gui_Singleton, ... 31 | 'gui_OpeningFcn', @anish_OpeningFcn, ... 32 | 'gui_OutputFcn', @anish_OutputFcn, ... 33 | 'gui_LayoutFcn', [] , ... 34 | 'gui_Callback', []); 35 | if nargin && ischar(varargin{1}) 36 | gui_State.gui_Callback = str2func(varargin{1}); 37 | end 38 | 39 | if nargout 40 | [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); 41 | else 42 | gui_mainfcn(gui_State, varargin{:}); 43 | end 44 | % End initialization code - DO NOT EDIT 45 | 46 | 47 | % --- Executes just before anish is made visible. 48 | function anish_OpeningFcn(hObject, eventdata, handles, varargin) 49 | % This function has no output args, see OutputFcn. 50 | % hObject handle to figure 51 | % eventdata reserved - to be defined in a future version of MATLAB 52 | % handles structure with handles and user data (see GUIDATA) 53 | % varargin command line arguments to anish (see VARARGIN) 54 | 55 | % Choose default command line output for anish 56 | handles.output = hObject; 57 | 58 | % Update handles structure 59 | guidata(hObject, handles); 60 | 61 | % UIWAIT makes anish wait for user response (see UIRESUME) 62 | % uiwait(handles.figure1); 63 | 64 | 65 | % --- Outputs from this function are returned to the command line. 66 | function varargout = anish_OutputFcn(hObject, eventdata, handles) 67 | % varargout cell array for returning output args (see VARARGOUT); 68 | % hObject handle to figure 69 | % eventdata reserved - to be defined in a future version of MATLAB 70 | % handles structure with handles and user data (see GUIDATA) 71 | 72 | % Get default command line output from handles structure 73 | varargout{1} = handles.output; 74 | 75 | 76 | % --- Executes on button press in Simulate. 77 | function Simulate_Callback(hObject, eventdata, handles) 78 | % hObject handle to Simulate (see GCBO) 79 | % eventdata reserved - to be defined in a future version of MATLAB 80 | % handles structure with handles and user data (see GUIDATA) 81 | NumOfNodes=str2double(get(handles.edit1,'string')); 82 | src_node=str2double(get(handles.edit2,'string')); 83 | if isnan(src_node) 84 | src_node=round(1+(NumOfNodes-1).*rand); 85 | end 86 | if src_node>NumOfNodes 87 | errordlg('Source ID is more than Number of nodes','Index Exceeds') 88 | return 89 | end 90 | 91 | dst_node=str2double(get(handles.edit3,'string')); 92 | if isnan(dst_node) 93 | dst_node=round(1+(NumOfNodes-1).*rand); 94 | end 95 | if dst_node>NumOfNodes 96 | errordlg('Destination ID is more than Number of nodes','Index Exceeds') 97 | return 98 | end 99 | [distance,energy,nw_liftime,throughput]=UrbanCitySimu(NumOfNodes,src_node,dst_node); 100 | if exist('out.xls','file') 101 | delete out.xls 102 | end 103 | xlswrite('out',distance','Distance') 104 | xlswrite('out',energy,'Energy') 105 | xlswrite('out',nw_liftime,'Network Life Time') 106 | xlswrite('out',throughput,'throughput') 107 | winopen('out.xls') 108 | 109 | 110 | 111 | 112 | % --- Executes on selection change in listbox3. 113 | function listbox3_Callback(hObject, eventdata, handles) 114 | % hObject handle to listbox3 (see GCBO) 115 | % eventdata reserved - to be defined in a future version of MATLAB 116 | % handles structure with handles and user data (see GUIDATA) 117 | 118 | % Hints: contents = cellstr(get(hObject,'String')) returns listbox3 contents as cell array 119 | % contents{get(hObject,'Value')} returns selected item from listbox3 120 | 121 | 122 | % --- Executes during object creation, after setting all properties. 123 | function listbox3_CreateFcn(hObject, eventdata, handles) 124 | % hObject handle to listbox3 (see GCBO) 125 | % eventdata reserved - to be defined in a future version of MATLAB 126 | % handles empty - handles not created until after all CreateFcns called 127 | 128 | % Hint: listbox controls usually have a white background on Windows. 129 | % See ISPC and COMPUTER. 130 | if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) 131 | set(hObject,'BackgroundColor','white'); 132 | end 133 | 134 | 135 | % --- Executes on button press in Clear. 136 | function Clear_Callback(hObject, eventdata, handles) 137 | % hObject handle to Clear (see GCBO) 138 | % eventdata reserved - to be defined in a future version of MATLAB 139 | % handles structure with handles and user data (see GUIDATA) 140 | % h=handles.axis1; 141 | cla 142 | 143 | 144 | 145 | function edit1_Callback(hObject, eventdata, handles) 146 | % hObject handle to edit1 (see GCBO) 147 | % eventdata reserved - to be defined in a future version of MATLAB 148 | % handles structure with handles and user data (see GUIDATA) 149 | 150 | % Hints: get(hObject,'String') returns contents of edit1 as text 151 | % str2double(get(hObject,'String')) returns contents of edit1 as a double 152 | 153 | 154 | % --- Executes during object creation, after setting all properties. 155 | function edit1_CreateFcn(hObject, eventdata, handles) 156 | % hObject handle to edit1 (see GCBO) 157 | % eventdata reserved - to be defined in a future version of MATLAB 158 | % handles empty - handles not created until after all CreateFcns called 159 | 160 | % Hint: edit controls usually have a white background on Windows. 161 | % See ISPC and COMPUTER. 162 | if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) 163 | set(hObject,'BackgroundColor','white'); 164 | end 165 | 166 | 167 | 168 | function edit2_Callback(hObject, eventdata, handles) 169 | % hObject handle to edit2 (see GCBO) 170 | % eventdata reserved - to be defined in a future version of MATLAB 171 | % handles structure with handles and user data (see GUIDATA) 172 | 173 | % Hints: get(hObject,'String') returns contents of edit2 as text 174 | % str2double(get(hObject,'String')) returns contents of edit2 as a double 175 | 176 | 177 | % --- Executes during object creation, after setting all properties. 178 | function edit2_CreateFcn(hObject, eventdata, handles) 179 | % hObject handle to edit2 (see GCBO) 180 | % eventdata reserved - to be defined in a future version of MATLAB 181 | % handles empty - handles not created until after all CreateFcns called 182 | 183 | % Hint: edit controls usually have a white background on Windows. 184 | % See ISPC and COMPUTER. 185 | if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) 186 | set(hObject,'BackgroundColor','white'); 187 | end 188 | 189 | 190 | 191 | function edit3_Callback(hObject, eventdata, handles) 192 | % hObject handle to edit3 (see GCBO) 193 | % eventdata reserved - to be defined in a future version of MATLAB 194 | % handles structure with handles and user data (see GUIDATA) 195 | 196 | % Hints: get(hObject,'String') returns contents of edit3 as text 197 | % str2double(get(hObject,'String')) returns contents of edit3 as a double 198 | 199 | 200 | % --- Executes during object creation, after setting all properties. 201 | function edit3_CreateFcn(hObject, eventdata, handles) 202 | % hObject handle to edit3 (see GCBO) 203 | % eventdata reserved - to be defined in a future version of MATLAB 204 | % handles empty - handles not created until after all CreateFcns called 205 | 206 | % Hint: edit controls usually have a white background on Windows. 207 | % See ISPC and COMPUTER. 208 | if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) 209 | set(hObject,'BackgroundColor','white'); 210 | end 211 | 212 | 213 | % --- Executes on button press in pushbutton3. 214 | function pushbutton3_Callback(hObject, eventdata, handles) 215 | % hObject handle to pushbutton3 (see GCBO) 216 | % eventdata reserved - to be defined in a future version of MATLAB 217 | % handles structure with handles and user data (see GUIDATA) 218 | close all -------------------------------------------------------------------------------- /equal.m: -------------------------------------------------------------------------------- 1 | function log=equal(inpu,dst) 2 | % dst=35; 3 | if ismember(dst,inpu) 4 | log=1; 5 | else 6 | log=0; 7 | end 8 | end -------------------------------------------------------------------------------- /evaluation.m: -------------------------------------------------------------------------------- 1 | function [E,pcktlossrate,total_dist,pcktloss,thrgput]=evaluation(nodtble,node_rsu) 2 | % take out the distance of nodes in routing table from each other 3 | for ii=1:numel(nodtble)-1 4 | distnc(ii)=sqrt((node_rsu(nodtble(ii+1),3)-node_rsu(nodtble(ii),3))^2+(node_rsu(nodtble(ii+1),4)-node_rsu(nodtble(ii),4))^2); 5 | end 6 | total_dist=sum(distnc); % total distnace from source to destination 7 | time_consumed=total_dist/(3*10e9); 8 | %% Perfromance Evolution 9 | pktsize=64;% in bytes 10 | datarate=[4,6,8,10,12,14]; % packets/sec 11 | Etx=1;% in joules 12 | Eini=Etx; 13 | Elec=50e-9; %amount of Energy consumption per bit in the transmitter or receiver circuitry 14 | Emp=0.0015e-12;%Amount of energy consumption for multipath fading 15 | EDA=5e-9; %Data aggregation energy. 16 | % paraemetrs for energy calculation using raio model of message 17 | % transmission 18 | 19 | alpha1=50e-9; %J/bit 20 | alpha2=0.1e-9; %J/bit/m2 21 | alpha=2; 22 | Ebit=0.3e-3; % energy assigned to each bit 23 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 24 | %radio Model for energy consumption is 25 | % E=alpha1+alpha2*(dist)^alpha 26 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 27 | hop=numel(nodtble); 28 | for ff=1:length(datarate) 29 | E(ff)=(alpha1*datarate(ff)*pktsize*8)+(alpha2*datarate(ff)*pktsize*8)*(total_dist)^alpha;% energy loss calculation in transmitting packets at datarate 30 | Edata(ff)=Ebit*datarate(ff)*pktsize*8; 31 | for ll=1:datarate(ff) 32 | Etx=Etx-(Elec*8*pktsize+Emp*8*pktsize); 33 | Erx=Eini-Etx; 34 | Erx=Erx-(Elec+EDA)*8*pktsize; 35 | Eini=Etx; 36 | if Etx<0.98 37 | pcktloss(ll)=1; 38 | else 39 | pcktloss(ll)=0; 40 | end 41 | 42 | end 43 | if hop>4 && datarate(ff)> 6 44 | pcktlossrate(1,ff)=(datarate(ff)-7)/datarate(ff); 45 | else 46 | pcktlossrate(1,ff)=0; 47 | end 48 | thrgput(1,ff)= (datarate(ff)*pktsize)/time_consumed; 49 | end -------------------------------------------------------------------------------- /out.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earthat/VANET-Simulation-in-MATLAB/acd1417e17c8a7aed0239151f4fd4e97ca08d445/out.xls --------------------------------------------------------------------------------