Can anyone help me to get this PV=nRT working? please.

70 views (last 30 days)
Just to let you know this is the first time that iam using matlab and have very basic knowledge, if any at all ahah. But basically i have to find the P constant at volume changes between 3 and 5 litres, and at two temperature constants 372k and 472k.
Yet the way i have put it into matlab is does not seem to work and help on this would be appreciated, ill put what ive writen below.
%Ideal gas equation
% a program for calculating the pressure distribution in the ideal gas equation
%define the tempurature
T1 =372; % units of temperature in kelvi
T2 =427; % units of temperature in kelvin
% define R
R=0.08206; % units L.atm.mol^-1
% define n
n=1;
% define the array of volume in litres
V=3:.01*5:5;
% calculate the pressure disribution (when P is the subject in the ideal % gas equation)
for j=1:10
P1(j)=T1*R*n/V(j);
P2(j)=T2*R*n/V(j);
end
% plot the two graphs on the one axis
plot(3,P1)
hold
plot(V,P2)

Accepted Answer

Matt Fig
Matt Fig on 1 Dec 2012
Edited: Matt Fig on 1 Dec 2012
Change your FOR loop to this:
for jj=1:length(V) % Note, length(V), not 10!
P1(jj)=T1*R*n/V(jj);
P2(jj)=T2*R*n/V(jj);
end
And your call to PLOT to this:
plot(V,P1,'b',V,P2,'r')
Now you can also do things in a more MATLAB-ish way, without loops. This is called vectorization:
P1 = T1*R*n./V; % Note the ./ rather than /
P2 = T2*R*n./V;
  1 Comment
adam
adam on 2 Dec 2012
Thanks alot for the help, you dont know how much it has helped me out :)

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 1 Dec 2012
You don't need the for loop, and you need ./ instead of / to do an element by element divide. Try it this way:
%Ideal gas equation % a program for calculating the pressure distribution in the ideal gas equation %define the tempurature
T1 = 372; % units of temperature in kelvin
T2 = 427; % units of temperature in kelvin
% define R
R=0.08206; % units L.atm.mol^-1
% define n
n=1;
% Define the array of volume in litres
V = 3 : .01*5 : 5;
% Calculate the pressure disribution (when P is the subject in the ideal % gas equation)
P1 = T1 * R * n ./ V;
P2 = T2 * R * n ./ V;
% Plot the two graphs on the one axis
plot(V, P1, 'r-', 'LineWidth', 2)
hold
plot(V,P2, 'b-', 'LineWidth', 2)
grid on;
legend('P1', 'P2');
xlabel('V', 'FontSize', 20);
ylabel('P', 'FontSize', 20);
title('PV=nRT', 'FontSize', 20);
By the way, you can highlight your code and click the {}Code icon above the text box so that you don't have to put blank lines in between your lines of code to get them to show up on separate lines.
  2 Comments
Arda Güçlü
Arda Güçlü on 11 Jan 2016
Edited: Arda Güçlü on 11 Jan 2016
My school project is to write a general function for the ideal gas equation. Have you got any examples about it ?
Image Analyst
Image Analyst on 11 Jan 2016
No. But programming PV=NRT sounds pretty trivial, or could be depending on how simple or fancy you want the program to be.
I don't know what kind of output you are expecting, like a contour plot or image or whatever. If you want, you could start a new question with your attempt at code and a good description of what you want to show/visualize.

Sign in to comment.


jackie
jackie on 13 Dec 2022
can anyone give a loop for this one thankyou
clc; clear all; close all;
% Make a Design program that will Calculate for the value of Pressure, Volume, and Temperature using Ideal gas Law.
% Program Data
fprintf('This program will be used to calculate for the values of Pressure, Volume, and Temperature using the Ideal Gas Law equation PV=nRT. \n');
% R Constant to be utilized
R=0.0821; %
% Equation Calculating Code
fprintf('Choose Variable to Solve. \n');
fprintf('Type in P for Pressure \n');
fprintf('Type in V for Volume \n');
fprintf('Type in T for Temperature \n\n');
Variable = input('Variable: ', 's');
if strcmp(Variable,'P');
fprintf('Calculate for Pressure.\n');
n=input('Type in Number of Moles (mol): ');
T=input('Type in Temperature value in deg Celsius (°C): ');
V=input('Type in Volume value in Liters (L): ');
P=[n*R*(T+273)]/V; % Calculating for Pressure with converted Temperature from °C to K
fprintf('Pressure = %f atm \n', P); % Final Answer
elseif strcmp(Variable,'V');
fprintf('Calculate for Volume.\n');
n=input('Type in Number of Moles (mol): ');
T=input('Type in Temperature value in deg Celsius (°C): ');
P=input('Type in Pressure value in Atmospheric Pressure (atm): ');
V=[n*R*(T+273)]/P; % Calculating for Volume with converted Temperature from °C to K
% fprintf('Volume = %f L \n', V); % Final Answer
elseif strcmp(Variable,'T');
fprintf('Calculate for Temperature.\n');
P=input('Type in Pressure value in Atmospheric Pressure (atm): ');
V=input('Type in Volume value in Liters (L): ');
n=input('Type in Number of Moles (mol): ');
T=(P*V)/(n*R)-273;
end
  1 Comment
Image Analyst
Image Analyst on 13 Dec 2022
@jackie here is one way:
% Make a Design program that will Calculate for the value of Pressure, Volume, and Temperature using Ideal gas Law.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% R Constant to be utilized
R=0.0821; %
button = 'Yes';
while strcmpi(button, 'Yes')
% Program Data
fprintf('This program will be used to calculate for the values of Pressure, Volume, and Temperature using the Ideal Gas Law equation PV=nRT. \n');
% Equation Calculating Code
fprintf('Choose Variable to Solve. \n');
fprintf('Type in P for Pressure \n');
fprintf('Type in V for Volume \n');
fprintf('Type in T for Temperature \n\n');
Variable = input('Variable: ', 's');
if strcmpi(Variable,'P')
fprintf('Calculate for Pressure.\n');
n=input('Type in Number of Moles (mol): ');
T=input('Type in Temperature value in deg Celsius (°C): ');
V=input('Type in Volume value in Liters (L): ');
P=[n*R*(T+273)]/V; % Calculating for Pressure with converted Temperature from °C to K
fprintf('Pressure = %f atm \n', P); % Final Answer
elseif strcmpi(Variable,'V')
fprintf('Calculate for Volume.\n');
n=input('Type in Number of Moles (mol): ');
T=input('Type in Temperature value in deg Celsius (°C): ');
P=input('Type in Pressure value in Atmospheric Pressure (atm): ');
V= n*R*(T+273) / P; % Calculating for Volume with converted Temperature from °C to K
% fprintf('Volume = %f L \n', V); % Final Answer
elseif strcmpi(Variable,'T')
fprintf('Calculate for Temperature.\n');
P=input('Type in Pressure value in Atmospheric Pressure (atm): ');
V=input('Type in Volume value in Liters (L): ');
n=input('Type in Number of Moles (mol): ');
T=(P*V)/(n*R)-273;
end
% Tell user the results and ask if they want to keep going.
message = sprintf('T = %f\nP = %f\nV = %f\nn = %f\n', T, P, V, n);
fprintf('%s', message);
reply = questdlg(message, 'Results', 'OK - Continue', 'Quit', 'OK - Continue');
if strcmpi(reply, 'Quit')
% User said Quit, so exit.
break;
end
end

Sign in to comment.

Categories

Find more on Oil, Gas & Petrochemical in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!