In [1]:
#Import packages.
import numpy as np
import matplotlib.pylab as plt
%matplotlib notebook
In [2]:
#Set folder to save images in.
import os
#os.chdir('Folder/Address/In/Here')
In [3]:
#Rename the function 'figure' from the 'plt' library (a.k.a. the 'matplotlib.pylab' library) to make it more convenient to use.
fig = plt.figure()
#Set up axes.
ax = fig.add_subplot(1, 1, 1)
In [4]:
#Define the parameters in the problem.
#For this phase space, if you are referring to the two earlier problems "Position of Moving Object Under Decelerating Force" and "Velocity of Moving Object Under Decelerating Force," note that here the parameters k, b, and v_0 must be the same.
k = 2
b = 3
v_0 = 15
C = v_0**2 / (v_0**2 + b**2)
#Pretend to plot the function as a smooth curve by plotting a thousand points close together.
t_f = 10
t = np.linspace(0, t_f, 10000)
#Above creates 1000 evenly-spaced locations between 0 and some final time, t_f.
#Define the function for position.
#Assume x > 0 (or use a negative sign in front of the expression for x < 0).
x = ( abs(b) / ( b**2 * k ) ) * (np.arccos( np.sqrt(C) * np.exp(-b**2 * k * t) ) - np.arccos( np.sqrt(C) ))
#Define the function for velocity.
#Assume v > 0 (or use a negative sign in front of the expression for v < 0).
v = ( abs(b) * C * np.exp( -b**2 * k * t ) ) / np.sqrt( 1 - C * np.exp(-2 * b**2 * k * t))
#Plot the function. Here we want to plot position, x, on one axis, and velocity, v, on the other. Position and velocity are both functions of time, t, so each point in time will make a point on the curve. This is called a parametric curve.
#A parametric curve also uses the 'ax.plot' function.
ax.plot(x, v, 'b-', label = 'Phase of Object')
#Label the plot.
fig.suptitle('Phase Space of Object with Decelerating Force $F = -mk(v^3 + a^2v)$')
ax.set_xlabel('Position, x (meters)')
ax.set_ylabel('Velocity, v ($\\frac{meters}{s}$)')
plt.show()
The graph might look 'choppy.' This is because the object may slow down so quickly from the inital velocity that the time span of 1/1000th of a second isn't enough to capture a smooth curve. To get a smooth curve, we'd need to either
reduce the force, with smaller values of k and b - which makes sense, because a large decelerating force slows the object very quickly, or
increase the number of points from 1000 to 2000, or perhaps to 10,000 to see the smooth curve. This is what to do if given a real physical situation where the constants determining the force can't be changed.