while loop: how to define a function when using equation with several variables?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Sandie Nhatien Vu
el 6 de Ag. de 2016
Comentada: Sandie Nhatien Vu
el 6 de Ag. de 2016
So. I have this function as the bacteria growth:
n (t+1) = n(t) * (1 + (alpha*(1-(n(t)/K))))
The template for the function should look like this:
function tN = bacteriaGrowth(n0, alpha, K, N)
where n0 = initial number of bacteria (scalar)
alpha = growth rate (scalar)
K = capacity (scalar)
N = final population size (scalar, no < N < K)
tN = Time t at which population size exceeds N (scalar)
So I want to write a function that simulates the bacteria growth hour by hour and stops when the number of bacteria exceeds some fixed number, N. It must return the time t at which the population first exceeds N.
Ex: function tN = bacteriaGrowth(100, 0.4, 1000, 500) will give the output: 7
because: n0 = 100, n1 = 136, n2 = 183, n3 = 242.8, n4 = 316.3, n5 = 402.9, n6 = 499.1, n7 = 599.1, n8 = 695.2
I came up with this code, but it seems to require some (or many) adjustments.
function tN = bacteriaGrowth(n, alpha, K,N)
n = 0;
tN = 0;
tN_after = 0;
while n < N < K
tN = n(t) * (1 + (alpha*(1-(n(t)/K))))
n = n + 1;
tN_after = tN;
fprintf('%.4f\n', tN_after)
end
0 comentarios
Respuesta aceptada
Walter Roberson
el 6 de Ag. de 2016
You are not changing t in your loop, so you would not be able to output the time.
while n < N < K
is the same as
while ((n < N) < K)
the first expression n < N will return either false, 0, or true, 1, so you would be checking either 0 < K or 1 < K . It is unlikely that is what you mean.
Your equation models an exponential growth. Exponential growth requires that there be something to start with, but you are initializing your population size to 0, and exponential growth from 0 is always going to remain 0.
What is the point of having parameters to a function, such as n, if you are going to overwrite them in the function without paying attention to them?
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!