what is wrong with my python code? I am trying to write a code to plot the direction field/phase portrait for a system of linear differential equations. The solution passes through (-2,-2) and the equations are x_1'=2x_1 - x_2 and x_2'=-x_1 + x_2

Advanced Engineering Mathematics
10th Edition
ISBN:9780470458365
Author:Erwin Kreyszig
Publisher:Erwin Kreyszig
Chapter2: Second-order Linear Odes
Section: Chapter Questions
Problem 1RQ
icon
Related questions
Question
100%

what is wrong with my python code? I am trying to write a code to plot the direction field/phase portrait for a system of linear differential equations. The solution passes through (-2,-2) and the equations are x_1'=2x_1 - x_2 and x_2'=-x_1 + x_2

 

```python
import matplotlib.pyplot as plt
from scipy.integrate import odeint
from numpy import linalg as LA
import numpy as np

a=2
b=-1
c=-1
d=1

## Vector field function
def vf(X, t):
    x_1prime = a*X[0] + b*X[1]
    x_2prime = c*X[0] + d*X[1]
    return [x_1prime, x_2prime]

## Solution curves
t0 = 0; tEnd = 10
t = np.linspace(t0, tEnd, 50*(tEnd-t0))

x_1_0 = [0, -2] # First initial condition
x_1 = odeint(vf, x_1_0, t)

x_2_0 = [-0.2] # Second initial condition
x_2 = odeint(vf, x_2_0, t)

## Generate the eigenvalues
B = np.array([[a, b], [c, d]])
u, v = LA.eig(B)
print('eigenvalues and eigenvectors')
print(u)
print(v)
```

### Explanation

This code snippet is written in Python and uses various libraries to integrate and analyze linear differential equations. Here's a breakdown of the elements:

1. **Imports:**
   - `matplotlib.pyplot` as `plt`: Used for plotting graphs (though not used in the provided code portion).
   - `scipy.integrate.odeint`: Used for integrating ordinary differential equations.
   - `numpy.linalg` as `LA`: Provides functions for linear algebra operations like finding eigenvalues.
   - `numpy`: A library for numerical operations in Python.

2. **Parameters:**
   - Coefficients `a`, `b`, `c`, and `d` are defined, which will be used in the vector field function to define a system of differential equations.

3. **Vector Field Function:**
   - `vf(X, t)`: Defines a vector field function for a system where:
     - \( x'_1 = a \cdot X[0] + b \cdot X[1] \)
     - \( x'_2 = c \cdot X[0] + d \cdot X[1] \)
   This returns these derivatives as a list.

4
Transcribed Image Text:```python import matplotlib.pyplot as plt from scipy.integrate import odeint from numpy import linalg as LA import numpy as np a=2 b=-1 c=-1 d=1 ## Vector field function def vf(X, t): x_1prime = a*X[0] + b*X[1] x_2prime = c*X[0] + d*X[1] return [x_1prime, x_2prime] ## Solution curves t0 = 0; tEnd = 10 t = np.linspace(t0, tEnd, 50*(tEnd-t0)) x_1_0 = [0, -2] # First initial condition x_1 = odeint(vf, x_1_0, t) x_2_0 = [-0.2] # Second initial condition x_2 = odeint(vf, x_2_0, t) ## Generate the eigenvalues B = np.array([[a, b], [c, d]]) u, v = LA.eig(B) print('eigenvalues and eigenvectors') print(u) print(v) ``` ### Explanation This code snippet is written in Python and uses various libraries to integrate and analyze linear differential equations. Here's a breakdown of the elements: 1. **Imports:** - `matplotlib.pyplot` as `plt`: Used for plotting graphs (though not used in the provided code portion). - `scipy.integrate.odeint`: Used for integrating ordinary differential equations. - `numpy.linalg` as `LA`: Provides functions for linear algebra operations like finding eigenvalues. - `numpy`: A library for numerical operations in Python. 2. **Parameters:** - Coefficients `a`, `b`, `c`, and `d` are defined, which will be used in the vector field function to define a system of differential equations. 3. **Vector Field Function:** - `vf(X, t)`: Defines a vector field function for a system where: - \( x'_1 = a \cdot X[0] + b \cdot X[1] \) - \( x'_2 = c \cdot X[0] + d \cdot X[1] \) This returns these derivatives as a list. 4
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Recommended textbooks for you
Advanced Engineering Mathematics
Advanced Engineering Mathematics
Advanced Math
ISBN:
9780470458365
Author:
Erwin Kreyszig
Publisher:
Wiley, John & Sons, Incorporated
Numerical Methods for Engineers
Numerical Methods for Engineers
Advanced Math
ISBN:
9780073397924
Author:
Steven C. Chapra Dr., Raymond P. Canale
Publisher:
McGraw-Hill Education
Introductory Mathematics for Engineering Applicat…
Introductory Mathematics for Engineering Applicat…
Advanced Math
ISBN:
9781118141809
Author:
Nathan Klingbeil
Publisher:
WILEY
Mathematics For Machine Technology
Mathematics For Machine Technology
Advanced Math
ISBN:
9781337798310
Author:
Peterson, John.
Publisher:
Cengage Learning,
Basic Technical Mathematics
Basic Technical Mathematics
Advanced Math
ISBN:
9780134437705
Author:
Washington
Publisher:
PEARSON
Topology
Topology
Advanced Math
ISBN:
9780134689517
Author:
Munkres, James R.
Publisher:
Pearson,