Reputation: 13
I intend to plot the eigenvalue(s) of the Jacobian matrix over the range -3 ≤ μ ≤ 3 in 0.1 steps in Python.
Please see my code below:
import numpy as np
mu = np.arange(-3,3,0.1)
J =([[0, 1],
[-1, -mu]])
assign lambda
lambda = np.linalg.eigvals(J)
print(lambda)
Then this showed up: "SyntaxError: invalid syntax," and it's pointing at "lambda" in the "assign lambda" line.
Then my code for plotting is
import matplotlib.pyplot as plt
plt.figure()
plt.plot(mu, lambda, 's')
plt.xlabel('mu')
plt.ylabel('Eigenvalue')
plt.show()
Another "SyntaxError: invalid syntax" showed up, pointing at the "lambda" in this section.
I've also tried to get rid of the "assign lambda" line but still didn't work. This kind of error has happened before when I try to define a function or parameter.
Could anyone please take a look and help me? I am very new to Python.
Thank you very much!
Upvotes: 1
Views: 328
Reputation: 39072
You are trying to vectorise mu
which is not the correct way in your code. You can simply use a for loop
import numpy as np
import matplotlib.pyplot as plt
mu = np.arange(-3,3,0.1)
lambda_list = []
for i in mu:
J = [[0, 1],[-1, -i]]
lambda_list.append(np.linalg.eigvals(J))
lambda_list = np.array(lambda_list)
plt.figure()
plt.plot(mu, lambda_list[:, 0], '-r', label='Eigenvalue 1')
plt.plot(mu, lambda_list[:, 1], '-b', label='Eigenvalue 2')
plt.xlabel('mu')
plt.ylabel('Eigenvalue')
plt.legend()
Upvotes: 1
Reputation: 16906
import numpy as np
import matplotlib.pyplot as plt
mu = np.arange(-3,3,0.1)
e = list()
for m in mu:
J = np.array([[0, 1], [-1, -m]])
e.append(np.linalg.eigvals(J))
plt.figure()
plt.plot(mu, e, 's')
plt.xlabel('mu')
plt.ylabel('Eigenvalue')
plt.show()
J
J
and save it in list e
e
with respect to mu
Upvotes: 0