For loop beginner question
Show older comments
I just want to know why my loop is not working properly.
x = 0;
t = transpose(1:1:62403); %time vector [s]
n = length(x);
xdot = zeros(n, length(t));
for i = 1:n;
NH3_in= (((M_urea/2).*(X_H4N2CO*rho_air))./(M_H4N2CO.*((M_exhgas+(M_urea/2))))); [mol/m3]
NH3_in_ppm = ((NH3_in.*M_air)./rho_air).*10^6; % [ppm]
subplot(4,1,1);
plot (NH3_in_ppm);
ylabel('NH3 In [ppm]','fontsize',12,'FontWeight','bold','color','r')
ylim([0 1500]);
mass = ((M_exhgas)+(M_urea/2)); %Exh_vel = ((mass)/(rho_air*A)); [m/s]
Gamma = ((Exh_vel)./(L*omega));
T = ((1./Tbscr_K)-(1./T_ref));
r_sn = Ar .* (exp((-E_NO2./R_gasconstant).*(T))); [m3/mol/s]
A = NOx_in; [mol/m3]
B = 1+((r_sn.*x)./(Gamma));
h1 = A./B ; [mol/m3] (h1)
subplot(4,1,2);
h1_ppm = ((h1.*M_air)./(rho_air)).*10^6; % [ppm] Conversion
plot(h1_ppm);
%ylim([0 40])
ylabel('NOx Out [ppm]','fontsize',12,'FontWeight','bold','color','r')
%
r_a = Aa * (exp((-E_a./R_gasconstant).*(T)));
r_d = Ad * (exp((-E_d./R_gasconstant).*(T)));
h2 = ((NH3_in.*Gamma) + (r_d.*x))./((Gamma)+(r_a.*(1-x))); [mol/m3] (h2)
subplot(4,1,3);
h2_ppm = ((h2*M_air)/rho_air)*1e6; % [ppm] Conversion
plot(h2_ppm);
ylabel('NH3 Out [ppm]','fontsize',12,'FontWeight','bold','color','r')
%ylim([0 500]);
%
xdot(i,:) = (Gamma.*(NH3_in+h1-h2-NOx_in)); [-]
subplot(4,1,4);
x = x + xdot(i,:) ; %integeration
plot(x);
%ylim([0 1.5])
xlabel('Time [s]','fontsize',12,'FontWeight','bold','color','r')
ylabel('surface Coverage','fontsize',12,'FontWeight','bold','color','r')
end
the formula written for 'B' in the above script doesnt take the iterative x value. its just taking zero in all the iteration.
where is the loop going wrong?
8 Comments
Adam
on 4 Nov 2015
Have you put a breakpoint in and looked at all the components? For B to be 0 every time suggests that ((r_sn.*x)./(Gamma)) equals -1 all the time. It should be easy to at least gather some more information by debugging.
Heartrin
on 4 Nov 2015
Adam
on 4 Nov 2015
So this line must be evaluating to 0 every time through the loop:
xdot(i,:) = (Gamma.*(NH3_in+h1-h2-NOx_in));
but this is still easiest to check with breakpoints. It is almost impossible for someone to just glance at a complex piece of code and pick out why the result of a whole chain of equations might evaluate to 0
Heartrin
on 4 Nov 2015
Adam
on 4 Nov 2015
No, in the editor add a breakpoint on the key line by clicking in the margin of the file.
You can add then programmatically as well I think, but I never do.
Heartrin
on 4 Nov 2015
Thorsten
on 4 Nov 2015
We need the values of all variables and the formula you try to implement to give some further advice.
Heartrin
on 4 Nov 2015
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!