In: Computer Science
The ideal gas law can be stated as P V = nRT where P = pressure in Pascals (Pa) V = volume in cubic meters (m3 ) n = number of moles in moles (mol) R = gas constant = 8.3145m3 Pa / ( K · mol ) T = temperature in Kelvin (K). Using MATLAB or Octave, Write a script that prompts the user for the pressure, the volume, and the temperature; storing each in a variable. The script should then calculate the number of moles, saving it to a variable, and print the result. Sample output: Enter the pressure in Pascals: 101.3 Enter the volume in cubic meters: 3.5 Enter the temperature in Kelvin: 301 A gas with pressure 101.30 Pa, volume 3.50 mˆ3, and temperature 301.00 K has 0.14 moles Enter the pressure in Pascals: 98.1378 Enter the volume in cubic meters: 10 Enter the temperature in Kelvin: 250.2 A gas with pressure 98.14 Pa, volume 10.00 mˆ3, and temperature 250.20 K has 0.47 moles
Screenshot of the code:
Sample Output:
//Sample Run 1:
//Sample Run 2:
Code To Copy:
%Prompt the pressure, temperature and volume.
Pressure = input("Enter the pressure in Pascals: ")
Volume = input("Enter the volume in cubic meters: ")
Temperature = input("Enter the temperature in Kelvin: ")
%Define the gas constant.
R = 8.3145;
%Compute the moles.
n = (Pressure .* Volume)/(R .* Temperature)
%Display the result.
fprintf("A gas with pressure %.2f Pa, volume %.2f mˆ3, and
temperature %.2f K has %.2f moles", Pressure, Volume, Temperature,
n)