Compute the missing quantity among P, V, T for an ideal gas - MATLAB Cody - MATLAB Central

Problem 45411. Compute the missing quantity among P, V, T for an ideal gas

Difficulty:Rate

Consider 100 mol of helium gas at a certain pressure (P), volume (V), and temperature (T). Assuming that the ideal gas law applies, can you compute one of the 3 quantities given the other two?

Recall that, with SI units, the ideal gas law is given by:

P x V = n x R x T
  where:
  P = pressure [Pa] or [kg/m/s^2]
  V = volume [m^3]
  n = number of moles [mol]
  R = gas constant, 8.314 [J/mol/K] or [kg.m^2/K/mol/s^2]
  T = temperature [K]

Write a function that takes a MATLAB variable, x, which is always a 3-element row vector containing the values of P, V, T in that order. However, exactly one of these values will be NaN, which you must solve using the ideal gas law equation above, given the other two values. All inputs are given in SI units, hence, you can use the given value of R above. Note that n = 100 mol. You are ensured that P, V, and/or T are floating-point numbers with 2 decimal places that satisfy the following constraints:

  • 1 x 10^5 <= P <= 3 x 10^5
  • 1 <= V <= 10
  • 300 <= T <= 500

Output the value of the missing quantity rounded to 2 decimal places, followed by a space, and then the correct units, either Pa, m^3, or K. For this, you can use sprintf. See sample test cases:

>> idealgas([233424.06 NaN 435.02])
ans =
  '1.55 m^3'
>> idealgas([109238.31 2.76 NaN])
ans =
  '362.64 K'
>> idealgas([NaN 1.19 411.97])
ans =
  '287825.09 Pa'

Solution Stats

32.2% Correct | 67.8% Incorrect
Last Solution submitted on Mar 18, 2025

Problem Comments

Solution Comments

Show comments
PIVlab surpasses 100K all-time File Exchange downloads
During the past twelve months, PIVlab, a MATLAB Community Toolbox for particle...
4
8
LLMs with MATLAB updated to support the latest OpenAI Models
Large Languge model with MATLAB, a free add-on that lets you access...
2
4

Group

Guess the Logic! Image
Guess the Logic!
  • 17 Problems
  • 241 Finishers

Problem Recent Solvers162

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!