TGraph scale problem

I tried to write a code about bifurcation diagram.
I drew data with TGraph, somehow I got a strange graph :unamused:

How i might fix it ?`What is wrong with it ?

By the way, How might i write my script with html code on forum ? That is as ā€œcode> ā€¦ </codeā€, isntā€™ it ? I tried it but it didnā€™t

My script is in the following and graph is in the attachment

;

#include <iostream>
#include "TGraph.h"

using namespace std;

void bifurcationdiagram() {


  Int_t i;
  Int_t max_iter = 50;
  Double_t *x = new Double_t;
  Double_t *r = new Double_t;
  *r = 1.0;
  Double_t count =0;
  //  cout<<*r<<endl;                                                                                                                                                                

  while(*r<=4.5){

    *x=0.1;

  for(i=0;i<=max_iter;i++){

    count++;

    (*x)=(*x)*exp((*r)*(1.0-(*x)));

    cout<<*r<<"\t"<<*x<<"\t"<<count<<endl;

  }

  *r +=0.01;

 }

  TGraph *gr = new TGraph(i,r,x);
  gr->SetLineWidth(2);
  gr->SetLineColor(kRed);
  gr->Draw("AC");

  delete r;
  delete x;

}

Thanks in advance,
Cheers,
Ersel

Use option L instead of option C to draw the graph.

  gr->Draw("AL");

The forum uses markdown syntax. To post code enclose it with ```

```
/// This is code
```

will give

/// This is code

Hi again,

I tried putting L instead C, but problem is the same, I donā€™t understand why the scale of axis occurs in such large values. Data iā€™ve got is as in the following ;

.
.
.
4.18 4.3342e-06
4.18 0.00028331
4.18 0.018497
.
.
.
.

The plot is in the attachment.

Cheers,
Ersel

You havenā€™t declared the arrays properly. Pointers can be dangerous if used incorrectly, Iā€™d suggest using vectors. Print the points in the graph and you will see they are random values from memory (besides the first which is the only memory address you actually set):

gr->Print();

yields:

Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
x[0]=4.51, y[0]=0.00496534
x[1]=1.73061e-77, y[1]=-3.1111e+231
x[2]=6.95255e-310, y[2]=3.10504e+231
x[3]=2.6868e+154, y[3]=6.95255e-310
x[4]=6.95255e-310, y[4]=2.55333e+151
x[5]=0, y[5]=6.01666e+151
x[6]=6.95255e-310, y[6]=1.92102e+170
x[7]=2.18877e-314, y[7]=7.33729e+223
x[8]=6.95255e-310, y[8]=1.27951e+199
x[9]=6.95255e-310, y[9]=1.72632e-306
x[10]=2.122e-314, y[10]=6.95255e-310
x[11]=6.95255e-310, y[11]=-4.34373e-311
x[12]=6.95255e-310, y[12]=2.52962e-321
x[13]=6.95255e-310, y[13]=6.95255e-310
x[14]=0, y[14]=6.95255e-310
x[15]=0, y[15]=6.95255e-310
x[16]=6.95255e-310, y[16]=6.95314e-310
x[17]=6.95255e-310, y[17]=0
x[18]=6.95255e-310, y[18]=6.95251e-310
x[19]=6.95255e-310, y[19]=6.95255e-310
x[20]=6.95255e-310, y[20]=2.122e-314
x[21]=6.95255e-310, y[21]=6.95255e-310
x[22]=6.95255e-310, y[22]=0
x[23]=6.95255e-310, y[23]=2.18872e-314
x[24]=6.95255e-310, y[24]=6.95255e-310
x[25]=6.95255e-310, y[25]=6.95255e-310
x[26]=-3.1111e+231, y[26]=2.68156e+154
x[27]=-2.00391, y[27]=6.95255e-310
x[28]=6.95255e-310, y[28]=8.09625e-320
x[29]=6.95255e-310, y[29]=6.95255e-310
x[30]=6.95255e-310, y[30]=0
x[31]=2.18877e-314, y[31]=0
x[32]=6.95255e-310, y[32]=0
x[33]=6.95255e-310, y[33]=0
x[34]=2.122e-314, y[34]=6.95255e-310
x[35]=6.95255e-310, y[35]=6.95255e-310
x[36]=6.95255e-310, y[36]=6.95255e-310
x[37]=6.95255e-310, y[37]=6.95255e-310
x[38]=0, y[38]=6.95255e-310
x[39]=6.95255e-310, y[39]=6.95255e-310
x[40]=3.1111e+231, y[40]=6.95255e-310
x[41]=-1.29075e-231, y[41]=6.95255e-310
x[42]=9.88131e-324, y[42]=0
x[43]=3.4766e-309, y[43]=0
x[44]=6.95255e-310, y[44]=3.39519e-313
x[45]=6.95255e-310, y[45]=6.95255e-310
x[46]=6.95255e-310, y[46]=6.95251e-310
x[47]=2.18877e-314, y[47]=1.8774e-308
x[48]=6.95255e-310, y[48]=6.95255e-310
x[49]=6.95255e-310, y[49]=-1.15331e-291
x[50]=2.12433e-314, y[50]=6.95255e-310

It does make some fun plots though:

Could you explain the goal of the plot? I canā€™t even guess from your code what you would like to be plotting.

Maybe you wanted this:

#include "TH2D.h"

void bifurcationdiagram() {
  Int_t max_iter = 50;
  Double_t x;
  Double_t r = 1.0;
  
  TH2D *h = new TH2D("h","H", 200, 1, 4.5, 200, -2, 10);
  while(r <= 4.5){
     x = 0.1;
     
     for(int i=0;i<=max_iter;i++){
        x = x * exp( r * (1.0 - x) );
        h->Fill(r, x);
     }
     
     r += 0.01;
  }
  h->Draw();
}

Thank you very much :wink:

Ok, then, why did i use TGraph instead of Histogram :dizzy_face:

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