首页 > 其他分享 >第七章例题及习题

第七章例题及习题

时间:2024-11-18 12:29:44浏览次数:1  
标签:plt y0 第七章 rcParams np import 习题 例题 x0

例7.3

import numpy as np
import pylab as plt
from scipy. interpolate import lagrange
yx=lambda x: 1/(1+x**2)
def fun(n):
    x=np.linspace(-5,5,n+1)
    p=lagrange(x,yx(x))
    return p
x0=np.linspace(-5,5,100)
plt.rc('text',usetex=True)
plt.rc('font',size=15);
plt.style.use('default')
N=[6,8,10]
s=['--*b','-.','-p']
for k in range(3):
    p=fun(N[k]);plt.plot(x0,np.polyval(p,x0),s[k])
plt.plot(x0,yx(x0));
plt.legend(['$n=6$','$n=8$','$n=10$','$1/(1+x^2)$'])
plt.show()

例7.4

import numpy as np
from scipy.interpolate import interp1d
from scipy.interpolate import lagrange
import pylab as plt

a = np.loadtxt('C:/Users/qazws/OneDrive/python代码/python建模作业/data7_4.txt')
x0=a[0] ;y0=a[1]
x = np.linspace(0, 15, 151)
yx1=interp1d(x0,y0)
y1=yx1(x)
p2=lagrange(x0,y0)
y2=np.polyval(p2,x)
yx3=interp1d(x0,y0,'cubic')
y3 = yx3(x)
dx=np.diff(x);dy=np.diff(y3)
dyx=dy/dx;dyx0=dyx[0]
xt=x[130:];yt=y3[130:]
ymin=min(yt)
xmin=[xt[ind]for ind,v in enumerate(yt) if v==ymin]
print('x=0处的斜率数值解为:',dyx0)
print('xmin=',xmin);print('ymin=',ymin)
plt.rc('font',family='SimHei')
plt.rc('axes',unicode_minus=False)
plt.subplots_adjust(wspace=0.5)
plt.subplot(131);plt.plot(x,y1)
plt.title("分段线性插值")
plt.subplot(132);plt.plot(x,y2)
plt.title("拉格朗日插值")
plt.subplot(133);plt.plot(x,y3)
plt.title('三次样条插值')
plt.show()

习题7.3

import numpy as np
import pandas as pd
from scipy.interpolate import interp1d, interp2d, UnivariateSpline, griddata
from scipy.optimize import least_squares, curve_fit
from scipy.integrate import quad
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['Times New Roman + SimSun + WFM Sans SC']
plt.rcParams['mathtext.fontset']='stix'
plt.rcParams['axes.unicode_minus']=False   
plt.rcParams['figure.dpi'] = 200
plt.rcParams['xtick.direction']='in'
plt.rcParams['ytick.direction']='in'
t0 = np.linspace(700, 780, 5)
v0 = np.array([0.0977, 0.1218, 0.1406, 0.1551, 0.1664])
f1 = interp1d(t0, v0)
f2 = interp1d(t0, v0, 'cubic')
tp = np.array([750, 770])
vp1 = f1(tp)
vp2 = f2(tp)
print("线性插值的预测结果为:", vp1)
print("三次样条插值的预测结果为:", vp2)
t = np.linspace(700, 780, 200)
v1 = f1(t)
v2 = f2(t)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(t, v1, '--', label='线性插值')
ax.plot(t, v2, '-', label='三次样条插值', color='#ff9900')
ax.scatter(t0, v0, label='数据', zorder=10, marker='.', s=15, color='r')
ax.legend(fontsize=8)
fig.show()
print("3035")

习题7.4

import numpy as np
import pandas as pd
from scipy.interpolate import interp1d, interp2d, UnivariateSpline, griddata
from scipy.optimize import least_squares, curve_fit
from scipy.integrate import quad
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['Times New Roman + SimSun + WFM Sans SC']
plt.rcParams['mathtext.fontset']='stix'
plt.rcParams['axes.unicode_minus']=False   
plt.rcParams['figure.dpi'] = 200
plt.rcParams['xtick.direction']='in'
plt.rcParams['ytick.direction']='in'
np.random.seed(114514)

x0 = np.random.uniform(-3, 3, 50)
y0 = np.random.uniform(-4, 4, 50)
f = lambda x, y: (x**2 - 2*x)*np.exp(-x**2 - y**2 - x*y)
z0 = f(x0, y0)

