尝试打印出一个矩阵 NameError: name 'Qb_matrix' is not defined

Xinrui Huang

我尝试使用如下代码打印出一个矩阵,但是,它显示了名称错误。我想知道我应该在哪里定义矩阵?python可以识别缩写为Qb到Q_bar吗?

import numpy as np

Q11 = 14.583
Q12 = 1.4583
Q23 = 0
Q22 = 3.646
Q33 = 4.2
theta = 60

def Q_bar(Q11, Q12, Q22, Q33, theta):


n = np.sin(theta*np.pi/180)
m = np.cos(theta*np.pi/180)
Qb_11 = Q11*m**4 + 2*(Q12 + 2*Q33)*n**2*m**2 + Q22*n**4
Qb_22 = Q11*n**4 + 2*(Q12 + 2*Q33)*n**2*m**2 + Q22*m**4
Qb_33 = (Q11 + Q22 - 2*Q12 - 2*Q33)*n**2*m**2 + Q33*(m**4 + n**4)
Qb_12 = (Q11 + Q22 - 4*Q33)*n**2*m**2 + Q12*(m**4 + n**4)
Qb_13 = (Q11 - Q12 - 2*Q33)*n*m**3 + (Q12 - Q22 + 2*Q33)*n**3*m
Qb_23 = (Q11 - Q12 - 2*Q33)*n**3*m + (Q12 - Q22 + 2*Q33)*n*m**3
Qb_matrix = np.array([[Qb_11, Qb_12, Qb_13],[Qb_12, Qb_22, Qb_23],[Qb_13, Qb_23, Qb_33]])
return(Qb_matrix)

print(Qb_matrix)
克里斯·道尔

你永远不会调用你的函数,所以它里面的代码永远不会被执行。更进一步,即使您确实调用了该函数。您在函数中创建的变量 Qb_matrix 将仅存在于函数作用域内,当您返回它时,您需要存储该返回值。

import numpy as np

Q11 = 14.583
Q12 = 1.4583
Q23 = 0
Q22 = 3.646
Q33 = 4.2
theta = 60

def Q_bar(Q11, Q12, Q22, Q33, theta):
    n = np.sin(theta*np.pi/180)
    m = np.cos(theta*np.pi/180)
    Qb_11 = Q11*m**4 + 2*(Q12 + 2*Q33)*n**2*m**2 + Q22*n**4
    Qb_22 = Q11*n**4 + 2*(Q12 + 2*Q33)*n**2*m**2 + Q22*m**4
    Qb_33 = (Q11 + Q22 - 2*Q12 - 2*Q33)*n**2*m**2 + Q33*(m**4 + n**4)
    Qb_12 = (Q11 + Q22 - 4*Q33)*n**2*m**2 + Q12*(m**4 + n**4)
    Qb_13 = (Q11 - Q12 - 2*Q33)*n*m**3 + (Q12 - Q22 + 2*Q33)*n**3*m
    Qb_23 = (Q11 - Q12 - 2*Q33)*n**3*m + (Q12 - Q22 + 2*Q33)*n*m**3
    Qb_matrix = np.array([[Qb_11, Qb_12, Qb_13],[Qb_12, Qb_22, Qb_23],[Qb_13, Qb_23, Qb_33]])
    return(Qb_matrix)

my_qb_matrix = Q_bar(Q11, Q12, Q22, Q33, theta)
print(my_qb_matrix)

输出

[[ 6.659175    1.179375    2.52896738]
 [ 1.179375   12.127675    2.20689254]
 [ 2.52896738  2.20689254  3.921075  ]]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我有一个错误:NameError: name 'GameDisplay' is not defined

NameError: name '' is not defined - 将函数返回作为输入传递给另一个函数

导入 kafka 给出“NameError: name 'true' is not defined”

NameError: ("name 'true' is not defined", 'occurred at index 0')

如何修复“NameError: name 'context' is not defined”

如何修复“NameError: name 'color' is not defined”?

如何解决 NameError: name 'xx' is not defined?

iPython调试器引发`NameError:name ... not defined`

不明白这个“NameError: name 'self' is not defined”错误

在类中导入模块时出现“NameError: name [module] is not defined”

“NameError: name 'update' is not defined” 使用数据表时出错

如何解决错误 NameError: name 'SparkConf' is not defined in pycharm

Scrapy:在scrapy 中使用SQLAlchemy 有“NameError: name 'connection' is not defined”

为什么我会收到“NameError: name 'self' is not defined”?

错误“NameError: name 'self' is not defined”即使我声明了“self”

raise self._value NameError: name 'global_df' is not defined

无法在 18.04 中添加 PPA 密钥:NameError: name 'KUrl' is not defined

试图从父类继承变量“NameError: name 'r' is not defined”

如何正确配置python 3的使用避免NameError: name '' is not defined

Django连接mysql问题,NameError: name '_mysql' is not defined

在 Django 中使用类型提示 Any - NameError: name 'Any' is not defined

为什么python在定义时返回“NameError:name 'correct' is not defined?

NameError: name 'i' is not defined - 为什么会发生这种情况?

当我在请求输入时尝试中断 while 循环时,如何克服“NameError: name 'x' is not defined”?

Pandas Dataframe NameError:我可以打印数据帧,但是当我尝试聚合列时,我得到 name '' is not defined 错误

我正在将一个模块导入另一个模块以在 tkinter 中创建树视图,但它向我显示了一个错误 NameError: name 'treeview' is not defined

如何在 python 中关于海面温度的图中修复“NameError: name 'cartopy' is not defined'?

为什么此重新加载失败并显示“NameError: name <xxx> is not defined”?

为什么会出现 NameError 的错误:name 'file' is not defined in python 3