How can i create a program that allows the user to enter in xy coordinates for the location of sprinklers, then calculates the water accumulation taking into account overlapping sprinklers and plots the water distribution?

I am trying to create a program that a user can input sprinkler locations (x,y) and greenhouse dimensions and it will calculate the water coverage over the entire greenhouse. I understand logically how to do it but am having trouble programming it into MATLAB.
Steps:
  1. User inputs greenhouse width and length in feet
  2. User inputs the number of sprinklers
  3. User inputs x,y locations of the sprinklers in feet
  4. Assuming "x" is along the length and "y" is along the width, divide the greenhouse into 0.5 ft by 0.5 ft areas and calculate the distance of each sprinkler to each small segment of the greenhouse.
  5. Using a lookup table, calculate the amount of water that each sprinkler will contribute to each small segment of the greenhouse.
  6. Add all of the contributions from all of the sprinklers to each small segment.
  7. Plot out a 3D contour that shows the floor of the greenhouse and how much water fell in each area.
I figured out how to do steps 1 through 3 but am stuck on step 4. Any help you could give would be much appreciated. Thank you

8 Comments

For number 3, first use xlim([0,xLength]) and ylim([0, yWidth]) to create a plot, then use ginput(numberOfSprinklers) to have the user specify the locations.
For #4, do they mean to the center of the square, or to the closest edge?
Thank you for your help. For #4 I would use the corners. If you had a 30 foot by 50 foot greenhouse, picture "x" along the length and "y" along the width. then have a grid of points starting at 0,0 every 0.5 feet so 0,0; 0.5,0; 1,0...to 100,30. Then calculate the water contribution at each point from every sprinkler.
When using ginput can I make the pop-up window be to scale with the xLength and yWidth?
It is not the cursor that I am trying to change. When I use the ginput command a window opens so I can select points with my cursor. The scales of the x and y axes are not the same. If my x-axis goes from 0 to 100 and my y-axis goes 0 to 50 then the pop-up window should be 2x as wide as it is tall. However this is not the case.
ginput() needs a figure to work on. It does not specifically open a new figure, it just asks for the current figure, and asking MATLAB for the current figure has the side effect of creating a new figure if there is no existing figure. So if you already have a figure it will use that.
Therefore, create a figure with points that you want. For example,
newax = axes('DataAspect', [1 1 1], 'Xlim', [0 100], 'YLim', [0 50])
sprinklersAt = ginput(NumberOfSprinklers);
thank you...that worked for the axes and figure. Any ideas on how to do #4 above. Once I have the coordinates of the sprinkler locations then I need to divide the green house into a grid of points and calculate the distance from each of the points to each of the sprinklers.
round(x*2)/2 gets you x to the nearest 0.5 .
Once you have the coordinates see pdist()

Sign in to comment.

Answers (1)

You'd need to create the axes in advance of using ginput(). Use xlim() and ylim() to set up the axes limits that you want.

7 Comments

For #4, I disagree with you. I think it would be way too complicated to determine the distance from the sprinkler to the edges or corners of the .5x.5 ft zones. I don't see anything that says that so I'd assume it would be to the center. That would be easy. You just use Pythagorean theorem. So if you have an N by 2 array or sprinkler locations, and an M by 2 array of square centers, you can use pdist() if you have the Statistics and Machine Learning Toolbox. See attached demo.
If you don't have that toolbox, you can do the brute force method with some nested for loops. The outer one over sprinklers and the inner one over grid zones. It's pretty trivial, but let me know if you can't figure it out. Here's a start.
distances = zeros(......
for s = 1 : numSprinklers
thisX = ...
thisY = ....
distances(s,:) = sqrt((z - thisX)^2+(z-thisY).^2);
end
I think I understand the outer for loop for the sprinklers but I am not sure how to do the inner for loop to create the grid zone. I allow the user to say what size greenhouse it is so the width and length of the greenhouse varies.
Perhaps I do a for loop for the length starting at 0, increasing by 0.5 and going to "x". Then have another for loop inside it for the width starting at 0, increasing by 0.5 and going to "y".
For example, a greenhouse with length of 20 and width of 5, I would star at (0,0; 0.5,0; 1,0; 1.5,0 all the way to 20,0). How do I get it to go to the next row for (0,0.5; 0.5,0.5; 1,0.5; 1.5,0.5 all the way to 20,0.5)? Thank you for your help.
Jeff, use meshgrid to create an array of all (x,y) pairs. Look it up - it's a handy function that all/most experienced MATLAB programmers know and use on occasion:
xSpacing = 0.5 : 0.5 : 19.5;
ySpacing = 0.5 : 0.5 : 19.5;
[x, y] = meshgrid(xSpacing, ySpacing)
thank you...I got the grid to work now. However I am having some difficulty getting the distance to calculate for each sprinkler. Can you fill in some more information in the "for" loop above please? I currently have a variable called "SprinklerLocation" which has the x,y coordinates of all of the sprinklers and an array for the grid [x, y].
If you ahve the Statistics and Machine Learning Toolbox, pdist2() might be easier. I think it's
distances = pdist2(xy1, xy2);
where xy1 is an M by 2 list of (x,y) coordinates for one set and xy2 is for the other set, but I think you have just one set so you'd use
distance = pdist2(xy, xy);
See if that works. If it does not then you'll just have to use the intuitive, brute force method of the for loops (can you really not figure out how to do that?)
With SprinklerLocation a 2 column matrix, and X and Y being arrays of coordinates for the grid centres,
distances = sqrt(sum(bsxfun(@minus, [X(:), Y(:)], SprinklerLocation),2));

Sign in to comment.

Categories

Find more on Agriculture in Help Center and File Exchange

Asked:

on 20 Nov 2015

Commented:

on 1 Dec 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!