Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
restrepo
GitHub Repository: restrepo/ComputationalMethods
Path: blob/master/homework/homework_2018_1_06_1035441007.ipynb
934 views
Kernel: Python 3
%pylab inline import pandas as pd from scipy import optimize
Populating the interactive namespace from numpy and matplotlib
#Generamos los puntos df= pd.DataFrame({'X':[1.2,2.3,3.6,4.1,5.2,6.7,7.3,8.1], 'Y':[-6.59,-2.66,2.36,34.8,76.5,7.77,2.45,1.40]})
'''Interpolamos los puntos con un polinomio de grado alto Para este caso es grado 8 puesto que es el que mejor se ajusta y que describe curvas más suaves''' coeffs=np.polyfit(df.X,df.Y,deg=7) P=np.poly1d(coeffs) print (P)
7 6 5 4 3 2 -0.04067 x + 1.022 x - 8.716 x + 20.45 x + 100.4 x - 640.7 x + 1179 x - 696.1
plt.plot(df.X,df.Y,'ro', label='Puntos a interpolar') x=np.linspace(df.X[0],df.X[7]) plt.plot(x,P(x), "b", label='Polinomio de interpolación') plt.legend()
<matplotlib.legend.Legend at 0x7fecfe38c320>
Image in a Jupyter notebook
#Implementando Scipy, hallamos el mínimo relativo y absoluto r=optimize.fmin_powell(P,7, full_output=True)#Minimo local a=optimize.fmin_powell(P,3, full_output=True)#Minimo global
Optimization terminated successfully. Current function value: 0.535360 Iterations: 2 Function evaluations: 50 Optimization terminated successfully. Current function value: -18.861286 Iterations: 2 Function evaluations: 40
print ('El mínimo absoluto y el mínimo relativo son, respectivamente {},{} y {},{}'.format(a[0],a[1],r[0],r[1]))
El mínimo absoluto y el mínimo relativo son, respectivamente 2.915336201653282,-18.861285909179855 y 7.089721927659005,0.5353595217214888