recurrent solution of difference equation

2 Ansichten (letzte 30 Tage)
Rastislav Haska
Rastislav Haska am 18 Mär. 2020
Kommentiert: Rastislav Haska am 18 Mär. 2020
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

Akzeptierte Antwort

Sriram Tadavarty
Sriram Tadavarty am 18 Mär. 2020
Bearbeitet: Sriram Tadavarty am 18 Mär. 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 Kommentare
John D'Errico
John D'Errico am 18 Mär. 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 am 18 Mär. 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Mathematics finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by