cambaluc的个人博客分享 http://blog.sciencenet.cn/u/cambaluc

博文

数值逼近Python程序

已有 453 次阅读 2024-12-8 12:55 |个人分类:Python|系统分类:教学心得| python, python

插值,三次样条,拟合一锅烩:

#interpolate_fit ,kangjian20191016

import numpy as np

from scipy.interpolate import spline

from scipy.interpolate import interp1d

import matplotlib.pyplot as plt

x=np.array([0,5,10,16,20])

y=np.array([1,4, 6, 5,9.2])

xx=np.linspace(0,20,80)

f1=interp1d(x,y)

f2=interp1d(x,y,kind='quadratic')

f3=interp1d(x,y,kind='cubic')

f,ax=plt.subplots(2,2)

ax[0,0].plot(x,y,'o',xx,f1(xx),'.')

ax[0,1].plot(x,y,'o',xx,f2(xx),'-')

ax[1,0].plot(x,y,'o',xx,f3(xx),'-')

sp=spline(x,y,xx)

ax[1,1].plot(x,y,'o',xx,sp,'.')

#plt.savefig('fit1.png',dpi=400,bbox_inches='tight')

plt.show()

plt.scatter(x,y,label='点')

poly1= np.polyfit(x, y, deg = 1)

plt.plot(xx, np.polyval(poly1, xx),'.',label='1次')

poly2= np.polyfit(x, y, deg = 2)

plt.plot(xx, np.polyval(poly2, xx),'.',label='2次')

poly3= np.polyfit(x, y, deg = 3)

fun=np.poly1d(poly3)##poly1d

plt.plot(xx, fun(xx),color='r',label='3次')

plt.legend(loc='best')

#plt.savefig('fit2.png',dpi=400,bbox_inches='tight')

plt.show()

最佳平方逼近,按原理

# -*- coding: utf-8 -*-

"""Created on Thu Oct 17 11:11:00 2019

@author: kangjian

"""

import numpy as np

import matplotlib.pyplot as plt

x=np.array([1,   2,   3,   4,    5.5, 6])

y=np.array([1.2, 3.5, 5.5, 7.25, 8,   7.4])

def fi0(x):

    return x*0+1

def fi1(x):

    return x

def fi2(x):

    return np.sin(x)

G=np.matrix(([np.dot(fi0(x),fi0(x)),np.dot(fi0(x),fi1(x))],

               [np.dot(fi1(x),fi0(x)),np.dot(fi1(x),fi1(x))]  ))

b=np.matrix(([np.dot(fi0(x),y)],[np.dot(fi1(x),y)]))

a=np.linalg.solve(G,b)

print(a)

xx=np.linspace(1,6,100)

plt.scatter(x,y)

fun=a[0,0]*fi0(xx)+a[1,0]*fi1(xx)

plt.plot(xx, fun,'.',color='r')

G=np.matrix(([np.dot(fi0(x),fi0(x)),np.dot(fi0(x),fi1(x)),np.dot(fi0(x),fi2(x))],

               [np.dot(fi1(x),fi0(x)),np.dot(fi1(x),fi1(x)),np.dot(fi1(x),fi2(x))],

               [np.dot(fi2(x),fi0(x)),np.dot(fi2(x),fi1(x)),np.dot(fi2(x),fi2(x))] ))

b=np.matrix(([np.dot(fi0(x),y)],[np.dot(fi1(x),y)],[np.dot(fi2(x),y)]))

a=np.linalg.solve(G,b)

print([a[2,0],a[1,0],a[0,0]])

fun2=a[0,0]*fi0(xx)+a[1,0]*fi1(xx)+a[2,0]*fi2(xx)

plt.plot(xx, fun2,'.',color='b')

#plt.savefig('fit3.png',dpi=400,bbox_inches='tight')

plt.show()

polyfit3.png

同样的数据,直接调函数

import numpy as np

import matplotlib.pyplot as plt

x=np.array([1,2,3,4,5.5,6])

y=np.array([1.2,3.5,5.5,7.25,8,7.4])

xx=np.linspace(1,6,100)

plt.scatter(x,y)

mypoly= np.polyfit(x, y,  1)

plt.plot(xx, np.polyval(mypoly, xx),'.',color='r')

mypoly2= np.polyfit(x, y,  2)

plt.plot(xx, np.polyval(mypoly2, xx),'.',color='g')

mypoly3= np.polyfit(x, y,3)

fun=np.poly1d(mypoly3)#函数形式

plt.plot(xx,fun(xx),'.',color='b')

#print(mypoly)

plt.savefig('fit4.png',dpi=400,bbox_inches='tight')

plt.show()

polyfit4.png



https://blog.sciencenet.cn/blog-797552-1202356.html

上一篇:Python小例:统计文本中单词出现的频次
下一篇:复变函数w=z^2的图
收藏 IP: 111.199.241.*| 热度|

5 宁利中 郑永军 王安良 孙颉 杨正瓴

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-12-25 01:50

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部