python中的方程如何建立和求解?

How to set up and solve for equations in python?

我正在寻找 运行 这个代码,它可以通过绘制图形来解决未知数 c_10c_01

方程式的一些背景是使用 Mooney-Rivlin 模型 (1940) 和 c_10[(2*λ+λ**2)-3]+c_01[(λ**-2+2*λ)-3]

一些未知数 I_1I_2 在下面的代码中定义。 P1(或称为 P)和 lambda 是在下面的 table(sheet ExperimentData of experimental_data1.xlsx)中以数字形式预定义的数据:

λ       P
1.00    0.00
1.01    0.03
1.12    0.14
1.24    0.23
1.39    0.32
1.61    0.41
1.89    0.50
2.17    0.58
2.42    0.67
3.01    0.85
3.58    1.04
4.03    1.21
4.76    1.58
5.36    1.94
5.76    2.29
6.16    2.67
6.40    3.02
6.62    3.39
6.87    3.75
7.05    4.12
7.16    4.47
7.27    4.85
7.43    5.21
7.50    5.57
7.61    6.30

代码如下:

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

from scipy.optimize import differential_evolution

# Generic Mooney-Rivlin Equation
# bounds on parameters are set in
# generate_Initial_Parameters() below
def generic_equation(c_10, c_01, λ, P):
    P = c_10[(2 * λ + λ ** 2) - 3] + c_01[(λ ** -2 + 2 * λ) - 3]
    return (P)

# function for genetic algorithm to minimize (sum of squared error)
# bounds on parameters are set in generate_Initial_Parameters() below
def sumOfSquaredError(parameterTuple):
    warnings.filterwarnings("ignore") # do not print warnings by genetic algorithm
    return np.sum((yData - generic_equation(xData, *parameterTuple)) ** 2)

def generate_Initial_Parameters():
    # min and max used for bounds
    maxX = max(xData)
    minX = min(xData)
    maxY = max(yData)
    minY = min(yData)

    parameterBounds = []
    parameterBounds.append([0.0, 1.0*1000000.0]) # parameter bounds for c_10
    parameterBounds.append([0.0, 1.0*1000000.0]) # parameter bounds for c_01
    parameterBounds.append([minX, maxX]) # parameter bounds for λ
    parameterBounds.append([minY, maxY]) # parameter bounds for P

# "seed" the numpy random number generator for repeatable results
result = differential_evolution(sumOfSquaredError, parameterBounds, seed=3)
return result.x


# load the test data from Experimental dataset
data = pd.read_excel('experimental_data1.xlsx', sheet_name='ExperimentData')
xData = data['λ'].values
yData = data['P'].values


# generate initial parameter values
initialParameters = generate_Initial_Parameters()

# curve fit the test data
fittedParameters, pcov = curve_fit(generic_equation, xData, yData, initialParameters)

# create values for display of fitted peak function
c_10, c_01, λ, P = fittedParameters
y_fit = generic_equation(xData, c_10, c_01, λ, P)

plt.plot(xData, yData) # plot the raw data
plt.plot(xData, y_fit) # plot the equation using the fitted parameters
plt.show()

print(fittedParameters)

当我运行它时,产生了这个错误:

RuntimeError: The map-like callable must be of the form f(func, iterable), returning a sequence of numbers the same length as 'iterable'

这是从 here 中找到的原始代码。

看起来您只是在尝试使用线性回归来找到 c_10c_01 的最佳值,以最适合您的 λP 数据。

所以我建议使用LinearRegression from scikit-learn

我将您的 λP 数据复制到一个名为 'curvefitdata.csv'.

的 csv 文件中
import pandas as pd
from sklearn.linear_model import LinearRegression

df = pd.read_csv('curvefitdata.csv', delim_whitespace=True)
y = df['P']
X = [[(2*λ+λ**2)-3, (λ**-2+2*λ)-3] for λ in df['λ']]

reg = LinearRegression(fit_intercept=False).fit(X, y)
c_10, c_01 = reg.coef_

print(c_10, c_01)
# 0.16781162162572094 -0.5190608351956174

测试:

import matplotlib.pyplot as plt

λ = df['λ']
z = reg.predict(X)
plt.plot(λ, y, label='data')
plt.plot(λ, z, label='best fit')
plt.legend(loc='best')
plt.xlabel('λ')
plt.ylabel('P')
plt.show()