首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python:使用共享参数拟合矩阵方程

Python:使用共享参数拟合矩阵方程
EN

Stack Overflow用户
提问于 2018-06-20 01:21:37
回答 1查看 214关注 0票数 1

我有两个源于一个矩阵方程的方程:

[x,y] = [[cos(n), -sin(n)],[sin(n), cos(n)]]*[x', y']

在那里x' = Acos(w1*t+ p1)y' = Bcos(w2*t + p2)

这只是向量[x,y]的一个矩阵方程,但它可以分解为两个标量方程:x = A*cos(s)*cos(w1*t+ p1)*x' - B*sin(s)*sin(w2*t + p2)*y'y = A*sin(s)*cos(w1*t+ p1)*x' + B*cos(s)*sin(w2*t + p2)*y'

所以我拟合了两个数据集,xtyt,但在这些拟合中有一些共同的参数,即ABs

1)我可以直接拟合矩阵方程,还是必须将其分解为标量方程?前者会更优雅。

2)我可以在curve_fit上使用共享参数吗?所有相关问题都使用其他包。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-20 05:15:13

下面的示例代码使用curve_fit使用一个共享参数来拟合两个不同的方程。这不是对你问题的回答,但是我不能在评论中格式化代码,所以我把它贴在这里。

代码语言:javascript
复制
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

y1 = np.array([ 16.00,  18.42,  20.84,  23.26,  25.68])
y2 = np.array([-20.00, -25.50, -31.00, -36.50, -42.00])
comboY = np.append(y1, y2)

h = np.array([5.0, 6.1, 7.2, 8.3, 9.4])
comboX = np.append(h, h)


def mod1(data, a, b, c): # not all parameters are used here
        return a * data + c


def mod2(data, a, b, c): # not all parameters are used here
        return b * data + c


def comboFunc(comboData, a, b, c):
    # single data set passed in, extract separate data
    extract1 = comboData[:len(y1)] # first data
    extract2 = comboData[len(y2):] # second data

    result1 = mod1(extract1, a, b, c)
    result2 = mod2(extract2, a, b, c)

    return np.append(result1, result2)


# some initial parameter values
initialParameters = np.array([1.0, 1.0, 1.0])

# curve fit the combined data to the combined function
fittedParameters, pcov = curve_fit(comboFunc, comboX, comboY, initialParameters)

# values for display of fitted function
a, b, c = fittedParameters

y_fit_1 = mod1(h, a, b, c) # first data set, first equation
y_fit_2 = mod2(h, a, b, c) # second data set, second equation

plt.plot(comboX, comboY, 'D') # plot the raw data
plt.plot(h, y_fit_1) # plot the equation using the fitted parameters
plt.plot(h, y_fit_2) # plot the equation using the fitted parameters
plt.show()

print(fittedParameters)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50933819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档