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.

 Accepted Answer

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 Comment

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

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!