Supplying Water to the Community [MATLAB Code]
I'd definetly appreciate it if you could look through this and attempt it in any way possible. I have tried, but failed to develop the MATLAB code. I hope the information I have provided is sufficient to get you started.
Task:Supply water to neighbouring homes, from the centrak water supply unit (tank).
What we want (what the code should run):
- Select at a random siteWITHOUTwater supply
- Connected that selected site to the closest siteWITHwater supply
- Repeat from Step 1 untill all homes (sites) are successfully supplied with water.
Note: So really, the central water supply unit (tank) can be treated as a site/home that has been supplied with water.
Use the below function:
function [begin_x, finish_x, begin_y, finish_y, total_L] = village_water_supply(supplied_with_water_x, supplied_with_water_y, sites_x, sites_y)
% This function takes the list of sites with x & y coordinates (sites_x & sites_y) and designs a network connecting all sites (potentially indirectly) to site with water supply located by supplied_with_water_x and supplied_with_water_y. total_L is the total length of pipe required to supply water to all houses in the village.
% At the end of your calculations, use the function below to plot your results.
plotWaterSupplyNetwork(supplied_with_water_x, supplied_with_water_y, begin_x, finish_x, begin_y, finish_y);
-----------------------
The functions below are the functions that will help you develop the above function. We haverandom_selection_of_values.m (to select a random site from the listed sites),calculate_the_distance.m(to calculate the distance between selected sites) andclosest_chosen_site.m(this selects the closest site using thecalculate_the_distancefunction).
1. calculate_the_distance.m
% Formula for calculating the distance between any two points (the length of pipe that will run from one home to the other)
function L = calculate_the_distance (x_c, y_c, x, y)
L = sqrt((x_c - x).^2 + (y_c - y).^2)
end
Just an Exmaple of this Function in action
given location (2, -1) -this is x_c and y_c
x = [0, 2, 3]
y = [3, 2, 4]
L = calculate_distances (2, -1, x, y) L = 4.4721 3.0000 5.0990
2. closest_chosen_site.m
function [selected_site, L] = closest_chosen_site(present_site, list_of_sites, x, y)
x1 = x (list_of_sites);
y1 = y (list_of_sites);
D = calulate_the_distance (x(present_site), y(present_site), x1, y1);
[L, indx] = min(D);
closest_chosen_site = list_of_sites(indx);
end
Just an Example of this function in action:
we have a list of four sites given to us:
x = [2, 4, 1, 5]
y = [3, 6, 5, 8]
present_site = 4
list_of_sites = [1, 2, 3](Note: this essentially means, find me which of the 1st, 2nd, and 3rd site is closest to the present_site (4) where you use information about their positions stored in x and y)
L= distance
Example of use:
x = [2, 4, 1, 5]
y = [3, 6, 5, 8]
present_site = 4
list_of_sites = [1, 2, 3]
[selected_site, L] = closest_chosen_site(4, [1 2 3], x, y)
ans:
selected_site
= 1
L
= 1.732
3. random_selection_of_values.m
% This function allows you to select a random value from a list of values.
function selected_value = random_selection_of_values(value_choices)
selecting_a_value = randi(length(value_choices), 1);
selecting_a_value = value_choices(selecting_a_value);
end