Solving Recurrence Relations in Matlab: A Homework Challenge

Click For Summary
The discussion revolves around solving a recurrence relation in MATLAB to evaluate the series ∑u(n) for n=1 to ∞, where u(n) is defined recursively. The initial attempts included using Excel for verification and MATLAB for coding the recurrence, but issues arose with incorrect implementations and error messages. The lecturer clarified that the stopping condition for the summation was misstated, allowing for a focus on correctly coding the recurrence relation instead. After several iterations, the user resolved discrepancies between MATLAB and Excel results by correcting the initial values and syntax errors in the code. The conversation highlights common pitfalls in programming recurrence relations and emphasizes the importance of accurate variable indexing and initialization.
emergentecon
Messages
57
Reaction score
0

Homework Statement


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 realized 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?

Homework Equations


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


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;
 
Physics news on Phys.org
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
 
  • Like
Likes 1 person
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

Matlab(your solution):=
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:
I get in Excel the same results.
 
  • Like
Likes 1 person
Thanks!
 
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?
 
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
 
  • Like
Likes 1 person
Again, thanks, really!
Any chance you could explain why my original approach wouldn't work?
 
You did not specified what m should be.
 
  • Like
Likes 1 person
  • #10
Aaaaaahhhhh, ok . . . yes yes I see. Having removed the 'for m=2:11' etc. etc. Yeah yeah.
Cool, thanks man!
 

Similar threads

  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
8
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
6
Views
2K
Replies
10
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K