% LEACH Protocol for Wireless Sensor Networks % Parameters numNodes = 100; % Number of nodes in the network rounds = 50; % Number of rounds threshold = 0.05; % Threshold for selecting cluster heads initialEnergy = 1; % Initial energy level of each node % Randomly deploy nodes in a 2D field x = rand(1, numNodes) * 100; % X-coordinate y = rand(1, numNodes) * 100; % Y-coordinate % Initialize energy levels for each node energy = initialEnergy * ones(1, numNodes); for round = 1:rounds % Election of cluster heads if rand < threshold clusterHeads = randperm(numNodes, round); % Compute distances from cluster heads to all nodes distances = sqrt((x - x(clusterHeads)).^2 + (y - y(clusterHeads)).^2); % Nodes join the cluster of the nearest cluster head [~, clusterIdx] = min(distances); % Update energy levels (for simplicity, we assume equal energy consumption) energy(clusterHeads) = energy(clusterHeads) - 0.1; energy = energy - 0.1; end % Data transmission and processing % (Assume some data transmission and processing happens here) % Update energy levels (for simplicity, we assume equal energy consumption) energy = energy - 0.1; % Check for dead nodes and remove them deadNodes = find(energy <= 0); numNodes = numNodes - length(deadNodes); x(deadNodes) = []; y(deadNodes) = []; energy(deadNodes) = []; end % Display the final state of the network figure; scatter(x, y, 50, 'filled'); hold on; scatter(x(clusterHeads), y(clusterHeads), 50, 'r', 'filled'); % Cluster heads in red title('LEACH Protocol for Wireless Sensor Networks'); xlabel('X-coordinate'); ylabel('Y-coordinate'); legend('Normal Nodes', 'Cluster Heads'); hold off; % Parameters num_nodes = 100; % Number of sensor nodes network_lifetime = 0; % Initialize network lifetime % Energy consumption in different states (in Joules) energy_sensing = 0.1; % Example value, replace with actual energy consumption energy_transmitting = 0.5; % Example value, replace with actual energy consumption energy_receiving = 0.3; % Example value, replace with actual energy consumption energy_sleeping = 0.01; % Example value, replace with actual energy consumption % Duty cycle for each state (proportion of time spent in each state) duty_cycle_sensing = 0.2; % Example value, replace with actual duty cycle duty_cycle_transmitting = 0.3; % Example value, replace with actual duty cycle duty_cycle_receiving = 0.1; % Example value, replace with actual duty cycle duty_cycle_sleeping = 0.4; % Example value, replace with actual duty cycle % Simulation time (in seconds) simulation_time = 24 * 3600; % Example: 24 hours % Calculate energy consumption during the simulation total_energy_consumption = num_nodes * (... duty_cycle_sensing * energy_sensing + ... duty_cycle_transmitting * energy_transmitting + ... duty_cycle_receiving * energy_receiving + ... duty_cycle_sleeping * energy_sleeping... ); % Calculate network lifetime in days network_lifetime_days = simulation_time / (total_energy_consumption / num_nodes); fprintf('Estimated network lifetime: %.2f days\n', network_lifetime_days); % Parameters num_nodes = 100; % Number of sensor nodes data_packet_size = 100; % Data packet size in bits transmission_range = 50; % Transmission range in meters simulation_time = 1000; % Simulation time in seconds % Energy consumption in different states (in Joules) energy_transmitting = 0.5; % Example value, replace with actual energy consumption energy_receiving = 0.3; % Example value, replace with actual energy consumption % Cluster formation parameters p = 0.1; % Percentage of nodes to become cluster heads % Simulation total_data_transmitted = 0; for time = 1:simulation_time % LEACH cluster formation (simplified for demonstration) if mod(time, round(1/p)) == 0 % Nodes advertise themselves as potential cluster heads % Cluster heads are selected based on some criteria (not included in this example) % Clusters are formed, and cluster heads are responsible for data aggregation end % Data transmission within clusters (simplified for demonstration) % Assuming each node sends a data packet to its cluster head data_transmitted_per_round = num_nodes * data_packet_size; % Calculate energy consumption during data transmission energy_consumed_transmitting = num_nodes * energy_transmitting; % Calculate energy consumption during data reception at cluster heads energy_consumed_receiving = energy_receiving; % Update total data transmitted total_data_transmitted = total_data_transmitted + data_transmitted_per_round; end % Calculate throughput (bits per second) throughput = total_data_transmitted / simulation_time; fprintf('LEACH Throughput: %.2f bps\n', throughput);