2

I'm working on an investigation proyect, where I have to plot some experimetal data, and then find the max value (critical point), like on this image:

Example_image

I was trying to adjust a fit curve (using a cuadratic equation) in order to find that max value, and I am looking to optimize that curve right now. I've tried with maxarray, but there is an specific value that doesn´t fix to the tendence.

This is what I've done until now:

import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np
import scipy

# Subo los datos experimentales #
datos=np.loadtxt("OFET2.txt", delimiter="\t")
x=datos[:,1]
y=datos[:,4]
n=len(x)
dr=np.diff(y)/np.diff(x)
dx=datos[:n-1,1]

def func(x, a, b, c):
    return -(a*x+b*x**2-c)       # Este x no tiene nada que ver con el dato de x

plt.plot(dx, dr, label='experimental-data')

popt, pcov = curve_fit(func, dx, dr)
print(popt)

#x values for the fitted function
xFit = np.arange(-100, 400, 0.01)       

#Plot the fitted function
plt.plot(xFit, func(xFit, *popt), 'r--', label='fit params: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))

plt.xlabel('x')
plt.ylabel('y')
plt.grid()
plt.legend()
plt.show()

And here there is an image of that plot:

Ploted graphic

An idea was to fit the curve just to the "y" values greater than 0.008 (y<0.008), but actually I don't know how to do it. After that, I would need to (somehow) point the max value of the curve. I should try to get something like this I would be really thankful if you could help me to solve this,either like on the first image, or fixing my script. I am also keen on any advice or correction you might have about my script or some techniques, since I'm kinda new on Python.

Greetins!

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • Really nice first question - I found a duplicate that should solve your problem.The accepted answer seems to do exactly what you need to do. I found it using google: `python fit curve through points site:stackoverflow.com` – Patrick Artner Sep 18 '20 at 05:36
  • I would suggest using slices of your data - the values seem to be tightly packed, so try every 10th or so value, every 50th value etc. then check the highest points via f.e. [finding-the-maximum-of-a-curve-scipy](https://stackoverflow.com/questions/29954153/finding-the-maximum-of-a-curve-scipy) and see how much delta it gives you to a real point on your dataset until you get an approcimation thats good enough for you. – Patrick Artner Sep 18 '20 at 05:39
  • Thanks for such a quick answer! After some time, I was able to solve it perfectly with the pages you've recommend me, even better than I was thinking! Thank you very much for your help! – Francisco Amante Sep 19 '20 at 01:39
  • happy tpo help. Thats the cool thing about SO - knowledge abundance albeit sometimes difficult to unearth.Stick to google and `site:stackoverflow` and try 3-5 different searchterms which you would describe the problem to your younger sibling thats not into it and you`ll eventually find a solution – Patrick Artner Sep 19 '20 at 09:30

0 Answers0