How do I evaluate the inifinite sum using a while loop?

I need to evaluate the following series:
where u(n) is given by the recurrence relation:
The first and second terms are given as 0.5 and 0.6 respectively. The loop needs to end when un > 1e-8. The problem I have is that it seems that this series will never go below 0.5 so why even limit it to such a small number? Obviously the loop will just go on ad infinitum?
Anyway this is my code:
u(1) = 0.5;
u(2) = 0.6;
S = u;
n = 1;
while (abs(u(n)) >= 1e-8)
n = n + 1;
u(n+1) = u(n-1)^2 + u(n)^2;
S(n) = u(n) + S(n-1);
end
disp(S)
The first 6 cumulative terms I get with this code are 0.5; 1.1; 1.71; 2.4421; 3.35017; 4.71073 (in the array I defined as S), but then it rapidly moves up to infinity. I just want some confirmation on my code and whether I'm not misinterpreting something.

 Respuesta aceptada

Torsten
Torsten el 16 de Feb. de 2016
Your argument is correct.
Here is your modified code:
u(1) = 0.5;
u(2) = 0.6;
S(1) = u(1);
S(2) = u(1) + u(2);
n = 2;
while (abs(u(n)) >= 1e-8)
n = n + 1;
u(n) = u(n-2)^2 + u(n-1)^2;
S(n) = u(n) + S(n-1);
end
disp(S)
Best wishes
Torsten.

1 comentario

Ah, thanks for the response and the feedback on the code. I was wondering if my sum array was logically defined.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 16 de Feb. de 2016

Comentada:

el 16 de Feb. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by