# I want to plot graph between partial pressure of ammonia and Length but it has error.

5 views (last 30 days)
clear all
tic
rlength =10; %meter
dx = 0.1;
Numdata = rlength/dx;
%constant value
Cpf=0.707; %kcal/kg.K
Cpg=0.719; %kcal/kg.K
f=1.0;
H=-26000; %kcal/kg.mol
R=1.987; %kcal/kg.mol.K
S1=10; %meter
S2=0.78; %square meter
U=500; %kcal/h.m^2.K
W=23760; %kg/h
N0=701.2; %kg.mol/m^2.h
%initial Temperature and mass flow rate of nitrogen
Tf(1)=600;
Tg(1)=600;
N2(1)=700;
for n=1:1:Numdata
%Find k values
k1=1.78954e4*exp(-20800/(R*Tg(n)));
k2=2.5714e16*exp(-47400/(R*Tg(n)));
%find partial pressure
pN2=286*N2(n)/(2.598*N0 + 2*N2(n));
pH2=3*pN2;
%Process
pNH3(n)=286*(2.23*N0-2*N2(n))/(2.598*N0 + 2*N2(n));
Tf(n+1)=Tf(n)+dx*(-((U*S1)/(W*Cpf))*(Tg(n)-Tf(n)));
Tg(n+1)=Tg(n)+dx*(-((U*S1)/(W*Cpg))*(Tg(n)-Tf(n))+((-H*S2)/(W*Cpg))*(f*(k1*pN2*(pH2^1.5)/pNH3(n) - k2*pNH3(n)/(pH2^1.5))));
N2(n+1)=N2(n)+dx*(-f*(k1*pN2*(pH2^1.5)/pNH3(n) - k2*pNH3(n)/(pH2^1.5)));
end;
for n = 1:1:Numdata+1
%convert length
xi(n) = (n-1)*dx;
end
Tf(Numdata)
Tg(Numdata)
N2(Numdata)
%show figure
figure
plot(xi,Tf,'-r'),...
ylabel('Temperature(K)'),title('Feed gas'),grid
xlabel('Reactor Length (m)')
axis([0 10 100 900])
figure
plot(xi,Tg,'-b'),...
ylabel('Temperature(K)'),title('Product gas'),grid
xlabel('Reactor Length(m)')
axis([0 10 100 900])
figure
plot(xi,N2,'-g'),...
ylabel('Temperature(K)'),title('flow rate of nitrogen'),grid
xlabel('Reactor Length(m)')
axis([0 10 100 900])
figure
plot(xi,pNH3,'-y'),...
ylabel('Patial Pressure of ammonia(psia)'),title('ammonia'),grid
xlabel('Reactor Length (m)')
axis([0 10 100 900])
%show running time
disp("sampling length is "+dx)
toc

Jon on 19 Jan 2022
Edited: Jon on 19 Jan 2022
Note that in your assignment for pNH3 the index is n, not n+1 so it only get assigned values up to the max value of n set by the for loop of for n = 1:Numdata. So the length of pNH3 is 100. On the other hand xi is assigned using the index n+1, so its length is 101. So when you plot pNH3 vs xi the two vectors don't have the same length, So you get the error:
Error using plot
Error using plot
Vectors must be the same length.
Error in ammonia (line 62)
plot(xi,pNH3,'-y'),...
To correct this change line 62 to
plot(xi(1:end-1),pNH3,'-y'),...
ylabel('Patial Pressure of ammonia(psia)'),title('ammonia'),grid
That is only plot elements 1 to 100 of xi.
You will also have to change your plot limits, as it seems that pNH3 is around 14 psia, so change line 65 to
axis([0 10 0 900])
For the future, when you have an error in your code, please add some explanation of what you are trying to do in your post, and the problem you are having. also cut and paste the full error message into your post so readers can see exactly what error you are getting
Nattanit Benjangvisanu on 20 Jan 2022
thank you.