Calculate recursive inhomogenous equation in Matlab

I want to calculate the following recursive inhomogenous equation:
x(n+2) - 5x(n+1) + 6x(n) = 4n+1
x(0)=1
x(1)=2
How do I write in Matlab, to solve for example x(30)?

Answers (4)

You can try to call into MuPAD to construct a recurrence and solve() it. See http://www.mathworks.com/help/symbolic/mupad_ref/rec.html for the MuPAD part. You would use the Symbolic Toolbox and
evalin(symengine, ....)
to access MuPAD.
This computes the value of x(n):
x0 = 1;
xn = 2;
for k = 2:n
t = xn;
xn = 5*xn-6*x0+4*k-7;
x0 = t;
end
The variable 'xn' gives the value of x(n). For n = 30 this gives:
xn = 308832403174741
However, this is about as high as n can go to give accurate values for x(n). Beyond that, matlab's 'double' format begins to suffer round-off errors. For larger values of n you need to use the symbolic toolbox or to follow Walter's advice.

1 Comment

What if you have the function:
xn − 10x(n−1) + 50x(n−2) = 0
n = 2, 3, ...
x0 = 0
x1 = 10
How would you write? I tried the way as you but I don't know how to do when n starts at 2.

Sign in to comment.

Max
Max on 9 Feb 2014
Edited: Max on 9 Feb 2014
What if you have the function
x(n+2) 8x(n+1) + 16x(n) = 9n + 3
n = 0, 1, 2, ...
x0 = 0
x1 = 0
Please help how to write in Matlab to calculate x(n). Appreciate fast answer.
The codes for the two iterative procedures you asked about are:
% The first one
n = 16; % <-- Choose the value of n you want
x0 = 0;
xn = 10;
for k = 2:n
t = xn;
xn = 10*xn-50*x0
x0 = t;
end
% The second one
n = 20; % <-- Choose the value of n you want
x0 = 0;
xn = 0;
for k = 2:n
t = xn;
xn = 8*xn-16*x0+9*k-15
x0 = t;
end
Note: There is a shortcut formula for that first one. It is:
x(n) = 2*(5*sqrt(2))^n*sin(n*pi/4)
Using this, you don't need to compute all the x values before x(n). Try comparing its results with those of the above first iteration.

2 Comments

Thanks. Can you please just explain the line "xn = 8*xn-16*x0+9*k-15". Where did the number "-15" come from?
Also, how come the second method for the first equation gives another answer than the iteration method?
On your first question, rewrite
x(n+2) = 8*x(n+1) - 16*x(n) + 9*n + 3
as:
x(n) = 8*x(n-1) - 16*x(n-2) + 9*(n-2) + 3
= 8*x(n-1) - 16*x(n-2) + 9*n - 15
In the for-loop this latter takes the form
xn = 8*xn-16*x0+9*k-15;
For the second question, the difference between the two results is caused by the round-off error in computing sin(n*pi/4). If you used n = 16, it should be exactly zero, but because of the round-off error it returns with the value -4.8986*10^(-16) which is a very tiny error, well within the required accuracy for the 'sin' function. However, it is multiplied by 2*(5*sqrt(2))^16 which is a huge 7.8125*10^13, so the product is -0.0383 instead of exactly zero. Since we know that for this series, all values are actually integers, I should have told you to use matlab's 'round' function to round this "shortcut" answer to its nearest integer, which would indeed be zero here.
As I warned you earlier, there is a limit to the size of n for which these calculations, even with the for-loop method, can give accurate results. Eventually the x-value integers become so large that matlab's 'double' numbers cannot accurately represent them. That happens when numbers beyond 2^53 = 9.007*10^15 occur in the calculations. For larger values of n you need to use something like matlab's symbolic toolbox to get exact answers.

Sign in to comment.

Asked:

Max
on 8 Feb 2014

Commented:

on 10 Feb 2014

Community Treasure Hunt

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

Start Hunting!