S Ringne S Ringne -4 years ago 165
Python Question

Plotting mathematical function in python

i'm trying to write function called plotting which takes i/p parameters Z, p and q and plots the function

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


(Note: I is the identity matrix.) det() is the determinant.

For finding det(),
numpy.linalg.det()
can be used
and for indentity matrix ,
np.matlib.identity(n)


Is there a way to write such functions in python? and plot them?

import numpy as np

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


Is what i am tryin correct? any alternative?

Answer Source

You could use the following implementation.

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()

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download