xy0 = np.vstack([x0, y0]).T
x = np.linspace(x0.min(), x0.max(), 300)
y = np.linspace(y0.min(), y0.max(), 300)
X, Y = np.meshgrid(x, y)
z = griddata(xy0, z0, (X,Y), 'cubic')
zl = griddata(xy0, z0, (X,Y), 'linear')
z[np.isnan(z)] = zl[np.isnan(z)]

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, z, cmap='hot')
fig.show()
print("3035")

标签:plt,y0,第七章,rcParams,np,import,习题,例题,x0
From: https://www.cnblogs.com/miling/p/18552337

相关文章

  • 猫映射(Arnold变换),猫脸变换介绍与基于例题脚本的爆破
    前置信息http://www.jiamisoft.com/blog/index.php/7249-erzhituxiangjiamisuanfaarnold.htmlhttps://mp.weixin.qq.com/s/IbkAlyAPvbgMeNgqfwisTgArnold变换Arnold变换是V.J.Arnold在遍历理论的研究中提出的一种变换,原意为catmapping,俗称猫脸变换。Arnold变换直观、简单、具有......
  • Scala字符串练习题
    objectStringManipulationExamples{defmain(args:Array[String]):Unit={//问题1valfloatString="3.14"valfloatValue=floatString.toFloatvalbackToString=floatValue.toStringprintln(backToString)//问题2v......
  • C语言习题~day16
    1.关于函数调用说法不正确的是:()A.函数可以传值调用,传值调用的时候形参是实参的一份临时拷贝B.函数可以传址调用,传址调用的时候,可以通过形参操作实参C.函数可以嵌套定义,但是不能嵌套调用D.函数可以嵌套调用,但是不能嵌套定义答案解析:A:正确,形参按照值的方式传递,将来形参就......
  • 第五章习题
    学号后四位:30185.4:点击查看代码importcvxpyascpimportnumpyasnpimportpandasaspdfromscipy.optimizeimportminimizeimportsympyasspsp.init_printing(use_unicode=True)importmatplotlib.pyplotaspltplt.rcParams['font.sans-serif']=['Ti......
  • 第二章习题
    学号后四位:30182.1:点击查看代码importmathimportpylabaspltimportnumpyasnpplt.rc('text',usetex=True)#调用字库x=np.linspace(-10,10,100)y1=np.cosh(x)y2=np.sinh(x)y3=math.e**x/2plt.plot(x,y1,label='$\\mathrm{cosh}(x)$'......
  • python基础练习题----练手
    python—练手题—40题#01-helloworldprint('helloworld!')#如果3大于0,则打印'ok'和'yes'if3>0:print('ok')print('yes')x=3y=4print(x+y)#02-printprint('helloworld!')print(�......
  • HDLBIts习题(7):状态机
    (1)较难习题1:134题(fsm_ps2data)    有个积攒数据的过程。(1)较难习题2:135题(fsm_serial)        读清题意,有一个检验选择是否发送的进程。(3)较难习题3:137题(fsm_serialdp)    加入了奇偶校验位检测机制(4)较难习题4:138题(fsm_hdlc)   ......
  • HDLBIts习题(2):位操作,For循环(generate与integer)
    (1)冷门习题1:VerilogLanguage-MoreVerilogFeatures-Reductionoperators    一个矢量的位操作,多比特矢量操作会变得方便。(2)冷门习题2:VerilogLanguage-MoreVerilogFeatures-Combinationfor-loop:Vectorreversal2     Verilog中的for循环(3......
  • 习题7.7
    1.代码实现点击查看代码importnumpyasnpfromscipy.optimizeimportcurve_fit,least_squaresfromscipy.linalgimportlstsqimportmatplotlib.pyplotaspltdefg(x,a,b):return(10*a)/(10*b+(a-10*b)*np.exp(-a*np.sin(x)))#给定参数a......
  • 习题7.4
    1.代码实现点击查看代码importnumpyasnpfromscipy.interpolateimportgriddataimportmatplotlib.pyplotasplt#定义函数deff(x,y):return(x**2-2*x)*np.exp(-x**2-y**2-x*y)#生成随机点np.random.seed(0)x_random=np.random.uni......