Homework Help: Root Plotting 2d-Array in Histogram

1. Mar 10, 2015

Rapier

1. The problem statement, all variables and given/known data
The task is to plot a 2-d surface of the potential and field lines calculated from a numerical method. In this case, there is a charged box (v = 1) @ r = 1 (it's not round, but each side is d = 2 and the center of the box is at the origin) and the edges of the box are v=0. My plan was to use symmetry so that I only have to do the calculations for Quadrant I and then I could do some trickery to set those values for the other quadrants.

2. Relevant equations
N/A

3. The attempt at a solution
My initial conditions work correctly and I get (exactly what I expected):

--------Initial Conditions--------
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
1.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
1.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00

At the end of my numerical method I have the correct field values in my 2-d array.

--------Final Conditions--------
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
0.02 0.02 0.02 0.02 0.01 0.01 0.01 0.00 0.00 0.00
0.04 0.04 0.04 0.03 0.03 0.02 0.01 0.01 0.00 0.00
0.08 0.08 0.07 0.06 0.04 0.03 0.02 0.01 0.01 0.00
0.14 0.14 0.12 0.09 0.07 0.04 0.03 0.02 0.01 0.00
0.24 0.23 0.19 0.14 0.09 0.06 0.04 0.02 0.01 0.00
0.39 0.37 0.28 0.20 0.13 0.08 0.05 0.03 0.01 0.00
0.64 0.60 0.41 0.26 0.16 0.09 0.05 0.03 0.01 0.00
1.00 1.00 0.52 0.29 0.17 0.10 0.05 0.03 0.01 0.00
1.00 1.00 0.58 0.33 0.18 0.10 0.05 0.03 0.01 0.00

NOW my problem is that I need to plot these guys in a 2D space. My instructor has all but forced us to use a TH2D histogram, but mostly because it's the only root structure she knows how to use. I don't really need a histogram. I'm not going to be doing any calculations off the graph. I just need to graph those points on a surface.

The second portion of the project (which I will start once I get the code working well) is to create a field line arrow at each (x,y) in the axis and have the arrow be equal to the size of the field. Of course, we also can't let ROOT do the work so I have to do the calculations myself and force the issue (so if you want to chime in on that, please feel free).

This is what I am trying to do now (maxSize is the number of elements, in my case 10, and potential is the 2d array where the two indices are the x and y coords of the point):

for (int y = 0; y < (2 * maxSize - 1); y++)
.....for (int x = 0; x < (2 * maxSize - 1); x++)
..........potent->SetBinContent(x + 1, y + 1, potential[abs((maxSize - 1) - x)][abs((maxSize - 1) - y)]);

What I'm getting is not what I want. I've attached an image ( ) for reference. What I am looking for is something like ( ). I am perfectly willing to use something other than a histogram (TGraph2D, perhaps).

My instructor is not as helpful as I would like and I am forced to be teaching myself root. I can give any other info/code you need and I could REALLY use some help. This seems like it should be so easy, and it is turning out to be anything but.

Oh, and I will be the first to admit that I really have a very poor understanding (if at all) of what a bin is and am fairly certain it isn't quite what I think it is.

Thanks.

Last edited: Mar 10, 2015
2. Mar 11, 2015

BvU

A few comments:
The difference between what you have and what you're looking for is mirroring to the other quadrants: $\phi(-x,y) = \phi(x,y)$ and $\phi(x,-y) = \phi(x,y)$. But I suppose you know that and you know how to program that ...

There is another symmetry you haven't used \phi(y,x) = \phi(x,y)$, and your final values dont show this symmetry -- why not ? The understanding of "what a bin is" is important when making histograms. A bin is just a representation of a range (1,2 or 3D). And the proper axis title is of the kind (number)/(bin width), e.g. (number of babys with birth length l at birth) / (5 cm l range) For a surface plot of a function a bin stands for the center point of that range. The axis title can be just l for a plot of birth length probability density. Example: a Gaussian distribution like$e^{-x^2}$(properly scaled and normalized) is a function, a probability density distribution. Function values can be plotted for arbitrarily many points/bins. But when you repeat an experiment a number of times, you make a histogram of the number of outcomes that fall in each bin. And the probability for these numbers is$\int_a^b P(x) dx## where a and b are the limits of the range the bin represents. Obivously you can't make the bins too small if you want your histogram to look reasonable.
I'm grateful for your mentioning Root and Tgraph2d. Looks impressive! I'm from the HBook era so I can't help you with those.

3. Mar 11, 2015

Rapier

I believe that the lack of (y,x) = (x,y) symmetry is due to the numerical method we are using. We are taking the average of the surrounding points and while the text and the instructor haven't said as much, I'm assuming that this is the reason. I know that my results (accepting that my graphs are only for Quadrant 1) are exactly what is expected.

I imagine that our methodology isn't quite perfect.

That does kind of help explain bins. It is further evidence that our instructor is completely clueless (as I expected) that she is forcing us into a histogram model when we aren't doing anything that requires histogram functionality. I was initially trying to use a TGraph2D and I wasn't having much luck so I decided to give her method a go. I've spent over 6 hours now trying to get the histogram to plot the surface. I hate to throw away that effort, but I might concentrate on trying to use TGraph2D.

ROOT is actually pretty useful when you get used to it. It did take a while for me to get into the ROOT mindset, but now that I'm there I'm finding things pretty easy. It also helps that ROOT is free. Mathematica, Maple and all the rest are pretty expensive and knowledge of those wouldn't do me a bit of good once I leave the lab.

4. Mar 11, 2015

Rapier

I have some good news to report. My backup plan was to take my data for quadrant 1 and create another array and propagate the data to the other quadrants. That worked. There was still a bug or two that needed worked out and I had to tinker with my bounds to get it exactly right (I initially had the center of the field shifted to the left by .1. I'm now working on the 'draw the field line arrows' section of the assignment.

I am getting a lot of schmutz in my histogram, so like the previous problem I've dumped the whole histogram approach and am working on a TGraph. I need a break so I'm headed towards bed and I'll tackle it again fresh in the morning. And yes, I know the arrows aren't even remotely in the right place but at least I am now actually seeing arrows so I'm moderately pleased with that breakthrough.

I've also attached a copy of the 'schmutzy' one just in case someone sees this and says 'OH YEAH! I know how to fix that.' (crosses fingers)

Attached Files:

File size:
69 KB
Views:
110
File size:
154.4 KB
Views:
94
• empty.jpg
File size:
152.7 KB
Views:
113
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted