Dynamic Pressure for an Average Rocket
Develop the code for the dynamic pressure experienced by a rocket during launch.
We'll cover the following
Marking Max Q
You also want to mark on the graph what the maximum pressure is and where it occurs; by marking this point, you can say at what time Max Q occurs and what value the maximum dynamic pressure reaches. You will use the built-in max()
function to determine the greatest number in y_values
, and you will use the index()
function to determine where that value is in the x_values
. Then, you can use that index to find out what elapsed time the highest value corresponds to. It is important to note that the index is the location in the list
of where the Max Q is in y_values
and the time at which it occurs in x_values
; it is not the time at which Max Q occurs, nor is it the value of Max Q.
max_val = max(y_values)
ind = y_values.index(max_val)
So, x_values[ind]
is the time at which Max Q occurs, and y_values[ind]
is the value of Max Q. You will use plt.annotate()
function to add an arrow pointing to the max value and to add a label with the value and time at which Max Q occurs. In Python:
plt.annotate('{:,2E} psf @ {} s'.format(max_val, x_values[ind]),
xy=(x_values[ind] + 2, max_val),
xytext=(x_values[ind] + 15, max_val + 1E5),
arrowprops=dict(facecolor='blue', shrink=0.05))
Since the value will be in billions, let’s round the value to 2 decimal points and use scientific notation, which is what {:,2E}
is doing. The plt.annotate()
argument accepts the label text (which is populated with Max Q and the time at which it occurs), x and y values to start the arrow, x and y values to start the label text, and a dictionary for arrowprops
to customize the arrow color and direction. The final step is some housekeeping to label the axes, create a chart title, create the legend, and show the plot that you have been creating.
plt.xlabel('Time (s)')
plt.ylabel('Pressure (psf)')
plt.title('Dynamic pressure as a function of time')
plt.legend()
plt.show()
Seeing your work
Hit run, and let’s see your creation!
Get hands-on with 1400+ tech skills courses.