The Student Room Group

Python getting the value of X when Y=0

Good evening,
I am trying to get the value of X when y=0 from a graph but the problem is, the value of X I get for Y=0 is not the correct one and it also doesn't change if I change the value of Y to something else. I am not sure what I a doign wrong or what I have missed.

#openned a file before where I got the data
time=data[:,0]
signal=data[:,1]

#Getting the maximum value and minimum value.
ymax=max(signal)
xmax = time[signal.argmax()]
ymin=min(signal)
xmin= time[signal.argmin()]

#Interpolating
Newsignal=np.linspace(ymax,ymin,100)
Newtime=np.linspace(xmax,xmin,100)
func=interp.interp1d(time,signal, bounds_error=False)

#Getting the value for when y=0
y_val=0
Valuex=np.interp(y_val,Newsignal,Newtime)

Thank you for any help !
Reply 1
Original post by yseult85
Good evening,
I am trying to get the value of X when y=0 from a graph but the problem is, the value of X I get for Y=0 is not the correct one and it also doesn't change if I change the value of Y to something else. I am not sure what I a doign wrong or what I have missed.

#openned a file before where I got the data
time=data[:,0]
signal=data[:,1]

#Getting the maximum value and minimum value.
ymax=max(signal)
xmax = time[signal.argmax()]
ymin=min(signal)
xmin= time[signal.argmin()]

#Interpolating
Newsignal=np.linspace(ymax,ymin,100)
Newtime=np.linspace(xmax,xmin,100)
func=interp.interp1d(time,signal, bounds_error=False)

#Getting the value for when y=0
y_val=0
Valuex=np.interp(y_val,Newsignal,Newtime)

Thank you for any help !

Not really sure exactly what youre doing as the min/max will just treat the signal as a straight line between those two points and you seem to not use func and linspace runs backwards etc. There is a warning in numpy that the x values are expected to be increasing (your newsignal) otherwise the results are meaningless.
https://numpy.org/doc/stable/reference/generated/numpy.interp.html


But Id chuck it through a debugger (or a few print statements) and put a simple signal in so maybe 3 or 4 points and work through the above. Writing your own version wouldnt be hard by simply finding the index where there is a sign change, and then doing linear interporlation on those two points.
(edited 5 months ago)
Reply 2
Original post by mqb2766
Not really sure exactly what youre doing as the min/max will just treat the signal as a straight line between those two points and you seem to not use func and linspace runs backwards etc. There is a warning in numpy that the x values are expected to be increasing (your newsignal) otherwise the results are meaningless.
https://numpy.org/doc/stable/reference/generated/numpy.interp.html


But Id chuck it through a debugger (or a few print statements) and put a simple signal in so maybe 3 or 4 points and work through the above. Writing your own version wouldnt be hard by simply finding the index where there is a sign change, and then doing linear interporlation on those two points.

The min/max is something I also need to add in my graph this is why I have it.
I didn't see that I put the linspace running backwards, I will fix this.

The func I do use it for my plot later on, I just didn't put whole code in as I mostly want to focus on the np.interp but now that I need to change some stuff maybe it will work better! I will let you know.

Quick Reply

Latest

Trending

Trending