recurrent solution of difference equation

1 次查看(过去 30 天)
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

采纳的回答

Sriram Tadavarty
Sriram Tadavarty 2020-3-18
编辑:Sriram Tadavarty 2020-3-18
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 个评论
John D'Errico
John D'Errico 2020-3-18
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 2020-3-18
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by