recurrent solution of difference equation

1 view (last 30 days)
Hello,
We were tasked with solving the difference equation with recurrent solution
difference equation u(k) - 0.8u(k-1) - 0.16u(k-2) = e(k)
with initial conditions: e(k) = 0.5 sin2k for k ≥ 0
u(-1) = u(-2) = 0
I solve this on paper, beacuse we must first solve on paper then verify in MATLAB and I have problem to write a code for that. I tried to write some code, but my results are not the same from Matlab and on paper.
My code:
clc;
clear all;
close all;
n=15;
u=zeros(15,1);
u(1:2)=[0.017,0.0484]
for k=3:n
u(k)=0.5*sin(k*2)+0.8*u(k-1)+0.16*u(k-2)
end
So, can anyone help me with code?
Thank You in advance

Accepted Answer

Sriram Tadavarty
Sriram Tadavarty on 18 Mar 2020
Edited: Sriram Tadavarty on 18 Mar 2020
Hi Rasistlav,
You are in the correct way itself. Just use sind instead of sin function. Since, the values are treated as degrees, use sind function.
You can even replace the starting values with below code and use them directly. The complete modifications comprise to the code below
u(1) = 0.5*sind(2);
u(2) = 0.5*sind(2*2)+0.8*u(1);
for k=3:n
u(k)=0.5*sind(k*2)+0.8*u(k-1)+0.16*u(k-2)
end
Hope this helps.
Regards,
Sriram
  2 Comments
John D'Errico
John D'Errico on 18 Mar 2020
A good catch here, in that e(k) uses sin as a function of degrees, not as radians which is the default for sin(x). But a quick check shows that this must be so to make e(1), and therefore u(1) to be the indicated value.
The advice to use the correct values for u(1) and u(2) is also important, because when you use only 3 significant digit approximations to the initial values, those small errors can accumulate, and in some cases, significantly magnified. As it turns out for this particular difference equation the coefficients are not such that they will tend to amplify small errors in the lower bits. So the problem must lie mainly in the forcing term, thus e(k).
Rastislav Haska
Rastislav Haska on 18 Mar 2020
Hello Sriram,
it works now when I put d on sin, results are the same as on a paper, thank You very much.
Regard,
Rastislav

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!