You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python非线性复杂函数拟合问题:实部虚部拟合失效

复数模型同时拟合实部与虚部的curve_fit拟合失效问题

我需要用一个复数函数模型同时拟合实部和虚部数据,尝试了scipy的curve_fit但拟合结果和实际曲线严重不符,改用minimize()也没有得到有效结果。

以下是示例代码(使用模拟数据):

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

def long_function(fre, e_inf, e_s, alfa, beta, tau):
    return ((e_s-e_inf)*((1+1j*2*np.pi*fre*tau)**(1-alfa))**(-beta))+e_inf

def funcBoth(x, e_inf, e_s, alfa, beta, tau):
    N = len(x)
    x_real = x[:N//2]
    x_imag = x[N//2:]
    y_real = np.real(long_function(x_real, e_inf, e_s, alfa, beta, tau))
    y_imag = np.imag(long_function(x_imag, e_inf, e_s, alfa, beta, tau))
    return np.hstack([y_real, y_imag])

def plot_graph(poptBoth, fre,yReal,yImag):
    # Compute the best-fit solution
    yFit = long_function(fre, *poptBoth)
    print("alfa: {0:.2f}".format(poptBoth[2]))
    print("beta: {0:.2f}".format(poptBoth[3]))
    print("epsilon_infinita: {0:.2f}".format(poptBoth[0]))
    print("epsilon_s: {0:.2f}".format(poptBoth[1]))
    print("tau: ",poptBoth[4])

    # Plot the results
    plt.figure(figsize=(9, 4))

    plt.subplot(121)
    plt.plot(fre, np.real(yFit), label="Best fit")
    plt.plot(fre, np.real(yReal), "k.", label="Noisy y")
    plt.ylabel("Real part of y")
    plt.xlabel("x")
    plt.legend()

    plt.subplot(122)
    plt.plot(fre, np.imag(yFit), label="Best fit")
    plt.plot(fre, np.real(yImag), "k.", label="Noisy y")
    plt.ylabel("Real part of y")
    plt.xlabel("x")

    plt.tight_layout()
    plt.legend(loc='best')
    plt.show()

def curve_fitter(fre, yReal, yImag):
    yBoth = np.hstack([yReal, yImag])
    poptBoth, pcovBoth = curve_fit(funcBoth, np.hstack([fre, fre]), yBoth, maxfev=500000) #         method='lm' , p0=guess
    plot_graph(poptBoth,fre,yReal,yImag)


yReal =  [70.0, 68.0, 60.0, 50.0, 42.0, 38.0, 36.0, 35.4, 34.0, 33.0, 32.0, 30.0, 29.1, 28.8, 28.6, 28.4, 28.3, 28.2, 28.2, 28.1, 28.0]
yImag =  [17.0, 21.0, 22.5, 23.0, 22.5, 21.0, 19.0, 18.0, 17.3, 16.9, 16.4, 16.3, 16.2, 16.0, 15.7, 15.2, 14.8, 14.7, 14.7, 14.6, 14.5]
fre =  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
yReal = np.array(yReal)
yImag = np.array(yImag)
fre = np.array(fre)

curve_fitter(fre, yReal, yImag)

拟合结果显示,实部和虚部的拟合曲线均无法匹配原始数据点:
拟合结果

内容的提问来源于stack exchange,提问作者Alonso

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 18:15:50