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

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)

Best Answer

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;
Related Question