How to solve difference equation in MATLAB

How to solve the difference equation for
yn+1 =5/2 yn +yn-1 ,y0 =y1 =1 in terms of the roots of its characteristic equation in MATLAB ?

 Accepted Answer

An alternative is to use filter()
a = [1 -5/2 -1];
b = 0;
ic = [1 1];
n = 50; % 50 terms
y1 = [ic(1) filter(b, a, ones(1, n-1), ic)];

3 Comments

Thanks for your response.It gives the following error "Unrecognized function or variable 'Y'."
Sorry! There was a typo. Try again.
Thank you for your help.After finding y1 according to your code,I am trying to check the behavior of the sequence {yn} as n → infinity based on following code but it doesn't look right.
clear all
close all
%function for fixed point iteration
yn=@(y1,y0) (5/2)*y1+y0;
%all initial guess
yy0=1; yy1=1;
y_val(1)=yy0; y_val(2)=yy1;
cnt=2; tt(1)=0;tt(2)=1;
%loop for all y
for i=1:10
cnt=cnt+1;
y_val(cnt)=yn(yy1,yy0);
yy0=yy1; yy1=y_val(cnt);
tt(cnt)=cnt-1;
end
%plotting the result
plot(tt,y_val)
xlabel('iterations')
ylabel('y_n')
title('y_n vs. iteration plot')
fprintf('The solution is diverging and tends to infinity at t tends to infinite.\n')

Sign in to comment.

More Answers (2)

n = 50 ;
y = zeros(1,n) ;
y(1:2) = 1 ;
for i = 2:n-1
y(i+1) =5/2*y(i) +y(i-1) ;
end

3 Comments

Thank you for your response.I am trying to check the behavior of the sequence {yn} as n → infinity based on your code.It gives me error.
No error in the KSSV posted.
n = 50 ;
y = zeros(1,n) ;
y(1:2) = 1 ;
for i = 2:n-1
y(i+1) =5/2*y(i) +y(i-1) ;
end
disp(y(end-4:end))
1.0e+21 * 0.1255 0.3577 1.0198 2.9073 8.2881
You cannot, of course, run this out to infinity.
There are no negative coefficients, and no coefficients with absolute value less than one, and the initial values are positive. Each value is at least 5/2 times the previous one, so a lower bound would be (5/2)^(n-1) and therefore the bound to infinity is infinite

Sign in to comment.

a = [1 -5/2 -1];
b = 0;
ic = [1 1];
n = 50; % 50 terms
y1 = [ic(1) filter(b, a, ones(1, n-1), ic)];

Community Treasure Hunt

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

Start Hunting!