while loop: how to define a function when using equation with several variables?
Show older comments
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
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!