Skip to content
The Chat Archive
gpt-3.5-turbo-0301· OpenAI WildChat-4.8M (AI2)

I need a python programm to draw a phase portrait of system of differential equations

  1. User

    I need a python programm to draw a phase portrait of system of differential equations

  2. Assistant

    Here is an example code that uses the matplotlib library to draw the phase portrait of a system of differential equations:

    import numpy as np
    import matplotlib.pyplot as plt
    
    # Define the system of differential equations
    def f(y, t):
        x, y = y
        dxdt = 3*x + 2*y
        dydt = 4*x + 6*y
        return [dxdt, dydt]
    
    # Set the range of t values
    t = np.linspace(0, 2, 200)
    
    # Set the initial conditions
    y0 = [1, 1]
    
    # Solve the system of differential equations using the odeint function from scipy.integrate
    from scipy.integrate import odeint
    ys = odeint(f, y0, t)
    
    # Plot the phase portrait using the plot function from matplotlib
    plt.plot(ys[:,0], ys[:,1])
    plt.xlabel('x')
    plt.ylabel('y')
    plt.title('Phase Portrait of System of Differential Equations')
    plt.show()
    

    In this example, the system of differential equations is defined as f(y, t), where y is a vector of the variables x and y, and t is the independent variable. The initial conditions are set to y0 = [1, 1], which are the values of x and y at t = 0. The odeint function is used to solve the system of differential equations for the range of t values, and the resulting values of x and y are plotted using the plot function from matplotlib.