Assignment 04

Assignment 4 – Functions Part 1

Outcomes

Be able to write functions that return numeric values

Use a python library (in this case pyplot).

Assignment

The Van Der Waals equation given at https://en.wikipedia.org/wiki/Van_der_Waals_equation is the full form of the well known equation PV = nRT or the ideal gas law. Where

P = Pressure of gas (atm)
V = Volume of gas (liters)
n = moles of gas
R = Universal gas constant (0.0821 liter-atm/mole-Kelvin)
T = Absolute temperature of the gas (Kelvin)

Using this write a function that (named CalculateVolume) CalculateVolume(P, n, T) will Calculate the Volume of a gas in liters given the arguments P, n, and T (R is a constant and can be built into the function).

Your program should prompt for user input in the correct units for the Temperature and plot the output the Volume in liters.

Your program should plot the volume vs. Temperature for the values of Pressure shown in the sample code. You will need to make sure that Temperature (input) is in the correct set of units (Kelvin). This plot should also be included in your Engineering Report.

Notice the plotting is simply 2 lines;

# Just set some values, put pressure and volume in arrays
# for plotting
pressure = [0.8, 0.9, 1.0, 1.1, 1.2] # Array of pressure
volume = []
n = 1
T = 473

# This will go through each value in pressure as p
# another type of loop that may be handy
# This assumes you wrote the function CalculateVolume
for p in pressure: 
   volume.append(CalculateVolume(p, T, n))

# This is - the plotting code
import matplotlib.pyplot as plt 
plt.scatter(pressure, volume)

I do typically like labels on my axis – that is very easy to do, just add the labels to the plotting code.

import matplotlib.pyplot as plt 
plt.xlabel("Pressure (atm)")
plt.ylabel("Volume (liters")
plt.scatter(pressure, volume)

There are a lot of plotting types in pyplot, in engineering programming scatter will be the most commonly used, but you can find them all at https://matplotlib.org/api/pyplot_summary.html

Your engineering report should solve the problem for a user input value of T (70F) and output the plot of values for the range of pressures. As always the methodology includes writing the code to solve the problem and output the desired results.

Materials

Lecture 4 – https://youtu.be/Y0T5Ud4YZ0k – Plotting is pretty simple, but much of what you need for this assignment is in Lecture 4. I do recommend by now that you have watched all the supplied Python learning video’s in the Lectures Page

Ideal Gas Law – https://en.wikipedia.org/wiki/Ideal_gas_law – This may be useful to you, programming or not, as an engineer,  you should know this.

Note that this is a part one of a multi-part assignment and this assignment is continued in Assignment 07 where you will be adding units to your program.