No output when plotting data in TGraph through for-loop


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.22/06
Platform: Ubuntu 20.04
Compiler: Not Provided


When I run the following program, I get no output:

#include <TMath.h>

void interference()
{

    float pi = TMath::Pi();

    TGraph *wave1 = new TGraph();
    TGraph *wave2 = new TGraph();

    TGraph *waveCombined = new TGraph();

    for (int i = 0; i < 2 * pi; i+= 0.1)
    {
        wave1->SetPoint(wave1->GetN(), i, sin(i));
        wave2->SetPoint(wave2->GetN(), i, cos(i));

        waveCombined->SetPoint(waveCombined->GetN(), i,  sin(i) + cos(i));
    }

    TCanvas *c1 = new TCanvas();
    wave1->SetTitle("sin");
    wave1->Draw();
    TCanvas *c2 = new TCanvas();
    wave2->Draw();
    wave2->SetTitle("cos");
    TCanvas *c3 = new TCanvas();
    waveCombined->SetTitle("both");
    waveCombined->Draw();
}

It is due to the i+=0.1 in the for loop. I know this because when I change it to i++, it works. What is the issue here?

Replace

    for (int i = 0; i < 2 * pi; i+= 0.1)

by

    for (float i = 0; i < 2 * pi; i+= 0.1)

Note we have AddPoint now.

    for (float i = 0; i < 2 * pi; i+= 0.1) {
        wave1->AddPoint(i, sin(i));
        wave2->AddPoint(i, cos(i));
        waveCombined->AddPoint(i,  sin(i) + cos(i));
    }

Oh ok, thanks.

Ahh ok thanks that makes sense.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.