Program to calculate drag forces experienced by a cyclist.
Program #1: Plot velocity v/s drag force for a cyclist
'''Program for calculating and plotting the drag force experienced by a cyclist when riding at different speeds.
Here, the Drag force is calculated as '0.5*density*frontal_area*velocity^2*drag_coefficient'
'''
import math # import required modules.
import matplotlib.pyplot as plt
#define variables.
c_d = 0.8 # drag coefficient
A = 0.1 # frontal area in m^2
rho = 1.2 # density of air in (kg/m^3)
v = [] # array of velocities in m/s.
#F_d = 0.5 * rho * A * (math.pow(v, 2)) * c_d # drag force in (kgm/s^2)
D = []
for velocity in range(0,100):
v.append(velocity)
print(v)
for velocity in v:
F_d = 0.5 * rho * A * (pow(velocity, 2)) * c_d
D.append(F_d)
print(D)
plt.plot(v,D)
plt.xlabel('velocity(m/s)')
plt.ylabel('drag force(N)')
plt.savefig('velocity versus drag force')
plt.show()
Explanation:
Imported the necessary modules and defined the required variables
A for loop is used to create a velocity list, v[], with velocities ranging from 0 to 100 m/s.
Another for loop is used to calculate the drag force value corresponding to each of the velocities in v[].
A list of all the drag forces, D[], is created by using '.append()' inside the second for loop.
The list of velocities and the list of drag forces are used to plot the required velocity v/s drag force graph.
plt.plot(x,y) was used to plot the graph
plt.savefig() was used to save the plots as .png files.
Output:
The output shows the list of velocities, the list of drag forces and the velocity vs drag force plot.
Program #2: Plot drag coefficient vs drag force for a cyclist riding at a constant speed assuming the same frontal area at all instances.
'''Program for plotting a graph btwn drag coefficient and drag force for a cyclist riding at a constant speed (for a range of drag coefficients.)'''
import math
import matplotlib.pyplot as plt
import numpy as np
v = 10 # velocity in m/s
rho = 1.2 # density of air in (kg/ m^3)
A = 0.1 # frontal area in m^2
c_d = np.linspace(0, 1, 11) # array of drag coefficients
print(c_d)
#f_d = 0.5 * density * frontal area * velocity^2 * drag_coeff (drag_force in Newton)
D = [] # array of drag forces corresponding to different drag_coeff's
for drag_coeff in c_d:
f_d = 0.5 * rho * A * (math.pow(v,2)) * drag_coeff
D.append(f_d)
print(D)
plt.plot(c_d, D)
plt.xlabel('drag coefficient')
plt.ylabel('drag force(N)')
plt.savefig('drag_coefficient versus drag_force')
plt.show()
Explanation:
Imported the necessary modules.
Defined the required variables.
Created an array of drag coefficients using np.linspace()
Used a for loop to calculate the drag force value corresponding to each value of the drag coefficient.
Used a list, D[] to hold all the drag force values.
plt.plot(x,y) was used to plot the graph between the drag coefficient and drag force.
Used plt.savefig() to see the plots.
Output:
The output shows the drag coefficient values, corresponding drag force values and the plot between the two.