C++ how to obtain the x value of graph corresponding to a certain y value

In summary: You need to have the second form. I think the compiler is getting confused when it sees the second ) and is expecting a semicolon instead.
  • #1
lavster
217
0

Homework Statement



i have drawn a graph in C++/root which is a bunch of points (in an array) joined together ie it is not a function. it looks kind of like a gauss, but isn't ie its a peak. i want to find out the x distance between the points of 90% and 20% of the maximum. i am having difficulty finding how from knowing the value of y to obtain the corresponding x value of eg the 90% level. I can do it from clicking on the graph but i want to write some code to find it and print it on screen using the cout thingy

Homework Equations


The Attempt at a Solution



my array is called Es[]. to find the maximum value i have made a for loop;
Code:
 float max = 0;
  max=Es[0];
  for(int n=0; n<40; n++)
    {
      if(Es[n]>max)
	max=Es[n];
    }
 
  cout << "max value of energy deposited =" << max << "MeV" <<endl;

so i know what the y value is. I have no idea how to get the corresponding x value.

by graph is defined as so:

Code:
 TGraph *g =new TGraph(h->GetNbinsX());//defining a graph
 g->SetTitle("hi");

 cout << "hello" << endl;
 float graphPointNumber=0;//where we are
  cout << "salut" << endl;
  for(int m=1;  m<=h->GetNbinsX();m++)
   {
      cout << "hi" << endl;
      cout << "m="<< m  << endl;

     g->SetPoint(graphPointNumber,h->GetBinCenter(m),Es[m]); 
     graphPointNumber++;
 cout << "value =" << Es[m] << "MeV" << endl;

   }

can anyone tell me how to do this please?

thanks
 
Physics news on Phys.org
  • #2
You have already found the largest value in the Es array - call it maxEs.
Calculate the two values .2 * maxEs and .9 * maxEs.
Loop through your Es array looking at successive pairs of Es and Es[i + 1] to find the values that straddle .2 * maxEs. (It's not likely that an array value will be exactly equal to .2*maxEs.)
For the left x-value you could use i or i + 1, or the average of the two, which is (2i + 1)/2. Note that this is not an actual x value - it's just the index of the array of Es values. In your previous work you mapped x values to interval numbers, so you will need to do the opposite to map array indexes back to x values.

For the right x-value, do pretty much the same thing, except this time you're looking for successive values in your array that straddle .9 * maxEs.
 
  • #3
thanks! i think i pretty much followed what you said. however it doesn't work so i think I am doing something stupid... Here is what i wrote:

Code:
float max = 0;
  max=Es[0];
  for(int n=0; n<40; n++)
    {
      if(Es[n]>max)
        max=Es[n];
    }
 
  cout << "max value of energy deposited =" << max << "MeV" <<endl;

  float m90;
  m90=0.9*max;

cout << "90% level =" << m90 << "MeV" <<endl;

 float m20;
 m20=0.2*max;

cout << "20% level =" << m20 << "MeV" <<endl;


 float ind1;
 for(int n=0;n<40;n++)
   {
    
     if(m20>=Es[n]) && (m20<Es[n+1]))
   {
     ind1 =(2*n+1)/2;
   }
 
}

float a;
a=Phantom_xpre[ind1];
                                       
                                        cout << "index" << ind1 << "\t x position =" << a << "mm" <<endl;



 float ind2;
 for(int n=0;n<40;n++)
   {
    
     if(m90>=Es[n]) && (m90<Es[n+1]))
   {
     ind2 =(2*n+1)/2;
   }
 
}

float b;
b=Phantom_xpre[ind2];
                       
        cout << "index" << ind2 << "\t x position =" << b << "mm" <<endl;              

float c=b-a;

        cout << "penumbra =" << c << "mm" <<endl;

and the error message i receive is:

Error: reference tyoe $ with no initialization
Error: Incorrect referencing of $ graphy.C

I don't know what this means.

Can you see where i am going wrong?

thanks
 
  • #4
The errors don't seem to have anything to do with the code you show. At least I don't see what they're referring to.

Are these compiler errors or runtime errors? Are there line numbers and filenames given with the error information?

The only thing I see, and I don't think it's related to the errors you're seeing, is where you have float c = b - a; down near the bottom. I don't see a mentioned anywhere else, so if a doesn't have a known value, you're going to get garbage for c.
 
  • #5
Mark44 said:
The errors don't seem to have anything to do with the code you show. At least I don't see what they're referring to.

Are these compiler errors or runtime errors? Are there line numbers and filenames given with the error information?

The only thing I see, and I don't think it's related to the errors you're seeing, is where you have float c = b - a; down near the bottom. I don't see a mentioned anywhere else, so if a doesn't have a known value, you're going to get garbage for c.

a is defined, before b but after ind1.

it is when i process it. so i don't no if that's a compiler error or runtime error. yes it gives line number to be the line where i cout the 20% level. but it gives prints the 20% max level to the screen suggesting that it isn't this line that's the problem. but it doesn't do the for loop straight after. and its in my emacs file I am writing in. "graphy.C"

i thought that since my graph is a peak it will have two 90% max points and 20% max points instead of just the one so this may cause problems. but the peak is symmetric so it doesn't really matter which pair i choose... do you think this is the problem?

thanks
 
  • #6
Your if statements for m20 and m90 are malformed. Instead of this:
if(m20>=Es[n]) && (m20<Es[n+1]))

do this:
if( (m20>=Es[n]) && (m20<Es[n+1]) )

The syntax for the if statement is
1. if (expression) stmt;
or
2.
Code:
if (expression)
{
    stmt1;
    stmt2;
    etc.
}
 

1. How do I use C++ to obtain the x value of a graph for a given y value?

The first step is to have the graph data stored in a suitable data structure, such as an array or vector. Then, you can iterate through the data and check the y values to find the corresponding x value. Alternatively, you can use a built-in function like std::find to search for the y value and return its corresponding x value.

2. Can I use C++ to interpolate or extrapolate the x value for a given y value?

Yes, you can use mathematical algorithms and functions in C++ to interpolate or extrapolate the x value for a given y value. Some common methods for interpolation include linear interpolation, polynomial interpolation, and spline interpolation. The appropriate method to use will depend on the nature and complexity of your graph data.

3. Is there a library or framework in C++ specifically for obtaining x values from graphs?

Yes, there are various libraries and frameworks available for data analysis and graph manipulation in C++. Some popular options include the Boost C++ Libraries, GNU Scientific Library (GSL), and Armadillo. These libraries provide functions and algorithms for various mathematical operations, including obtaining x values from graphs.

4. Can I use C++ to plot a graph and then obtain the x value for a given y value?

Yes, you can use C++ to plot graphs using libraries such as GNUplot or Qt. However, obtaining the x value for a given y value will require additional coding and mathematical operations. It may be more efficient to store the graph data in a data structure and use the methods mentioned in the first question to obtain the x value.

5. Are there any specific data types or data structures in C++ that are best for storing graph data?

There is no specific data type or structure that is universally considered the best for storing graph data in C++. The choice will depend on the nature and complexity of your data and the operations you want to perform on it. Some commonly used options include arrays, vectors, and linked lists. It is important to choose a data structure that allows for efficient access and manipulation of data for your specific use case.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
23
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
Back
Top