How to Solve recurrence equation

How to solve the recurrence equation
h[n]=-0.36*h[n-2]+1.2*h[n-1]

1 Comment

Thanks a lot @Star Strider. This helped me a lot to learn. I am very new to the matlab coding.

Sign in to comment.

 Accepted Answer

Try something like this —
h(1) = rand; % Initial Condition
h(2) = rand; % Initial Condition
N = 50;
for n = 3:N
h(n)=-0.36*h(n-2)+1.2*h(n-1);
end
nv = 1:N;
figure
plot(nv, h, '.-')
grid
xlabel('n')
ylabel('h')
Experiment to get the result you want.
.

14 Comments

"solving" a recurrance equation means coming up with a non-recursive closed form expression for the function.
Jiby
Jiby on 17 Sep 2022
Edited: Jiby on 17 Sep 2022
Thank you! This helps me a lot!!
So if the question is to plot for M-file recur with T=0.4, compute the approximation to y(t) for the equation
y[n-2][1+T^2-2T]+y[n-1][2T-2]+y[n]=0
y[n-2][-0.36]+y[n-1][1.2]=y[n]
As always, my pleasure!
That appears to me to be correct.
T = 0.4;
y(1) = 0.95;
y(2) = 0.35;
N = 50;
coeffv = [(1+T^2-2*T) (2*T-2)]
coeffv = 1×2
0.3600 -1.2000
for n = 3:N
y(n) = -y(n-2)*(1+T^2-2*T)-y(n-1)*(2*T-2);
end
nv = 1:N;
figure
plot(nv, y, '.-')
grid
xlabel('n')
ylabel('h')
.
I have trying to solve the 2.27 c,d,e in matlab [attached screenshot].
y(1) = 0.95;
y(2) = 0.35;
From where do we get these two values?
Star Strider
Star Strider on 17 Sep 2022
Edited: Star Strider on 17 Sep 2022
The current code appears to be (c). Part (d) is the same with a different value for ‘T’, and the analytic solution for the differential equation is given in (a), so you simply need to code it and run all of them together.
Use the hold function to plot them all on the same axes, since that seems to be required.
EDIT — I did not previously see your last Comment. Those are random numbers, since I needed something to define the first two elements of ‘y’ in order to test my code.
syms h
T = 0.4; % T = 0.4 sec
h(1) = 0.95; %h(1)=arbitrary value
h(2) = 0.35; %h(2)=arbitrary value
N = 10;
coeff1 = [(1+T^2-2*T) (2*T-2)]
for n = 3:N
h(n) = -h(n-2)*(1+T^2-2*T)-h(n-1)*(2*T-2);
end
h
nv = 1:N;
figure
plot(nv, h, '.-')
grid
title ('T = 0.4 Sec')
xlabel('n')
ylabel('h')
syms h1
R = 0.1; % T2 = R = 0.1 sec
h1(1) = 0.95; %h(1)=arbitrary value
h1(2) = 0.35; %h(2)=arbitrary value
N = 10;
coeff2 = [(1+R^2-2*R) (2*R-2)]
for n = 3:N
h1(n) = -h1(n-2)*(1+R^2-2*R)-h1(n-1)*(2*R-2);
end
h1
nv = 1:N;
figure
plot(nv, h1, '.-')
grid
title ('T = 0.1 Sec')
xlabel('n')
ylabel('h')
t = linspace(1, 10);
y = (2 .* exp(-t))+ (t .*exp(-t));
figure(1)
plot(t, x)
grid
title ('Exponential Graph')
xlabel('t')
ylabel('y')
plot(t, x)
hold on
plot(nv, h, '.-')
hold on
plot(nv, h1, '.-')
hold off
legend('Exponential','T 0.4 Sec','T 0.1 Sec')
I did not run any of that, howewver it appears to be correct.
Torsten
Torsten on 18 Sep 2022
Edited: Torsten on 18 Sep 2022
h(1) = y(0) and h(2) must be an approximation to y(T) obtained from Euler's method.
Maybe h(2) = y(0) + T*y'(0).
They are not arbitrary values.
And the t-values at which the y values are computed are not nv = 1:N, but nv = T*(0:N-1).
h(n) = -h(n-2)*(1+T^2-2*T)-h(n-1)*(2*T-2);
h(1) = y(0) and h(2) must be an approximation to y(T) obtained from Euler's method.
Maybe h(2) = y(0) + T*y'(0).
could you please explain the statement with the above h(n)
h(n) is an approximation of the solution of your differential equation y at t = (n-1)*T.
Thus h(1) = y(t=0) and h(2) = y(t=T).
An approximation for h(2) = y(T) with Euler forward is y(T) = y(0) + T*y'(0) = 2 + T*(-1).
yexact = @(t) (2+t).*exp(-t);
T = 0.4; % T = 0.4 sec
h(1) = 2; %h(1)=y(0)
h(2) = 2+T*(-1); %h(2)=y(0)+T*y'(0)
N = 10/T+1;
for n = 3:N
h(n) = -h(n-2)*(1+T^2-2*T)-h(n-1)*(2*T-2);
end
nv = 0:T:(N-1)*T;
figure
hold on
plot(nv, h, '.-')
plot(nv, yexact(nv))
hold off
grid
title ('T = 0.4 Sec')
xlabel('n')
ylabel('h')
Jiby
Jiby on 18 Sep 2022
Edited: Jiby on 18 Sep 2022
Thanks a lot for your response @Torsten
When i tried plotting this, it showed h & nv doesnot have same vector length with 101*26 respectively
Torsten
Torsten on 18 Sep 2022
Edited: Torsten on 18 Sep 2022
I don't know your code, but as you can see above, plotting is possible.
nv and h are both vectors of size 1 x (10/T+1) (1 x 26 for T = 0.4).
101 smells like T = 0.1 while 26 smells like T = 0.4. I think you somehow mixed the two stepsizes for T in the h and nv arrays.
I tried with T=0.1 without clearing workspace.
Thank you @Torsten

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Asked:

on 17 Sep 2022

Commented:

on 18 Sep 2022

Community Treasure Hunt

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

Start Hunting!