HW04: Functions and Objects#

Purposes of this assignment is to get practice with the following:

  • If statements

  • Defining and calling functions

  • Define an object (class instance) and make a calculation using the objects attributes

Problem 1#

(a) Write a function that will print whether x is positive, negative, or zero for any given x. Test your function with x = 5, x = -3, and x = 0.


(b) Write function that will print whether x and y are both positive, or both negative, or if one or both are zero. Test your function with (x = 5, y = 3.2), (x = -4, y = 3.2), and (x = 0, y = 3.2).


(c) The ideal gas law is an approximation, but it is only a good approximation under certain circumstances. The general rule of thumb is when the specifc volume is greater than 5 L/mol for diatomic gases and 20 L/mol for all other gases. (Specific volume: \(\hat{V} = \frac{V}{n}\) ) $\(\hat{V}=\frac{RT}{P} > 5 \text{or 20 L/mol for diatomic or other gases, respectively} \)$ Write function that will print the ideal specifc volume of a gas, and state whether or not the ideal gas law is a good approximation. You may need more than one input for this function. Test your function with the following scenarios:

Scenario 1

Scenario 2

gas = oxygen

gas = butane

gas_type = ‘diatomic’

gas_type = ‘other’

T = 600 K

T = 300 K

P = 15 atm

P = 1 atm

Problem 2#

(a) Write a function called “sum” that takes two numbers and returns the sum of them.

(b) Write a function called “quadroots” that takes three numbers \(a\), \(b\), and \(c\) as function arguments that are the coefficients of the quadratic formula \(ax^2+bx+c=0\), and returns the two roots. Test your function on \(a=1,\,b=-1,\, c=-2\). You should get roots of 2 and -1.

(c) Functions are a convenient way to convert units. Write a function that converts

  1. Celsius for Farhenheit,

  2. Farheneit to Celsius,

  3. Liters to Gallons, and

  4. kg/h to lbm/s

Use each function at least once to print the values of 40 C to F, 102 F to C, 19 L to gal, and 34 kg/h to lbm/s.

Problem 3#

Write a function for the Peng Robinson equation of state. The Peng Robinson equation of state is given by:

\[\begin{split} \begin{align} P = \frac{RT}{V_m-b}&-\frac{a\alpha}{V_m(V_m+b)+b(V_m-b)} \tag{1} \\ a &= 0.45724\frac{R^2T_c^2}{P_c} \tag{2a} \\ b &= 0.07780\frac{RT_c}{P_c} \tag{2b} \\ \alpha &= \left[1+\kappa\left(1-\sqrt{\frac{T}{T_c}}\right)\right]^2 \tag{2c}\\ \kappa &= 0.37464+1.54226\omega-0.26992\omega^2 \tag{2d} \\ T_r &= \frac{T}{T_c} \tag{2e}\\ \end{align} \end{split}\]

where \(T_c\) is the critical temperature, \(P_c\) is the critical pressure, \(\omega\) is the acentric factor, and \(T_r\) is the reduced temperature. The critical properties for carbon dioxide are \(T_c = 304.2\) K and \(P_c = 73.8\) atm. The acentric factor for carbon dioxide is \(\omega = 0.225\). The gas constant is \(R = 0.08206\) L atm/(mol K).

Evaluate the function to determine the pressure for the following conditions:

  • T = 600 K

  • V = 0.05 \(\text{m}^3\)/mol

  • Carbon dioxide (\(\text{CO}_2\)) is the gas

Problem 4#

In the class Motivation, we calculated the adiabatic flame temperature for a butane-air mixture. Modify the code to calculate the adiabatic flame temperature for a pentane-air mixture. You’ll use property data from the DIPPR database. As a BYU student (accessing the website from on-campus), you have access here.

Action 3: Copy the code done in the manual calculation in the Motivation discussion (the flame temperature calculation that does not use Cantera). Follow the below steps to modify the code to calculate the adiabatic flame temperature for a pentane-air mixture.

  • Copy the code from the Motivation discussion and paste it into the cell below. There are 3 code blocks you’ll need. Make sure you can get the code to run to predict the adiabatic flame temperature for a butane-air mixture.

  • Modify the code by switching the butane (C4H10) to pentane (C5H12) with the associated Cp properties from DIPPR. You’ll also have to modify the coefficients in the other gas descriptions (C5H12 + 8O2 = 6H2O + 5CO2) for pentane.

Report the code and the adiabatic flame temperature in degrees Celsius and comment on whether you think it is accurate or not.

Problem 5#

Create a class called gas that has the following attributes:

  • critical temperature

  • critical pressure

  • acentric factor

  • temperature

  • molar volume

  • a function that can return the pressure given the temperature and molar volume using the Peng Robinson equation of state you programmed in Problem 3

Credit to Dr. John Hedengren for many of these problems (see also APMonitor.com))