Python: Calculation of drag force experienced by a cyclist.
AIM: Write a code in python to calculation drag force experienced by a cyclist and output the plots
OBJECTIVES:
Write a code to -
- calculate drag force
- show velocity vs drag force plot
- show drag force co-efficient vs drag force plot
GOVERNING EQUATION:
The Drag Force Formula,
`F_D=1/2 ρAC_D v^2`
Where, `F_D` is the drag force, N or kg/m2
`C_D` is the drag coefficient, unitless
`A` is the reference area, m2
`ρ` is the density of the fluid, kg/m3
`v` is the velocity of the cyclist, m/s
PROCEDURE:
First we import a package/module/library named matplotlib.pyplot into our program and address it as plt. matplotlib package is an object-oriented plotting library. A procedural interface is provided by the companion pyplot module, which may be imported directly, e.g. import matplotlib.pyplot as plt. Module matplotlib.pyplot is a state-based interface to matplotlib. It provides a MATLAB-like way of plotting. pyplot is mainly intended for interactive plots and simple cases of programmatic plot generation.
Then the necessary parameters values are given viz. drag coefficient, density of medium, frontal area, drag force, velocities. Here we have given 25 velocities ranging from 0 to 25 with step of 1. In python we directly do that without creating an array by using range() function. range() can be used in two ways : 1) range(last_value) – in this case python we consider values from 0 to our inputted last value with step of 1; 2) range(first_value,last_value,step) – in this case we can specify the starting range value, ending range value and the step values.
We create an empty array for drag forces. Then we use for loop to calculate drag force for a specific value of velocity and repeat this loop for all 25 values of velocity.
Then we create plot for velocity vs drag force for our result. Here we make use of matplotlib.pyplot which we imported at the start and defined to refer it as plt. While writing code first we have to specify the library name followed by ‘.’ and our command which is in this case ‘plot’. This simply tells python that we are using plot command which resides under plt (matplotlib.pyplot). This command will create our desired plot. Now we label the x-axis, y-axis and give the title to the plot. In the end we have to use show() command otherwise plot will be generated but not be displayed as output. Beware to use this command in the end if we use this command before specifying labels for x and y axis the labels and title of the plot will not be shown in output.
After this we calculate drag force for different shapes. For different shapes the values of drag coefficient are different. For simplification of calculation we keep the velocity constant i.e. 10 m/s. Then we apply for loop to calculate drag force for all the values of drag coefficient. After the loop is complete, we generate a drag coefficient vs drag force plot in a similar way as we generated previous plot.
Here we use append in loop while calculating drag force. What append does is it appends (adds) objects at the end of the list. In this code it appends the values of drag force for different values of velocities and drag coefficient one after the other and creates an array.
CODE:
'''
Program to calculate the drag force
By Utkarsh Palwekar
'''
import matplotlib.pyplot as plt
#Inputs
dc = 0.8 #drag coefficients
rho = 1.2 #density of the medium (kg/m^3)
a = 0.1 #frontal area (m^2)
velocities = range(25) #velocities of cyclist (m)
drag_forces = [] #drag forces
#For Loop
for velocity in velocities:
drag_forces.append((0.5*dc*rho*a*velocity*velocity))
#Print Drag force results
print(drag_forces)
#Creating Plots - Velocity vs Drag Force
plt.plot(velocities,drag_forces,'-o')
plt.xlabel('Velocity')
plt.ylabel('Drag Force')
plt.title('Velocity vs Drag Force')
plt.grid()
plt.show()
#List of different drag co-efficient for different shapes
dc =[0.04, 0.09, 0.42, 0.47, 0.82, 1.15] #Drag co-efficient array
v=10 #velocity of cyclist
d_f=[] #drag forces
#For Loop
for d_c in dc:
d_f.append((0.5*d_c*rho*a*v*v))
#Print Drag force results
print(d_f)
#Creating Plots - Drag Coefficient vs Drag Force
plt.plot(dc,d_f,'-*')
plt.xlabel('Drag Co-efficient')
plt.ylabel('Drag Force')
plt.title('Drag Coefficient vs Drag Force')
plt.grid()
plt.show()
OUTPUT:
The first output line represents the drag force results for different velocities.
The second output line represents the drag force results for different drag coefficient values.
CONCLUSION:
From velocity vs drag force plot we can conclude that as the velocity increases the drag force increases.
From drag coefficient vs drag force plot we can conclude that as the drag coefficient increases drag force increases.
ERRORS & SOLUTIONS:
Indentation Error - While apply for loop if the next code line starts from the beginning instead of some space from the start it will give indentation error. Make sure that all the code line of the loop leaves some space form the start until the loop code is complete. Also remember to give ‘:’ in the loop starting code line to tell python that code line from here are loop’s code lines, for here the loop starts.
Plot error - We have to give plt.show() command in the end to tell python – show us the plot. Beware to use this command after all plot code is done.
REFERENCES: