import numpy as np import matplotlib. pyplot as plt from scipy. integrate import odeint # Define the system of differential equations def system(y, t): [z,p] = y dzdt= 2 * z - z* *2- z*p dpdt = p**2 p * (z**2) return [dzdt, dpdt] # Function to calculate the Jacobian matrix at an equilibrium point def jacobian_matrix (point): z, p = point return np.array ( [ [2 - 2+ z-p, -z], [-2 pz, 2 p - z**2]]) # Equilibrium points equilibrium_points = [(0, 0), (1, 1), (2, 0), (-2, 4)] # Set up the plot fig, axs plt.subplots (2, 2, figsize=(10, 8)) fig.suptitle('Phase Portraits for Linearized Solutions') # Plot linearized solutions at each equilibrium point for i, point in enumerate(equilibrium_points): # Calculate the Jacobian matrix at the equilibrium point A = jacobian_matrix (point) # Eigenvalues and eigenvectors eigenvalues, eigenvectors = np. linalg.eig (A) # Initial conditions for the linearized system ye = np.concatenate ([point, eigenvectors[:, 0]]) # Time points t = np. linspace(0, 5, 100) # Solve the linearized system linearized_solution = odeint (system, ye, t) #Plot the phase portrait axs[i // 2, 1 % 2].plot(linearized_solution[:, 0], linearized_solution[:, 1]) axs [i // 2, 1 % 2].set_title(f'Equilibrium Point: {point}') #Set common labels for ax in axs.flat: ax.set (xlabel='z', ylabel='p') # Adjust layout plt.tight_layout(rect= [0, 0.03, 1, 0.95]) # Show the plot plt.show()

Linear Algebra: A Modern Introduction
4th Edition
ISBN:9781285463247
Author:David Poole
Publisher:David Poole
Chapter4: Eigenvalues And Eigenvectors
Section4.6: Applications And The Perron-frobenius Theorem
Problem 70EQ
icon
Related questions
Question

what is wrong with the python code? Trying to plot phase portraits for the linearized systems for each equilibrium point of a non-linear system of differential equations, but command line 7 defining the system -- [z,p]=y --- gives me the error: too many values to unpack (expected 2) 

import numpy as np
import matplotlib. pyplot as plt
from scipy. integrate import odeint
# Define the system of differential equations
def system(y, t):
[z,p] = y
dzdt= 2 * z - z* *2- z*p
dpdt = p**2 p * (z**2)
return [dzdt, dpdt]
# Function to calculate the Jacobian matrix at an equilibrium point
def jacobian_matrix (point):
z, p = point
return np.array ( [ [2 - 2+ z-p, -z], [-2 pz, 2 p - z**2]])
# Equilibrium points
equilibrium_points = [(0, 0), (1, 1), (2, 0), (-2, 4)]
# Set up the plot
fig, axs
plt.subplots (2, 2, figsize=(10, 8))
fig.suptitle('Phase Portraits for Linearized Solutions')
# Plot linearized solutions at each equilibrium point
for i, point in enumerate(equilibrium_points):
# Calculate the Jacobian matrix at the equilibrium point
A = jacobian_matrix (point)
# Eigenvalues and eigenvectors
eigenvalues, eigenvectors = np. linalg.eig (A)
# Initial conditions for the linearized system
ye = np.concatenate ([point, eigenvectors[:, 0]])
# Time points
t = np. linspace(0, 5, 100)
# Solve the linearized system
linearized_solution = odeint (system, ye, t)
#Plot the phase portrait
axs[i // 2, 1 % 2].plot(linearized_solution[:, 0], linearized_solution[:, 1])
axs [i // 2, 1 % 2].set_title(f'Equilibrium Point: {point}')
#Set common labels
for ax in axs.flat:
ax.set (xlabel='z', ylabel='p')
# Adjust layout
plt.tight_layout(rect= [0, 0.03, 1, 0.95])
# Show the plot
plt.show()
Transcribed Image Text:import numpy as np import matplotlib. pyplot as plt from scipy. integrate import odeint # Define the system of differential equations def system(y, t): [z,p] = y dzdt= 2 * z - z* *2- z*p dpdt = p**2 p * (z**2) return [dzdt, dpdt] # Function to calculate the Jacobian matrix at an equilibrium point def jacobian_matrix (point): z, p = point return np.array ( [ [2 - 2+ z-p, -z], [-2 pz, 2 p - z**2]]) # Equilibrium points equilibrium_points = [(0, 0), (1, 1), (2, 0), (-2, 4)] # Set up the plot fig, axs plt.subplots (2, 2, figsize=(10, 8)) fig.suptitle('Phase Portraits for Linearized Solutions') # Plot linearized solutions at each equilibrium point for i, point in enumerate(equilibrium_points): # Calculate the Jacobian matrix at the equilibrium point A = jacobian_matrix (point) # Eigenvalues and eigenvectors eigenvalues, eigenvectors = np. linalg.eig (A) # Initial conditions for the linearized system ye = np.concatenate ([point, eigenvectors[:, 0]]) # Time points t = np. linspace(0, 5, 100) # Solve the linearized system linearized_solution = odeint (system, ye, t) #Plot the phase portrait axs[i // 2, 1 % 2].plot(linearized_solution[:, 0], linearized_solution[:, 1]) axs [i // 2, 1 % 2].set_title(f'Equilibrium Point: {point}') #Set common labels for ax in axs.flat: ax.set (xlabel='z', ylabel='p') # Adjust layout plt.tight_layout(rect= [0, 0.03, 1, 0.95]) # Show the plot plt.show()
AI-Generated Solution
AI-generated content may present inaccurate or offensive content that does not represent bartleby’s views.
steps

Unlock instant AI solutions

Tap the button
to generate a solution

Recommended textbooks for you
Linear Algebra: A Modern Introduction
Linear Algebra: A Modern Introduction
Algebra
ISBN:
9781285463247
Author:
David Poole
Publisher:
Cengage Learning
Algebra & Trigonometry with Analytic Geometry
Algebra & Trigonometry with Analytic Geometry
Algebra
ISBN:
9781133382119
Author:
Swokowski
Publisher:
Cengage