Shubham R
Shubham R

Reputation: 7644

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?

Upvotes: 0

Views: 350

Answers (1)

eyllanesc
eyllanesc

Reputation: 243897

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

Upvotes: 2

Related Questions