博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python曲线拟合
阅读量:4613 次
发布时间:2019-06-09

本文共 1313 字,大约阅读时间需要 4 分钟。

http://blog.sina.com.cn/s/blog_aed5bd1d0102vid7.html

1.多项式拟合范例:

import matplotlib.pyplot as pltimport numpy as npx = np.arange(1, 17, 1)y = np.array([4.00, 6.40, 8.00, 8.80, 9.22, 9.50, 9.70, 9.86, 10.00, 10.20, 10.32, 10.42, 10.50, 10.55, 10.58, 10.60])z1 = np.polyfit(x, y, 3) # 用3次多项式拟合p1 = np.poly1d(z1)print(p1) # 在屏幕上打印拟合多项式yvals=p1(x) # 也可以使用yvals=np.polyval(z1,x)plot1=plt.plot(x, y, '*',label='original values')plot2=plt.plot(x, yvals, 'r',label='polyfit values')plt.xlabel('x axis')plt.ylabel('y axis')plt.legend(loc=4) # 指定legend的位置,读者可以自己help它的用法plt.title('polyfitting')plt.show()plt.savefig('p1.png')

2.指定函数拟合

# 使用非线性最小二乘法拟合import matplotlib.pyplot as pltfrom scipy.optimize import curve_fitimport numpy as np# 用指数形式来拟合x = np.arange(1, 17, 1)y = np.array([4.00, 6.40, 8.00, 8.80, 9.22, 9.50, 9.70, 9.86, 10.00, 10.20, 10.32, 10.42, 10.50, 10.55, 10.58, 10.60])def func(x,a,b):    return a*np.exp(b/x)popt, pcov = curve_fit(func, x, y)a=popt[0] # popt里面是拟合系数,读者可以自己help其用法b=popt[1]yvals=func(x,a,b)plot1=plt.plot(x, y, '*',label='original values')plot2=plt.plot(x, yvals, 'r',label='curve_fit values')plt.xlabel('x axis')plt.ylabel('y axis')plt.legend(loc=4) # 指定legend的位置,读者可以自己help它的用法plt.title('curve_fit')plt.show()plt.savefig('p2.png')

 

转载于:https://www.cnblogs.com/jingsupo/p/python_curve_fit.html

你可能感兴趣的文章
linux的kernel是怎样工作的(TI_DM36X_ARM系统)(1)
查看>>
[luogu4310] 绝世好题 (递推)
查看>>
[luogu3203 HNOI2010] 弹飞绵羊 (分块)
查看>>
mui搜索框 搜索点击事件
查看>>
2016012003+陈琦+散列函数的应用及其安全性
查看>>
Android 状态栏通知Notification、NotificationManager详解
查看>>
UIApplicationDelegate协议
查看>>
Jmeter测试dubbo接口填坑
查看>>
[zz]GDB调试精粹及使用实例
查看>>
数据库的创建和删除
查看>>
最简单的三层实例【插入据
查看>>
设计模式学习笔记——Prototype原型模式
查看>>
pom.xml里有红叉报错的解决办法
查看>>
Perl last和next的用法区别
查看>>
Selenium 管理 Cookies
查看>>
exceptionfunction[LeetCode]Permutations
查看>>
xp_cmdshell 命令的开启与关闭,和状态查询
查看>>
Linux sudoers
查看>>
bzoj 4595 激光发生器
查看>>
multi cookie & read bug
查看>>