如何在Python 2.7中同时优化和找到两个方程的系数? [英] How do I optimize and find the coefficients for two equations simultaneously in Python 2.7?

查看:96
本文介绍了如何在Python 2.7中同时优化和找到两个方程的系数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有适合两个方程的数据集:

I have data sets that I would like to fit to two equations:

y1 = a1 + a2 * T / 2 + a3 * T ^ 2/3 + a4 * T ^ 3/4 + a5 * T ^ 4/5 + a6 / T

y2 = a1 * lnT + a2 * T + a3 * T ^ 2/2 + a4 * T ^ 3 / 3 + a5 * T ^ 4/4 + a7

y1 = a1 + a2 * T / 2 + a3 * T^2 / 3 + a4 * T^3 / 4 + a5 * T^4 / 5 + a6 / T
y2 = a1 * lnT + a2 * T + a3 * T^2 / 2 + a4 * T^3 / 3 + a5 * T^4 / 4 + a7

两个多项式共享一些参数(从a1到a5),所以我想同时拟合这两个方程。

The two polynomials share some parameters (a1 through a5) so I would like to fit these two equations simultaneously.

我尝试使用scipy.optimize.curve_fit:

I tried to do it with scipy.optimize.curve_fit:

import numpy as np
from scipy.optimize import curve_fit

def func(T, a1, a2, a3, a4, a5, a6, a7):
    y1 = a1 + a2 * T / 2 + a3 * T**2 / 3 + a4 * T**3 / 4 + a5 * T**4/5 + a6/T
    y2 = a1*np.log(T) + a2*T + a3 * T**2/2 + a4 * T**3/4 + a5 * T**4/4 + a7
    return np.stack((y1, y2), axis = 1)

T = np.linspace(300, 1000, 20)
ydata_1 = np.array([
    0.02139265,  0.40022353,  0.70653103,  0.95896469,  1.17025634,
    1.34944655,  1.50316659,  1.63641239,  1.75303086,  1.85603601,
    1.94782051,  2.03030092,  2.10501971,  2.17321829,  2.23589026,
    2.29382086,  2.34761661,  2.39772787,  2.44446625,  2.48801814])

ydata_2 = np.array([
    15.73868267,  16.14232408,  16.50633034,  16.83724622,
    17.14016153,  17.41914701,  17.67752993,  17.91807535,
    18.14310926,  18.35460465,  18.55424316,  18.74346017,
    18.92347836,  19.09533317,  19.25989235,  19.41787118,
    19.56984452,  19.71625632,  19.85742738,  19.99356154])

ydata = np.stack((ydata_1, ydata_2), axis = 1)
popt, pconv = curve_fit(f = func, xdata = T, ydata = ydata)

但是我得到了错误:

minpack.error: Result from function call is not a proper array of floats.

我什至不确定这是否是解决问题的正确方法。

I am not even sure if this is the right approach to solve the problem.

推荐答案

您可以尝试在二维空间中将y值的L_2范数最小化(即最小二乘拟合):

You can try minimising the L_2 norm (i.e. least squares fitting) in a 2 dimensional space for your y-values:

from scipy.optimize import minimize

def func(params):
    a1, a2, a3, a4, a5, a6, a7 = params
    y1 = a1 + a2 * T / 2 + a3 * T**2 / 3 + a4 * T**3 / 4 + a5 * T**4/5 + a6/T
    y2 = a1*np.log(T) + a2*T + a3 * T**2/2 + a4 * T**3/4 + a5 * T**4/4 + a7
    return np.sum((y1 - ydata_1) ** 2 + (y2 - ydata_2) ** 2)

T = np.linspace(300, 1000, 20)
ydata_1 = np.array([
    0.02139265,  0.40022353,  0.70653103,  0.95896469,  1.17025634,
    1.34944655,  1.50316659,  1.63641239,  1.75303086,  1.85603601,
    1.94782051,  2.03030092,  2.10501971,  2.17321829,  2.23589026,
    2.29382086,  2.34761661,  2.39772787,  2.44446625,  2.48801814])

ydata_2 = np.array([
    15.73868267,  16.14232408,  16.50633034,  16.83724622,
    17.14016153,  17.41914701,  17.67752993,  17.91807535,
    18.14310926,  18.35460465,  18.55424316,  18.74346017,
    18.92347836,  19.09533317,  19.25989235,  19.41787118,
    19.56984452,  19.71625632,  19.85742738,  19.99356154])

# choose reasonable values for your 7 parameters here,
# i.e. close to the "right" answer, this may take a few tries
first_guess = [a1_0, a2_0, a3_0, a4_0, a5_0, a6_0, a7_0]  

# here we run the minimisation
res = minimize(func, first_guess)

# this is an array of your best fit values for a1-a7
best_fit = res.x

但是,@ Stelios似乎是对的,因为您很难适应特定型号。

However, it seems that @Stelios is right in that you will have a hard time getting a good fit with your particular model.

这篇关于如何在Python 2.7中同时优化和找到两个方程的系数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