牛叔叔 的笔记

好好学习

2020-03-25 00:38

一个三角函数拟合程序演示

牛叔叔

Python

(1421)

(0)

收藏

blog

函数:

 (2*b*(a+x)/(b**2+4*(a+x)**2)+math.atan(2*(a+x)/b)-2*b*a/(b**2+4*a**2)-math.atan(2*a/b))*(-c**2*b/4/9.81*1022.85)


在开发时,注意math.atan要替换成np.arctan函数,否则因为参数类型原因会报错:

only size-1 arrays can be converted to Python scalars

#encoding=utf-8
import math

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

#自定义函数
def func(x, a, b, c):
    return (2 * b * (a + x) / (b ** 2 + 4 * (a + x) ** 2) + np.arctan(2 * (a + x) / b) - 2 * b * a / (
                 b ** 2 + 4 * a ** 2) - np.arctan(2 * a / b)) * (-c ** 2 * b / 4 / 9.81 * 1022.85)
#使用实例函数定义x、y散点坐标
x = np.linspace(-2*np.pi,2*np.pi,100)
print('x is :\n',x)
y=func(x,3.1,4.2,8.3) + 1000 * np.random.normal(size=len(x))
print('y is :\n',y)

popt, pcov = curve_fit(func, x, y)
#获取popt里面是拟合系数
a = popt[0]
b = popt[1]
c = popt[2]


yvals = func(x,a,b,c) #拟合y值
print(u'系数a:', a)
print(u'系数b:', b)
print(u'系数c:', c)

#绘图
plot1 = plt.plot(x, y, 's',label='original values')
plot2 = plt.plot(x, yvals, 'r',label='polyfit values')
plt.xlabel('x')
plt.ylabel('y')
plt.legend(loc=1) #指定legend的位置
plt.title('curve_fit')
plt.show()

运行结果

x is :
 [-6.28318531 -6.15625227 -6.02931923 -5.9023862  -5.77545316 -5.64852012
 -5.52158709 -5.39465405 -5.26772102 -5.14078798 -5.01385494 -4.88692191
 -4.75998887 -4.63305583 -4.5061228  -4.37918976 -4.25225672 -4.12532369
 -3.99839065 -3.87145761 -3.74452458 -3.61759154 -3.4906585  -3.36372547
 -3.23679243 -3.10985939 -2.98292636 -2.85599332 -2.72906028 -2.60212725
 -2.47519421 -2.34826118 -2.22132814 -2.0943951  -1.96746207 -1.84052903
 -1.71359599 -1.58666296 -1.45972992 -1.33279688 -1.20586385 -1.07893081
 -0.95199777 -0.82506474 -0.6981317  -0.57119866 -0.44426563 -0.31733259
 -0.19039955 -0.06346652  0.06346652  0.19039955  0.31733259  0.44426563
  0.57119866  0.6981317   0.82506474  0.95199777  1.07893081  1.20586385
  1.33279688  1.45972992  1.58666296  1.71359599  1.84052903  1.96746207
  2.0943951   2.22132814  2.34826118  2.47519421  2.60212725  2.72906028
  2.85599332  2.98292636  3.10985939  3.23679243  3.36372547  3.4906585
  3.61759154  3.74452458  3.87145761  3.99839065  4.12532369  4.25225672
  4.37918976  4.5061228   4.63305583  4.75998887  4.88692191  5.01385494
  5.14078798  5.26772102  5.39465405  5.52158709  5.64852012  5.77545316
  5.9023862   6.02931923  6.15625227  6.28318531]
y is :
 [ 2.07281404e+04  2.27189464e+04  2.07020572e+04  2.19094737e+04
  2.15517490e+04  1.92623067e+04  2.10199859e+04  2.12591784e+04
  1.99465687e+04  2.06059158e+04  1.97719072e+04  2.05942927e+04
  1.95583168e+04  1.94393931e+04  1.87423303e+04  1.80745964e+04
  1.91406125e+04  1.64073068e+04  1.77216138e+04  1.67012355e+04
  1.48767554e+04  1.47983845e+04  1.20528441e+04  1.23137902e+04
  9.94239990e+03  1.01346365e+04  1.01613007e+04  6.66934250e+03
  8.65848225e+03  5.56242336e+03  8.39393337e+03  4.64619061e+03
  5.25357639e+03  4.92293235e+03  3.98864889e+03  3.03121705e+03
  3.00227910e+03  3.55133470e+03  6.74696578e+02  3.76948451e+03
  1.17308382e+03  1.82316520e+03  7.96402262e+02  2.73996876e+03
  8.64548975e+02 -4.89958943e+02 -4.86396308e+02  1.25931245e+03
  1.36633896e+03  5.53603515e+02  4.22362764e+02 -1.76172474e+03
  9.06685556e+02  3.00839316e+01  1.62736565e+02  4.34331031e+02
  1.11070760e+03 -1.78126560e+03 -2.47896476e+02 -1.63080954e+03
 -4.04295429e+02  1.09933004e+02  1.73464695e+02  8.30625336e+01
 -1.72331935e+03 -5.36477029e+02  1.17361348e+03  1.62146302e+03
 -1.22800268e+03 -3.35859392e+02 -9.02540981e+02 -1.62065536e+03
 -1.39810032e+03 -2.96341689e+02 -2.69387783e+02 -1.58182553e+03
 -2.90597098e+00  7.94920580e+02  1.03550459e+02 -2.65421325e+03
 -2.35116023e+03 -3.86355345e+02 -3.40793609e+02 -2.16954829e+03
 -1.46430254e+03  1.58249171e+03  2.80709867e+02 -2.29916052e+02
  1.91626299e+02 -2.77934884e+03 -6.98147786e+02 -9.56310173e+02
 -8.21907005e+01 -1.01821672e+03  6.93071410e+02 -1.22856679e+03
 -6.65971857e+02  1.43685483e+02 -1.01433290e+03 -3.15992746e+02]
系数a: 3.1465334991833642
系数b: 3.951342820743008
系数c: 8.479197062909357

Process finished with exit code 0

微信图片_20200325003801.jpg

0条评论

点击登录参与评论