How do I generate a vector field plot for logistic equation K = 1 using matplotlib?

wonkybadonk

I'm going through Strogatz's Nonlinear Dynamics and Chaos and I've hit a snag in chapter 2 Exercise 2.8.1. (Educator flag: I've graduated so this isn't for a class, I'm just trying to get back into the numerical solving of differential equations) It's a pretty simple differential equation and I can plot individual solution curves given different initial conditions but I'm trying to use quiver or streamplot to superimpose individual solutions on top of the vector field.

My problem is in understanding how to translate the vector field plots for similar problems in the dy/dx form found here over to the dx/dt form that's primarily tackled in Strogatz's book.

Given that the x vector that's defined in the logistic function is only one dimensional I'm having a hard time reasoning out how express the u and v flows in quiver or streamplot since the problem only seems to have a u flow. It's probably super easy and is being over-thought but any guidance or assistance would be much appreciated!

So far I have the following:

# 2.8.1
# Plot the vector field and some trajectories for xdot = x(1-x) given 
# some different initial conditions for the logistic equation with carrying 
# capacity K = 1

# dx/dt = x(1-x)  

# Imports:
from __future__ import division
from scipy import *
import numpy as np
import pylab
import matplotlib as mp
from matplotlib import pyplot as plt  
import sys
import math as mt

def logistic(x,t):
    return np.array([x[0]*(1-x[0])])

def RK4(t0 = 0, x0 = np.array([1]), t1 = 5 , dt = 0.01, ng = None):  
    tsp = np.arange(t0, t1, dt)
    Nsize = np.size(tsp)
    X = np.empty((Nsize, np.size(x0)))
    X[0] = x0

    for i in range(1, Nsize):
        k1 = ng(X[i-1],tsp[i-1])
        k2 = ng(X[i-1] + dt/2*k1, tsp[i-1] + dt/2)
        k3 = ng(X[i-1] + dt/2*k2, tsp[i-1] + dt/2)
        k4 = ng(X[i-1] + dt*k3, tsp[i-1] + dt)
        X[i] = X[i-1] + dt/6*(k1 + 2*k2 + 2*k3 + k4)
    return X

def tplot():
    t0 = 0
    t1 = 10
    dt = 0.02
    tsp = np.arange(t0,t1,dt)
    X = RK4(x0 = np.array([2]), t1 = 10,dt = 0.02, ng = logistic)
    Y = RK4(x0 = np.array([0.01]), t1 = 10,dt = 0.02, ng = logistic)
    Z = RK4(x0 = np.array([0.5]), t1 = 10,dt = 0.02, ng = logistic)
    P = RK4(x0 = np.array([3]), t1 = 10,dt = 0.02, ng = logistic)
    Q = RK4(x0 = np.array([0.1]), t1 = 10,dt = 0.02, ng = logistic)
    R = RK4(x0 = np.array([1.5]), t1 = 10,dt = 0.02, ng = logistic)
    O = RK4(x0 = np.array([1]), t1 = 10,dt = 0.02, ng = logistic)
    pylab.figure()
    pylab.plot(tsp,X)
    pylab.plot(tsp,Y)
    pylab.plot(tsp,Z)
    pylab.plot(tsp,P)
    pylab.plot(tsp,Q)
    pylab.plot(tsp,R)
    pylab.plot(tsp,O)
    pylab.title('Logistic Equation - K=1')
    pylab.xlabel('Time')
    pylab.ylabel('Xdot')
    pylab.show()

print tplot()

image here

tom10

To graph a slope from a derivative (like, dx/dt), you can first find dx/dt, and then use a fixed dt to calculate dx. Then, at each (t, x) of interest, plot the little line segment from (t,x) to (t+dt, x+dx).

Here's an example for your equation dx/dt = x(1-x). (The Strogatz picture doesn't have arrowheads so I removed them too.)

import numpy as np
import matplotlib.pyplot as plt

times = np.linspace(0, 10,  20)
x = np.linspace(0 ,2, 20)
T, X = np.meshgrid(times, x)   # make a grid that roughly matches the Strogatz grid  

dxdt = X*(1-X)            # the equation of interest
dt = .5*np.ones(X.shape)  # a constant value (.5 is just so segments don't run into each other -- given spacing of times array
dx = dxdt * dt            # given dt, now calc dx for the line segment

