# Homework Help: Recurrence Relation in Matlab

1. Feb 14, 2014

### emergentecon

1. The problem statement, all variables and given/known data
Evaluate the following series ∑u(n) for n=1 → $\infty$ in which u(n) is not known explicitly but is given in terms of a recurrence relation.
You should stop the summation when u(n) < 10^(-8)
u(n+1) = (u(n-1))^2 + (u(n))2 with u(1) = 0.5, u(2) = 0.6

Note 1:The lecturer realised that the constraint: u(n) < 10^(-8) is an error and has stated that we can simply show that we managed to correctly code the recurrence relation, or solve it with the correct constraint.

Note 2: I am not looking for the answer, simply guidance on how to proceed / what I am doing wrong / not seeing?

2. Relevant equations
u(n+1) = (U(n-1))^2 + (u(n))2
u(1) = 0.5
u(2) = 0.6

3. The attempt at a solution
I have three attempts to this problem.

Attempt 1
I simply used excel to see how the values of u(n) evolved in order to verify any solution obtained in matlab . . . the values are:

0.61000000000000000
0.62210000000000000
0.75910841000000000
0.96325398813272800
1.50410382378633000
3.19018655838228000
12.43961859001160000

Attempt 2 - MATLAB
This implementation looks intuitively incorrect to me - plus the actual results to not correspond with what I attained in Excel.
clc; clear;
x1 = 0.5; x2 = 0.6; x3=0; counter=0;
while x1>10^-8;
counter=counter+1
x3 = x2 + x1;
x4 = x3 + x2;
x1 = x1^2;
x2 = x2^2;
x3
x4
end

Attempt 3 - MATLAB
This implementation looks more correct, but I get an error message? It is clearly missing the summation element, but am more concerned with getting the actual recurrence relation correct at this point.

clc; clear;
y(0) = 0.5; y(1) = 0.6; m = 0;
for m=1:10;
y(m+1)=(y(m))^2+(y(m-1))^2;
end

Error Mesage:
??? Attempted to access (0); index must be a positive integer or logical.

Error in ==> New at 2
y(0) = 0.5; y(1) = 0.6; m = 0;

2. Feb 14, 2014

### dirk_mec1

clc; clear;
y(1) = 0.5; y(2) = 0.6;
for m=2:11;
y(m+1)=(y(m))^2+(y(m-1))^2;
end

3. Feb 14, 2014

### emergentecon

Thanks! But, hmmmm . . . why is that an issue?
Why could m not be -1?
Also the results seem to be different from what I get in excel - could this be a floating-point issue?

Excel:=
0.6100
0.6221
0.7591
0.9633

0.6100
0.7321
0.9081
1.3606

EDIT:Ok my two solutions now agree - had mixed y(1) and y(2) values . . .

Thanks again!

Last edited: Feb 14, 2014
4. Feb 14, 2014

### dirk_mec1

I get in Excel the same results.

5. Feb 14, 2014

### emergentecon

Thanks!

6. Feb 14, 2014

### emergentecon

I am just playing around with the equation now . . . trying to implement a constraint for u(n) < 10^8 {not 10^-8 as originally stated}. Here I would use:

clc; clear;
y(1) = 0.5; y(2) = 0.6;
while y(m)<10^8;
y(m+1)=(y(m))^2+(y(m-1))^2;
end

I have also tried: while y<10^8

Both generate the error that m is an undefined function or variable?
Am curious as to how I might implement this?
I figure I just have a simple syntax issue?

7. Feb 14, 2014

### dirk_mec1

clc; clear;
y(1) = 0.5; y(2) = 0.6;
m=2;
while y(m)<10^8;
y(m+1)=(y(m))^2+(y(m-1))^2;
m=m+1;
end

8. Feb 14, 2014

### emergentecon

Again, thanks, really!
Any chance you could explain why my original approach wouldn't work?

9. Feb 14, 2014

### dirk_mec1

You did not specified what m should be.

10. Feb 14, 2014

### emergentecon

Aaaaaahhhhh, ok . . . yes yes I see. Having removed the 'for m=2:11' etc. etc. Yeah yeah.
Cool, thanks man!