Plotting sum of 1000 function

Hi!
As in topic I tried to add 1000 function and then plot it. I had some ideas however none of them works. It is my beginning with root so maybe it is quite basic but I can’t see it. This is what I tried to do:

#include "TF1.h"

#include "cmath"

Double_t fun(Double_t *x, Double_t *par) {
	double sr = 0;
	for(int j = 3; j < 1003; j++ ) {
		Double_t c = par[j] * cos(par[0])*sin(par[1])*sin(par[2] * x[0]  / (j - 3) );
		sr = sr + c;
	}
	Double_t wy = sr ;
	return wy;
}

void macro1() {
	
		gROOT->SetStyle("Plain");

		double w[1000];
		for (int i = 0; i < 1000; i++) {
			double en = 0.01 * i + 0.01;
			w[i]= TMath::Gaus(en, 5, 3);
		}


		double tjt = 1;
		double tdt = 2;
		double mo = 7;
		
	

			TF1 mm1("mm1", fun, 0.01, 5000);
			mm1.SetParameters(tjt, tdt, mo, w);

			

			mm1.SetTitle("xx ;yy;zz");
	

		TCanvas* Ccanvas = new TCanvas();
		
	mm1.DrawClone();

	Ccanvas->SetLogx();
		
	
}

I hope this will not be a problem for you and somebody will help me.

I’m using root 5.

#include "TROOT.h"
#include "TMath.h"
#include "TF1.h"
#include "TCanvas.h"

#include <cmath>

double fun(const double *x, const double *par) {
  double sr = 0.;
  for (int j = 3; j < 1003; j++) {
    sr += par[j] * std::cos(par[0]) * std::sin(par[1]) * std::sin(par[2] * x[0] / (j - 2.));
  }
  return sr;
}

void macro1() {
  gROOT->SetStyle("Plain");
  
  TF1 *mm1 = new TF1("mm1", fun, 0.01, 5000., 1003);
  mm1->SetTitle("xx ;yy;zz");
  mm1->SetNpx(1000);
  
#if 0 /* 0 or 1 */
  double w[1003];
  w[0] = 1.; // tjt
  w[1] = 2.; // tdt
  w[2] = 7.; // mo
  for (int i = 3; i < 1003; i++) {
    double en = 0.01 * (i - 3) + 0.01;
    w[i]= TMath::Gaus(en, 5., 3.);
  }
  mm1->SetParameters(w);
#else /* 0 or 1 */
  mm1->SetParameter(0, 1.); // tjt
  mm1->SetParameter(1, 2.); // tdt
  mm1->SetParameter(2, 7.); // mo
  for (int i = 3; i < 1003; i++) {
    double en = 0.01 * (i - 3) + 0.01;
    mm1->SetParameter(i, TMath::Gaus(en, 5., 3.));
  }
#endif /* 0 or 1 */
  
  TCanvas* Ccanvas = new TCanvas();
  mm1->Draw();
  Ccanvas->SetLogx();
}

BTW. When you post “source code” or “output” here, do remember to enclose them into two lines which contain just three characters ``` (see how your post has been edited above).

1 Like

Thank you very much! It works perfectly.

And yes, sorry for those missing “```”, I will remember next time.

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