python scipy.optimize curve_fit拟合非线性函数

# -*- coding: utf-8 -*-
"""
Created on Wed Dec  1 23:27:21 2021

@author: Machi
"""

import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
import seaborn as sns

sns.set(color_codes=True)
np.random.seed(20211201)

e = np.random.normal(0, 1, 30)
x = np.linspace(1, 5, 30)
y = 0.8*np.e**(1.1*x) + e

def model(x, a, b ):
    return a*np.e**(b*x)
popt, pcov = curve_fit(model, x, y)


print(popt)
xx = np.linspace(1, 5, 1000)
yy = model(xx, *popt)
plt.plot(x, y, 'o', xx, yy)
plt.title('Exponential Fit')
plt.show()

[0.76938063 1.10794215]
python scipy.optimize curve_fit拟合非线性函数_第1张图片

你可能感兴趣的:(python,开发语言,后端)