TGraph2D Number Error

Please read tips for efficient and successful posting and posting code

ROOT Version: Latest as of 23.02.21
Platform: Ubuntu
Compiler: Not Provided

I’m having an error with TGraph2D, where I am getting values of zero at x = 0 and y = 0 when I cout them, but they are higher than zero on the TGraph2D. What is the problem here?![

Maybe I don’t understand your question, but I see a value of zero at [0,0], but maybe @couet can also take a look

Yes, the value at [0,0] is also graphically 0, but the edges are curved in the positive direction. According to the formula I am plotting, the edges should also be 0.

Oh, OK, thanks for the clarification. Then I’ll let @couet comment on that

1 Like

Can you post your macro ?

What’s a macro :sweat_smile:

Well, post the instructions you are doing which lead to tell something is wrong.

ROOT does not draw “external edges” in this case. The first drawn lines correspond to some “bin centers” (a 2D graph is drawn with help of an automatically created histogram). So, even if the values on “external edges” are exactly 0, the values at the “bin centers” of the most “external bins” are not.

Okay. Here is my entire code for refrence:

// Created by AdvaitD on 22/02/2021.
// Display the wavefunction and amplitude of an electron in a box

#include <TMath.h>
#include <math.h>

float pi = TMath::Pi();

float L = 100; // m

int nx, ny, n;

float roundval(float var)
    float value = (int)(var*100+.5);
    return (float)value/100;
float waveFunction2D(float energyLevelx, float energyLevely, float lengthx, float lengthy)

    	float wave = (2/L) * ( roundval(sin(energyLevelx * (lengthx/L) *pi)) )*( roundval(sin(energyLevely * (lengthy/L) *pi)));
    	return wave;

float Amplitude2D(float wave)
    float amp =  wave * wave;

    return amp;

void electrons()
    cout << "Enter a value for n in x direction:" << endl;
    cin >> nx;
    cout << " Enter a value for n in y direction:" << endl;
    cin >> ny;

    TGraph2D *WaveFunctionTwo = new TGraph2D();
    TGraph2D *AmpTwo = new TGraph2D();

    int count = 0;
    for (int j = 0; j <= L; j++ )
        for (int k = 0; k <= L; k++)
            WaveFunctionTwo->SetPoint(count, j, k, waveFunction2D(nx, ny, j, k));
            AmpTwo->SetPoint(count, j, k, Amplitude2D(waveFunction2D(nx, ny, j, k)));

    TCanvas *c1 = new TCanvas();
    TCanvas *c2 = new TCanvas();

Now, the value that the function waveFunction2D() returns is 0 when lengthx == 0, lengthx == L, lengthy == 0 and lengthy == L. These are the edges. When I cout this in the function itself, I get a bunch of zeros. But when I plot them on the TGraph2D, they seem to be above zero in a curve.

you macro wants some input values:

root [0] 
Processing electrons.C...
Enter a value for n in x direction:

What should I enter ?

1 both times

So I get the plot you posted …
What should I do now to see the wrong behavior ?

The edges are a bit curved when seen from the side, even though the calculated value is 0, i.e. they should be flat.

I plotted with the option LEGO and zoomed on the Z axis I get this:

The lego and surface are obtained by filling an underlying histogram. As you can see the bin on the edge are not really 0. Only the conners are. It might be that the bins are to large and that one bin has several triangle above. You can try to increase the number of bins of the underlying histogram (SetNpx SetNpy see doc)

Ok, thanks, I’ll try doing that.

the triangles around (0,0)

and only two bins around (0,0)

I did this and it seems better now. Thanks.

Okay, I have one more question. Why is this occurring with the Wavefunction graph but not the Amplitude Graph?

The plots I posted are the AmpTwo graph

1 Like

Oh ok. I was talking about the wavefunction graph.