Root Plotting 2d-Array in Histogram

In summary, the student attempted to solve a homework problem involving plotting a 2-d surface of the potential and field lines calculated from a numerical method, but ran into difficulties. The student plans to start work on the second portion of the project once the code is working well, which involves creating field line arrows at each (x,y) coordinate in the axis and forcing the issue with root calculations.
  • #1
Rapier
87
0

Homework Statement


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.
problem.jpg


Homework Equations


N/A

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 (
potential.jpg
) for reference. What I am looking for is something like (
sample.jpg
). 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:
Physics news on Phys.org
  • #2
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 don't 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
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
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)
 

Attachments

  • potential.jpg
    potential.jpg
    45.2 KB · Views: 576
  • arrows.jpg
    arrows.jpg
    126.3 KB · Views: 494
  • empty.jpg
    empty.jpg
    124.6 KB · Views: 253

Related to Root Plotting 2d-Array in Histogram

1. What is root plotting for 2D-arrays in histogram?

Root plotting for 2D-arrays in histogram is a data visualization technique used to display the distribution of numerical data in a 2D-array as a histogram. It is commonly used in scientific research and data analysis to identify patterns and trends in large datasets.

2. How is a histogram different from a bar graph?

A histogram is a type of bar graph that displays the frequency or count of data within specific intervals or bins. It is used to visualize continuous data, while a bar graph is used to display discrete data. Additionally, the bars in a histogram do not have gaps between them, as the data is continuous, while bar graphs have gaps to indicate distinct categories.

3. What is the purpose of root plotting in histogram?

The purpose of root plotting in histogram is to provide a visual representation of the frequency distribution of a 2D-array of data. This allows for easier interpretation and analysis of large datasets, as well as identification of outliers and patterns in the data.

4. How is a 2D-array represented in a histogram?

In a histogram, the x-axis represents the intervals or bins of data and the y-axis represents the frequency or count of data within each interval. The 2D-array is divided into these intervals and the frequency of data falling within each interval is plotted as a bar on the y-axis.

5. Can root plotting in histogram be used for non-numerical data?

No, root plotting in histogram is specifically used for numerical data. For non-numerical data, other data visualization techniques such as pie charts, line graphs, and scatter plots may be more suitable.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
6
Views
3K
  • Introductory Physics Homework Help
Replies
4
Views
2K
  • Materials and Chemical Engineering
Replies
1
Views
3K
  • Biology and Chemistry Homework Help
Replies
2
Views
2K
  • Precalculus Mathematics Homework Help
Replies
6
Views
2K
  • Introductory Physics Homework Help
Replies
2
Views
2K
  • Astronomy and Astrophysics
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Calculus and Beyond Homework Help
Replies
13
Views
3K
Back
Top