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?
Show older comments
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:
- User inputs greenhouse width and length in feet
- User inputs the number of sprinklers
- User inputs x,y locations of the sprinklers in feet
- 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.
- Using a lookup table, calculate the amount of water that each sprinkler will contribute to each small segment of the greenhouse.
- Add all of the contributions from all of the sprinklers to each small segment.
- 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
Image Analyst
on 20 Nov 2015
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?
Jeff Chapin
on 20 Nov 2015
Jeff Chapin
on 21 Nov 2015
Walter Roberson
on 21 Nov 2015
What pop-up window would that be? ginput does not pop up a window. If you are referring to the cursor, then see http://www.mathworks.com/matlabcentral/answers/156143-change-mouse-cursor-appearance-in-ginput
Jeff Chapin
on 21 Nov 2015
Walter Roberson
on 21 Nov 2015
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);
Jeff Chapin
on 22 Nov 2015
Walter Roberson
on 22 Nov 2015
round(x*2)/2 gets you x to the nearest 0.5 .
Once you have the coordinates see pdist()
Answers (1)
Image Analyst
on 21 Nov 2015
0 votes
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
Image Analyst
on 22 Nov 2015
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
Jeff Chapin
on 22 Nov 2015
Image Analyst
on 22 Nov 2015
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)
Jeff Chapin
on 1 Dec 2015
Walter Roberson
on 1 Dec 2015
"Once you have the coordinates see pdist()"
Image Analyst
on 1 Dec 2015
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?)
Walter Roberson
on 1 Dec 2015
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));
Categories
Find more on Agriculture in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!