首页 > 其他分享 >7.7

7.7

时间:2024-11-18 17:39:58浏览次数:1  
标签:leastsq plt fit least values 7.7 squares

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit, leastsq, least_squares
from scipy.constants import e

def g(x, a, b):
return (10 * a) / (10 * b + (a - 10 * b) * np.exp(a * np.sin(x)))

a = 1.1
b = 0.01

x_values = np.arange(1, 21)
y_values = g(x_values, a, b)

for i, (xi, yi) in enumerate(zip(x_values, y_values), start=1):
print(f"({xi}, {yi:.6f})")

popt_curve_fit, pcov_curve_fit = curve_fit(g, x_values, y_values, p0=[a, b])
y_fit_curve_fit = g(x_values, *popt_curve_fit)

def func_leastsq(params, x, y):
return y - g(x, *params)

popt_leastsq = leastsq(func_leastsq, [a, b], args=(x_values, y_values))[0]
y_fit_leastsq = g(x_values, *popt_leastsq)

popt_least_squares = least_squares(func_leastsq, [a, b], args=(x_values, y_values)).x
y_fit_least_squares = g(x_values, *popt_least_squares)

print("\ncurve_fit parameters:", popt_curve_fit)
print("leastsq parameters:", popt_leastsq)
print("least_squares parameters:", popt_least_squares)

plt.figure(figsize=(10, 6))
plt.scatter(x_values, y_values, label='Simulated data', color='red')
plt.plot(x_values, y_fit_curve_fit, label='curve_fit', linestyle='-')
plt.plot(x_values, y_fit_leastsq, label='leastsq', linestyle='--')
plt.plot(x_values, y_fit_least_squares, label='least_squares', linestyle='-.')
plt.xlabel('x')
plt.ylabel('g(x)')
plt.legend()
plt.title('Fitting of g(x) using curve_fit, leastsq, and least_squares')
plt.grid(True)
plt.show()

2023310143007

标签:leastsq,plt,fit,least,values,7.7,squares
From: https://www.cnblogs.com/zzzzddddd/p/18553284

相关文章

  • 7.1 7.3 7.4 7.7 7.10
    7.1点击查看代码importnumpyasnpimportscipy.interpolateasspiimportscipy.integrateasspi_integratedefg(x):return((3*x**2+4*x+6)*np.sin(x))/(x**2+8*x+6)x_values=np.linspace(0,10,1000)y_values=g(x_values)spline=spi.C......
  • 习题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.7
    `importnumpyasnpimportmatplotlib.pyplotaspltfromscipy.optimizeimportcurve_fit,leastsq,least_squares定义函数g(x,a,b)defg(x,a,b):return(10*a)/(10*b+(a-10*b)*np.exp(a*np.sin(x)))初始参数a=1.1b=0.01生成数据x_values......
  • 习题7.7
    importnumpyasnpimportmatplotlib.pyplotaspltfromscipy.optimizeimportcurve_fit,leastsq,least_squaresfromscipy.constantsimportedefg(x,a,b):return(10*a)/(10*b+(a-10*b)*np.exp(a*np.sin(x)))a=1.1b=0.01x_value......
  • 7.7
    importnumpyasnpfromscipy.optimizeimportcurve_fit,leastsq,least_squaresimportmatplotlib.pyplotaspltdefg(x,a,b):return10*a/(10*b)+(a-10*b)*np.exp(-a*np.sin(x))x=np.arange(1,21)a=1.1b=0.01y=g(x,a,b)defg_fit......
  • centos7.7安装php7.3的lnmp环境和composer详细步骤
    1.更新源yumupdate2.安装nginxyuminstallnginx3.启动nginxservicenginxstart4.访问http://你的ip如果想配置域名,见最下面。5.安装mysql:安装mysql源yumlocalinstallhttp://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm安装:yuminst......
  • 7.7 g(x)=(10a)/(10b+(a-10b)e^(asinx)),取a=1.1,b=0.01,计算x=1,2,...,20时,g(x)对应的函
    importnumpyasnpimportmatplotlib.pyplotaspltfromscipy.optimizeimportcurve_fit,leastsq,least_squaresfromscipy.constantsimportedefg(x,a,b):return(10*a)/(10*b+(a-10*b)*np.exp(a*np.sin(x)))a=1.1b=0.01x_values=np.......
  • 高等数学 7.7常系数齐次线性微分方程
    在二阶齐次线性微分方程\[y''+P(x)y'+Q(x)y=0\tag{1}\]中,如果\(y',y\)的系数\(P(x),Q(x)\)均为常数,即\((1)\)式成为\[y''+py'+qy=0\tag{2}\]其中\(p,q\)是常数,那么称\((2)\)为二阶常系数齐次线性微分方程。如果\(p,q\)不全为常数,就称\((1......
  • Project ‘org.springframework.boot:spring-boot-starter-parent:2.7.7’ not found
    原文链接:Project‘org.springframework.boot:spring-boot-starter-parent:2.7.7’notfound–每天进步一点点(longkui.site)某日构建springboot项目,构建完毕以后发现下面这样然后打开pom文件,发现springboot的依赖爆红(这个版本号是随便举例)我去本地仓库看了看,有这个依赖,......
  • MathType7.7纯净和谐绿色版安装包下载
    在现代教育中,数学是一门基础而重要的学科。无论是中小学的基础教育,还是大学及科研机构中的高级研究,都离不开数学的应用和探索。随着科技的发展,电子文档和在线学习平台逐渐普及,如何高效地编辑和展示数学公式成为了一个亟待解决的问题。MathType数学公式编辑器正是在这样的背景下......