have to make a function and return an answer of how long before it passes a certain point. does not work. help needed.

13 visualizaciones (últimos 30 días)
i have to make a function that look like this
n(t)=n(t-1)*(1+alpha*(1-n(t-1)/K))
and know when n(t) surpasses a certain point,N.
my code looks like this:
function tN = bacteriaGrowth(n0,alpha,K,N)
for t=1:20
n(t)=n(t-1)*(1+alpha*(1-n(t-1)/K));
n(0)=n0;
n(1)=n0*(1+alpha*(1-n0/K));
end
if n(t)>=N
tN=t
end
i just get an error in line 4, that doesn't explain what is wrong. help would be very appreciated.
  1 comentario
Stephen23
Stephen23 el 26 de Feb. de 2017
Editada: Stephen23 el 26 de Feb. de 2017
"i just get an error in line 4, that doesn't explain what is wrong"
And that error is obviously so useless you want to keep it a secret. Please actually try to help us by giving editing your question and giving the complete error message. This means all of the red text.
When I tried running your code I got this error:
??? Undefined function or variable "n".
Error in ==> bacteriaGrowth at 3
n(t)=n(t-1)*(1+alpha*(1-n(t-1)/K));
Which is obviously not the same error that you are getting because you wrote that your error "doesn't explain what is wrong", whereas the error that I get does. And an internet search of that error message gives a thousand explanations of how to fix it.

Iniciar sesión para comentar.

Respuesta aceptada

Jan
Jan el 26 de Feb. de 2017
Please consider Stephen's advice and explain, what you observe and what you exactly want to calculate. I try it with a bold guess:
You have to start with n0 before you access n(t-1):
function tN = bacteriaGrowth(n0, alpha, K, N)
n = n0 * (1 + alpha * (1 - n0 / K));
t = 0;
while t < 20 && n < N
for t = 2:20 % Start at 2, because n(1) is defined already
n = n * (1 + alpha * (1 - n / K));
t = t + 1;
end
tN = t;
If you want to reply tN only, storing n in a vector is not useful.
Does this help you? If not, please explain the problem by editing the question, not in the sections for answers or comments. Thanks.
  1 comentario
jonas paludan
jonas paludan el 27 de Feb. de 2017
thanks for the help. enden up adding some small stuff but your answer helped a lot. my final code: function tN = bacteriaGrowth(n0,alpha,K,N)
n = n0*(1+alpha*(1-n0/K)); t= 0;
while t<100000 && n < N
for t=2:100000
n=n*(1+alpha*(1-n/K)); if n>N
break
end
end
end
if n0>N
tN=0;
else
tN=t;
end
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by