For loop beginner question
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Heartrin
el 4 de Nov. de 2015
Comentada: Heartrin
el 4 de Nov. de 2015
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 comentarios
Thorsten
el 4 de Nov. de 2015
We need the values of all variables and the formula you try to implement to give some further advice.
Respuesta aceptada
Stephen23
el 4 de Nov. de 2015
Editada: Stephen23
el 4 de Nov. de 2015
This is what happens when you write a large block of code without checking it as your are writing it. We get many questions from beginners who write long pieces of code and they then complain "it does not do what I want it to do". Often they don't know what their own code is doing. Code is not magic: it does exactly what you tell it to do: if you don't know, then how is MATLAB supposed to know this?
If you learned to use some code best-practices then these would make your own code more readable, more understandable and easier to debug. These best-practices are not there to annoy students and get in your way: they are there because millions of programmer have learned that they make code more robust, easier to understand, and easier to write. Here are a few things that you need to consider:
Write code carefully, checking every line as you write it, make sure that it does what it should do, and perform your own sanity check calculation (by hand, on paper, or on your calculator) to make sure that the output is reasonable. If you had done this then you would have more of an idea why a zero pops up.
Consider edge cases: what happens if a value is zero, or too large, or too small, or in some way unexpected. Consider how your code will respond to likely edge cases.
Write comments. This is really really important. Write comments explaining what, why, how, or whatever info is required. Your code should make as much sense just by reading the comments as it does by following the code itself.
Use consistent formatting: the default MATLAB editor settings does quite nice formatting: why did you remove all of the loop indentation and other important formatting? This formatting makes code more readable, and clarifies the code structure. Your code has missing indentation and whitespace placed randomly throughout, sometimes a whole line of trailing whitespace...
Summary: write neater code and test your code as you write it.
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!