Need Help with while loop
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
The code I have so far works but I think I missing something with time as a factor. I'm not sure how to utilize that part. Any help would be much appreciated.
The problem statement is this: A type of pathogenic bacteria in a lake under optimal conditions double their number (growth rate of p = 1) every 20 hours. Starting with n0 bacteria at time zero, there are n1=n0(1+p) bacteria after 20 hours, n2=n1(1+p) bacteria after 40 hours, and so on. For this particular type of pathogenic bacteria and lake, the lake is not safe to swim in if the bacteria 2 population density exceeds 104 bacteria per ml. Write a MATLAB program that will determine how long it will take for an initial pathogenic bacteria level of n0=100 per ml to reach an unsafe level of n = 104 per ml. The program should display the time in days it takes for the bacteria population density to reach or exceed the unsafe level.
So far the code I have created is this: I understand I'm missing the display I know how to do that part, Its just the other parts I'm missing in my statement that I don't understand to put in the code:
clear;
clc;
% define max bacteria density level that is unsafe for swimming per mL
n_max = 10^4;
% intitialize(else 1st iteration,n_sum, p will be undefined)
n_sum = 0;
p =0;
% while loop with condition defined
while (n_sum <= n_max)
% update counter
p = p + 1;
n_sum = n_sum + p;
end
4 comentarios
per isakson
el 26 de Feb. de 2015
"(growth rate of p = 1) every 20 hours"   and   p = p + 1; % update counter   defines two different meanings of p, which is confusing.
dpb
el 26 de Feb. de 2015
Editada: dpb
el 26 de Feb. de 2015
It (the code definition, that is) is also incorrect, not just confusing.
Second suggestion--rethink what the rate equation is from the problem description and factor that into the formulation...even if the coded solution is to be the iterative one, writing the algebra should help to see how to implement it.
Respuestas (0)
Ver también
Categorías
Más información sobre Elementary Math 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!