【问题标题】:Plotting mathematical function in python在python中绘制数学函数
【发布时间】:2017-03-24 06:41:25
【问题描述】:

我正在尝试编写名为 plotting 的函数,它采用 i/p 参数 Z、p 和 q 并绘制函数

f(y) = det(Z − yI) on the interval [p, q]

(注意:I 是单位矩阵。)det() 是行列式。

为了找到det(),可以使用numpy.linalg.det() 并且对于标识矩阵,np.matlib.identity(n)

有没有办法在python中编写这样的函数?并绘制它们?

import numpy as np

def f(y):
    I2 = np.matlib.identity(y)
    x = Z-yI2
    numpy.linalg.det(x)
    ....

我正在尝试的内容是否正确?有什么选择吗?

【问题讨论】:

  • 那么,变量Zpqy 取什么值?
  • @kmario23 任何整数值,可以指定为变量。例如,1,2,3...

标签: python python-2.7 python-3.x numpy


【解决方案1】:

您可以使用以下实现。

import numpy as np
import matplotlib.pyplot as plt

def f(y, Z):  
    n, m = Z.shape
    assert(n==m)
    I = np.identity(n)
    x = Z-y*I
    return np.linalg.det(x)

Z = np.matrix('1 2; 3 4')
p = -15
q = 15
y = np.linspace(p, q)
w = np.zeros(y.shape)

for i in range(len(y)):
    w[i] = f(y[i], Z)

plt.plot(y, w)
plt.show()

【讨论】:

    猜你喜欢
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 2015-10-16
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 1970-01-01
    • 2011-06-23
    相关资源
    最近更新 更多