plt.quiver(T, X, dt, dx, headwidth=0., angles='xy', scale=15.)
plt.show()

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python - matplotlib - how do I plot a plane from equation?

From Dev

How do I generate Primes Using 6*k +- 1 rule

From Dev

How do I plot a vector data in a spatial visualization, using GGMAP?

From Dev

How do I plot a vector data in a spatial visualization, using GGMAP?

From Java

How do I plot Shapely polygons and objects using Matplotlib?

From Dev

How do I plot a non-linear model using matplotlib?

From Dev

How do I plot the decision boundary of a regression using matplotlib?

From Dev

How do I create a purchase activity plot using matplotlib?

From Dev

How do I generate a histogram from a list of values using matplotlib?

From Dev

How can I plot a surface using a function with a single vector or array input using matplotlib?

From Dev

How do I plot non linear equation in MATLAB?

From Dev

How do I plot a list of tuples with matplotlib?

From Dev

How do I plot only a table in Matplotlib?

From Dev

How do I plot multiple functions in matplotlib?

From Dev

Calculate curl of a vector field in Python and plot it with matplotlib

From Dev

How can i plot this transcendental equation in R?

From Java

How do I plot in real-time in a while loop using matplotlib?

From Dev

How do I plot a 2D array graph in Python using matplotlib

From Dev

How do I solve exponential equation in symbolic form using Lambertw

From Dev

How to derive an objective function for a multi-class logistic regression classifier using 1-of-k encoding?

From Dev

How do I get the equation for a regression line in log-log plot in ggplot2?

From Dev

How do I plot the point of max value on a line graph in matplotlib?

From Dev

How do I make a matplotlib scatter plot square?

From Dev

How do I plot stacked histograms side by side in matplotlib?

From Dev

How do I plot a list all at once with matplotlib.pyplot?

From Dev

How do I generate a random vector in TensorFlow and maintain it for further use?

From Dev

How do I use seq() and rep() and paste() to generate a specific vector?

From Java

How to generate several legends for single plot matplotlib

From Dev

How can I generate this plot?

Related Related

  1. 1

    Python - matplotlib - how do I plot a plane from equation?

  2. 2

    How do I generate Primes Using 6*k +- 1 rule

  3. 3

    How do I plot a vector data in a spatial visualization, using GGMAP?

  4. 4

    How do I plot a vector data in a spatial visualization, using GGMAP?

  5. 5

    How do I plot Shapely polygons and objects using Matplotlib?

  6. 6

    How do I plot a non-linear model using matplotlib?

  7. 7

    How do I plot the decision boundary of a regression using matplotlib?

  8. 8

    How do I create a purchase activity plot using matplotlib?

  9. 9

    How do I generate a histogram from a list of values using matplotlib?

  10. 10

    How can I plot a surface using a function with a single vector or array input using matplotlib?

  11. 11

    How do I plot non linear equation in MATLAB?

  12. 12

    How do I plot a list of tuples with matplotlib?

  13. 13

    How do I plot only a table in Matplotlib?

  14. 14

    How do I plot multiple functions in matplotlib?

  15. 15

    Calculate curl of a vector field in Python and plot it with matplotlib

  16. 16

    How can i plot this transcendental equation in R?

  17. 17

    How do I plot in real-time in a while loop using matplotlib?

  18. 18

    How do I plot a 2D array graph in Python using matplotlib

  19. 19

    How do I solve exponential equation in symbolic form using Lambertw

  20. 20

    How to derive an objective function for a multi-class logistic regression classifier using 1-of-k encoding?

  21. 21

    How do I get the equation for a regression line in log-log plot in ggplot2?

  22. 22

    How do I plot the point of max value on a line graph in matplotlib?

  23. 23

    How do I make a matplotlib scatter plot square?

  24. 24

    How do I plot stacked histograms side by side in matplotlib?

  25. 25

    How do I plot a list all at once with matplotlib.pyplot?

  26. 26

    How do I generate a random vector in TensorFlow and maintain it for further use?

  27. 27

    How do I use seq() and rep() and paste() to generate a specific vector?

  28. 28

    How to generate several legends for single plot matplotlib

  29. 29

    How can I generate this plot?

HotTag

Archive