一、仿真系统架构
1. 网络模型参数
matlab
% 基础参数设置(参考)
xm = 100; ym = 100; % 区域尺寸(m)
n = 100; % 节点数量
p = 0.1; % 簇头比例
Eo = 0.5; % 初始能量(J)
ETX = 50e-9; % 发射能耗(J/bit)
ERX = 50e-9; % 接收能耗(J/bit)
Efs = 10e-12; % 自由空间路径损耗
Emp = 0.0013e-12; % 多径衰落路径损耗
EDA = 5e-9; % 数据聚合能耗
rmax = 5000; % 最大轮次
2. 节点部署
matlab
% 节点位置初始化(异构能量分布)
nodes = struct('x',[],'y',[],'E',[],'type',[]);
for i = 1:n
nodes(i).x = rand()*xm;
nodes(i).y = rand()*ym;
nodes(i).E = Eo*(1 + 0.1*rand()); % 异构能量
nodes(i).type = 'N';
end
nodes(n+1).x = xm/2; nodes(n+1).y = ym/2; % 汇聚节点
二、核心算法实现
1. LEACH协议仿真
(1) 簇头选举
matlab
function [countCH, energy] = leach_protocol(nodes, r, p)
countCH = 0; energy = 0;
cluster = 1; n = length(nodes)-1;
for i = 1:n
if nodes(i).E > 0
temp_rand = rand();
T = p / (1 - p*mod(r, round(1/p))); % 阈值计算
if temp_rand <= T
countCH = countCH + 1;
nodes(i).type = 'C';
nodes(i).G = round(1/p) - 1;
% 能量消耗计算
d = sqrt((nodes(i).x - nodes(n+1).x)^2 + (nodes(i).y - nodes(n+1).y)^2);
if d > sqrt(Efs/Emp)
energy_cost = (ETX + EDA)*4000 + Emp*d^4;
else
energy_cost = (ETX + EDA)*4000 + Efs*d^2;
end
nodes(i).E = nodes(i).E - energy_cost;
end
end
end
end
(2) 数据传输与能量更新
matlab
% 簇成员加入与数据传输
for i = 1:n
if nodes(i).type == 'N' && nodes(i).E > 0
min_dist = inf;
for c = 1:countCH
d = sqrt((nodes(i).x - nodes(c).x)^2 + (nodes(i).y - nodes(c).y)^2);
if d < min_dist
min_dist = d;
cluster = c;
end
end
% 传输能耗计算
if min_dist > sqrt(Efs/Emp)
nodes(i).E = nodes(i).E - (ETX + Emp)*4000;
else
nodes(i).E = nodes(i).E - (ETX + Efs)*4000;
end
end
end
2. 改进算法实现
(1) LEACH-C(能量感知簇头选举)
matlab
function p = leach_c_election(nodes, r)
avg_energy = mean([nodes.E]);
p = 0.1 * (nodes.E / avg_energy);
p(p > 0.2) = 0.2; % 概率上限限制
end
(2) 虚拟力覆盖优化
matlab
% 虚拟力驱动节点移动(参考)
function nodes = virtual_force_optimization(nodes, step)
for i = 1:length(nodes)-1
if nodes(i).E > 0
F_rep = 0; F_att = 0;
for j = 1:length(nodes)-1
if i ~= j
d = sqrt((nodes(i).x - nodes(j).x)^2 + (nodes(i).y - nodes(j).y)^2);
if d < 30 % 斥力半径
F_rep = F_rep + (d-30)*exp(-d/10);
elseif d > 50 % 引力半径
F_att = F_att + (50-d)*exp(-(50-d)/20);
end
end
end
nodes(i).x = nodes(i).x + step*(F_rep + F_att)/1000;
nodes(i).y = nodes(i).y + step*(F_rep + F_att)/1000;
end
end
end
三、性能评估模块
1. 能量消耗监控
matlab
STATISTICS = struct('DEAD',[],'ALIVE',[],'ENERGY_CONSUMED',[]);
for r = 1:rmax
[countCH, energy] = leach_protocol(nodes, r, p);
dead = sum([nodes.E] <= 0);
STATISTICS.DEAD(r) = dead;
STATISTICS.ALIVE(r) = n - dead;
STATISTICS.ENERGY_CONSUMED(r) = sum([nodes.E]);
end
2. 可视化展示
matlab
% 网络拓扑与存活状态
figure;
plot(nodes(1:n).x, nodes(1:n).y, 'bo'); hold on;
plot(nodes(n+1).x, nodes(n+1).y, 'rx', 'MarkerSize', 10);
title('WSN网络拓扑结构');
xlabel('X坐标(m)'); ylabel('Y坐标(m)');
legend('普通节点', '汇聚节点');
% 存活曲线
figure;
plot(1:rmax, STATISTICS.DEAD, 'r', 1:rmax, STATISTICS.ALIVE, 'g');
title('节点存活状态变化');
xlabel('轮次'); ylabel('节点数量');
legend('死亡节点', '存活节点');
参考代码 MATLAB仿真无线传感器网络算法 www.youwenfan.com/contentcsi/64397.html
四、扩展应用场景
- 动态拓扑调整:结合节点移动模型模拟移动自组织网络(MANET)
- 多跳路由优化:实现基于能量感知的多跳转发策略
- 环境监测仿真:集成温度/湿度传感器数据采集模块