Exerciseschevron_rightChapter 4chevron_right4.1
fitness_center

Exercise 4.1

Vectorized PVT Calculations

Level 2
Chapter 4: NumPy & Pandas
descriptionProblem

A reservoir engineer needs the oil formation volume factor BoB_o at a range of reservoir pressures. Below the bubble point, BoB_o rises as pressure drops because gas comes out of solution and the remaining oil expands. Above the bubble point, the oil is undersaturated and BoB_o stays roughly constant.

A simplified correlation:

Bo={1.0+0.00012×(PbP)if PPb1.0if P>PbB_o = \begin{cases} 1.0 + 0.00012 \times (P_b - P) & \text{if } P \le P_b \\ 1.0 & \text{if } P > P_b \end{cases}

Use vectorised NumPy (no Python for loops) to compute BoB_o at every pressure in one expression.

Given:

pressures = np.array([1500, 2000, 2500, 3000, 3500])
P_b = 2500

Store the result as a NumPy array bo with the same shape as pressures.

lightbulbHints (0/4)

Stuck? Reveal hints one at a time — they progress from nudge to near-solution.

codeYour solution
main.py
visibilityReveal reference solutionexpand_more

Try solving it yourself first — the hints walk you through it. The solution below is one valid approach; yours may differ and still be correct.

import numpy as np

pressures = np.array([1500, 2000, 2500, 3000, 3500])
P_b = 2500

bo = np.where(pressures <= P_b, 1.0 + 0.00012 * (P_b - pressures), 1.0)
print(bo)

lockCopying code is a Full Access feature.