天天看點

無線自組網AODV路由機制仿真源碼

%主程式
  clear;
  
  nodes_number  = 50; 
  A = 100;  
  R = 10; 
  
  rand('state', 0);
  X = rand(1,nodes_number)*A/2;  
  Y = rand(1,nodes_number)*A/2; 
  
 fprintf('此網絡有 %d 個節點。\n',nodes_number);
 fprintf('\n');     figure(1); 
  clf;
  hold on; title('無線自組網AODV路由機制仿真');
 xlabel('空間橫坐标 x  機關:m');
 ylabel('空間縱坐标 y  機關:m');for i = 1:nodes_number
      plot(X(i), Y(i), '.'); 
      text(X(i), Y(i), num2str(i));
      for j = 1:nodes_number
          distance = sqrt((X(i) - X(j))^2 + (Y(i) - Y(j))^2); 
          if distance <= R
              nodes_link(i, j) = 1;
              %line([X(i) X(j)], [Y(i) Y(j)], 'LineStyle', '-.'); 
              grid on;
          else
              nodes_link(i, j) = inf;
          end;
      end;
  end;
      
 s = input('請輸入源節點号:');
 d = input('請輸入目的節點号:');
 fprintf('\n');if (s<=nodes_number&s>=1)&(d<=nodes_number&d>=1)
     
      [path, hop] = path_discovery(nodes_number, nodes_link, s, d);      l=length(path);
       if l==0&s~=d 
            fprintf('源節點 %d 到目的節點 %d 的路徑為:空!\n',s,d);
            fprintf('\n');
            plot(X(s), Y(s), 'rp','markersize',15); 
            plot(X(d), Y(d), 'rp','markersize',15);
        elseif l==0&s==d
            fprintf('源節點 %d 與目的節點 %d 為同一節點。\n',s,d);
            fprintf('跳數為 %d 。\n',hop);
            fprintf('\n')
            plot(X(d), Y(d), 'rp','markersize',15);
        else fprintf('源節點 %d 到目的節點 %d 的路徑為:',s,d);
            i=2;
            fprintf('%d', s);
            while i~=l+1
                fprintf(' -> %d', path(i));
                i=i+1;
            end;
            fprintf('\n');
            fprintf('跳數為 %d 。\n',hop);
            fprintf('\n');
        end;     if l ~= 0
          for i = 1:(l-1)
              line([X(path(i)) X(path(i+1))], [Y(path(i)) Y(path(i+1))], 'Color','r','LineWidth', 1.50);
          end;
      end;
      
 hold off;
  
 else fprintf('輸入節點有誤,請重新運作!\n');
     fprintf('\n'); 
     
 end;      

繼續閱讀