Plot & SubPlot of First-order Differential equations

Plot & SubPlot of First-order Differential equations

In this article, We discuss more First-order Differential equations. Before moving to this article you should check the article to Solving first-order Ordinary differential equations then continue with this. Let's take a look at how plot and subplot of first-order differential equations in Python. First, start with Solving first-order Ordinary differential equations with scipy.integrate and then subplot to all of these equations in Python code.


To continue with Plot and SubPlot of First-order Differential equations one more thing you must be clear is subplots. What is the Matplotlib subplot and how it can be used in Python to plot multiple figures in one graph?


So let's discuss Matplotlib subplot() function. If you want to know how to install and import Matplotlib in Python then check Python Libraries tutorial first.

Matplotlib subplot ()

The matplotlib subplot () function can be said to plot two or more plots in a figure. Matplotlib supports all types of subplots including 2x1 vertical, 2x1 horizontal or 2x2 grids. pyplot.subplots construct a grid of figures and subplots with a call, providing appropriate control over how individual plots are created. For more advanced use cases you can use GridSpeak for a more general subplot layout or add subplots to arbitrary locations within the figure.

figure .add_subplot ()

The figure .add_subplot () method is one of the easiest ways to split an existing shape object into separate regions of different sizes. The function subplot creates a figure and a set of subplots. It is a wrapper function to facilitate the creation of common layouts of subplots, including function objects in a wrapper.


1. Plot the following First-order Differential equation using Python.

Python Code:
from scipy.integrate import odeint
import numpy as np
from matplotlib import pyplot as plt
def f(y,x):
    return (x**2/y)
y0 = 1xs = np.arange(0,5,0.30)
ys = odeint(f,y0,xs)
plt.plot(xs,ys,'-')
plt.plot(xs,ys,'ro')
plt.xlabel('x values')
plt.ylabel('y values')
plt.title('Differential equation: dy/dx=x**2/y')
plt.show()
Solving First-order Ordinary differential equations


2. Plot the following First-order Differential equation using Python.
Python Code:
from scipy.integrate import odeint import numpy as np from matplotlib import pyplot as plt def f(y,x): return (x**4-2*y/x) y1 = 1xs = np.arange(1,5,0.30) ys = odeint(f,y1,xs) plt.plot(xs,ys,'-') plt.plot(xs,ys,'ro') plt.xlabel('x values') plt.ylabel('y values') plt.title('Differential equation: dy/dx=(x**4-2*y/x)') plt.show()
Solving First-order Ordinary differential equations
3. Plot the following First-order Differential equation using Python.
from scipy.integrate import odeint
import numpy as np
from matplotlib import pyplot as plt
def f(y,x):
    return (np.exp(x-y))
y0 = 0xs = np.arange(0,5,0.25)
ys = odeint(f,y0,xs)
plt.plot(xs,ys,'-')
plt.plot(xs,ys,'ro')
plt.xlabel('x values')
plt.ylabel('y values')
plt.title('Differential equation: dy/dx=(np.exp(x-y))')
plt.show()
Solving First-order Ordinary differential equations
SubPlotting of the First-order Differential equation using Python
from scipy.integrate import odeint
import numpy as np
from matplotlib import pyplot as plt
def f(y,x):
    return x
y0 = 1xs = np.arange(0,1,0.1)
ys = odeint(f,y0,xs)
plt.figure()
plt.subplot(331)
plt.plot(xs,ys,'-')
plt.plot(xs,ys,'ro')
plt.xlabel('x values')
plt.ylabel('y values')
plt.title('dy/dx=x')
def f(y,x):
    return y/x
y1 = 1xs = np.arange(1,10,1)
ys = odeint(f,y1,xs)
plt.subplot(332)
plt.plot(xs,ys,'-')
plt.plot(xs,ys,'b*')
plt.xlabel('x values')
plt.ylabel('y values')
plt.title('dy/dx=y/x')
def f(y,x):
    return (x*y/(np.sqrt(y**2+x)))
y0 = 1xs = np.arange(0,5,0.25)
ys = odeint(f,y0,xs)
plt.subplot(333)
plt.plot(xs,ys,'-')
plt.plot(xs,ys,'g^')
plt.xlabel('x values')
plt.ylabel('y values')
plt.title('dy/dx=(x*y/(np.sqrt(y**2+x))')
def f(y,x):
    return (x +y)
y0 = 0xs = np.arange(0,2,0.1)
ys = odeint(f,y0,xs)
plt.subplot(334)
plt.plot(xs,ys,'-')
plt.plot(xs,ys,'bo')
plt.xlabel('x values')
plt.ylabel('y values')
plt.title('dy/dx=x+y')
def f(y,x):
    return (3*y-x)
y0 = 0xs = np.arange(0,5,0.25)
ys = odeint(f,y0,xs)
plt.subplot(335)
plt.plot(xs,ys,'-')
plt.plot(xs,ys,'b^')
plt.xlabel('x values')
plt.ylabel('y values')
plt.title('dy/dx=(3*y-x)')
def f(y,x):
    return (x**2)
y0 = 1xs = np.arange(0,5,0.25)
ys = odeint(f,y0,xs)
plt.subplot(336)
plt.plot(xs,ys,'-')
plt.plot(xs,ys,'g*')
plt.xlabel('x values')
plt.ylabel('y values')
plt.title('dy/dx=(x**2')def f(y,x):
    return (x**2/y)
y0 = 1xs = np.arange(0,5,0.30)
ys = odeint(f,y0,xs)
plt.subplot(337)
plt.plot(xs,ys,'-')
plt.plot(xs,ys,'r*')
plt.xlabel('x values')
plt.ylabel('y values')
plt.title('dy/dx=(x**2/y)')def f(y,x):
    return (x**4-2*y/x)
y1 = 1xs = np.arange(1,5,0.30)
ys = odeint(f,y1,xs)
plt.subplot(338)
plt.plot(xs,ys,'-')
plt.plot(xs,ys,'g*')
plt.xlabel('x values')
plt.ylabel('y values')
plt.title('dy/dx=(x**4-2*y/x)')def f(y,x):
    return (np.exp(x-y))
y0 = 0xs = np.arange(0,5,0.25)
ys = odeint(f,y0,xs)
plt.subplot(339)
plt.plot(xs,ys,'-')
plt.plot(xs,ys,'ro')
plt.xlabel('x values')
plt.ylabel('y values')
plt.title('dy/dx=(np.exp(x-y))')
plt.tight_layout()
plt.show()
Output:
SubPlotting of the First-order Differential equation using Python





SubPlotting of the First-order Differential equation using Python