필터 지우기
필터 지우기

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

조회 수: 29 (최근 30일)
adam
adam 2012년 12월 1일
댓글: Image Analyst 2022년 12월 13일
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)

채택된 답변

Matt Fig
Matt Fig 2012년 12월 1일
편집: Matt Fig 2012년 12월 1일
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
adam
adam 2012년 12월 2일
Thanks alot for the help, you dont know how much it has helped me out :)

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Image Analyst
Image Analyst 2012년 12월 1일
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
Arda Güçlü
Arda Güçlü 2016년 1월 11일
편집: Arda Güçlü 2016년 1월 11일
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 2016년 1월 11일
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.

댓글을 달려면 로그인하십시오.


jackie
jackie 2022년 12월 13일
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
Image Analyst
Image Analyst 2022년 12월 13일
@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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Oil, Gas & Petrochemical에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by