【发布时间】:2021-12-12 07:40:13
【问题描述】:
我正在尝试求解 T(p,q) 的方程,如附图中所示。
地点:
- p = 0.60
- q = 0.45
- M - 3行6列的系数矩阵
我创建了五个矩阵并将它们放在各自的函数中,以便稍后在 while 循环中调用它们。但是,循环不会循环遍历 i 的各种值。
我怎样才能使循环工作,或者有其他/更好的方法可以解决以下等式吗?
(仅供参考,这大约是我使用 Python 和编码的第三天)
import numpy as np
def M1(i):
M = np.array([[1,3,0,4,0,0],[3,3,1,4,0,0],[4,4,0,3,1,0]])
return M[i-1,0]
def M2(i):
M = np.array([[1,3,0,4,0,0],[3,3,1,4,0,0],[4,4,0,3,1,0]])
return M[i-1,1]
def M3(i):
M = np.array([[1,3,0,4,0,0],[3,3,1,4,0,0],[4,4,0,3,1,0]])
return M[i-1,2]
def M4(i):
M = np.array([[1,3,0,4,0,0],[3,3,1,4,0,0],[4,4,0,3,1,0]])
return M[i-1,3]
def M5(i):
M = np.array([[1,3,0,4,0,0],[3,3,1,4,0,0],[4,4,0,3,1,0]])
return M[i-1,4]
def T(p,q):
sum_i = 0
i = 1
while i <=5:
sum_i = sum_i + ((M1(i)*p**M2(i))*((1-p)**M3(i))*(q**M4(i))*((1-q)**M5(i)))
i = i +1
return sum_i
print(T(0.6,0.45))
"""I printed the below equation (using a single value for i) to test if the above loop is working and since I get the same answer as the loop, I can see that the loop is not cycling through the various values of i as expected"""
i=1
p=0.6
q=0.45
print(((M1(i)*p**M2(i))*((1-p)**M3(i))*(q**M4(i))*((1-q)**M5(i))))
【问题讨论】:
-
对不起,我很迂腐,但在我看来,您并没有“解决”一个方程。您正在“计算”或“计算”一个表达式。
-
我是来学习的,感谢您指出这一点。
标签: python arrays loops matrix equation