sum of a series

Hi,
Can someone please help me to write the following code using a summation
for i = 1:1000
v(i) =y(i) - a0- a1*y(i-1)-a2*y(i-2);
end
Thanks.

 Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 19 de Mzo. de 2013

1 voto

a0=1;
a1=2;
a2=1;
y=rand(1,1000) % your data
coeff=[-a2 -a1 1];
for ii = 3:1000
v(ii) =sum(y(ii-2:ii).* coeff)-a0;
end

Más respuestas (1)

Youssef  Khmou
Youssef Khmou el 18 de Mzo. de 2013
Editada: Youssef Khmou el 18 de Mzo. de 2013

1 voto

hi, what is Y[nT]? random variable?
N=1000;
a0=2.36;
a1=4.56;
a2=5.57;
y=randn(N,1);
v=zeros(N,1);
v(1:2)=y(1:2); % Optional, take is as initial condition
% Using Loops
for n=3:N
v(n)=y(n)-a0-(a1*y(n-1))-(a2*y(n-2));
end
figure, plot(y), hold on, plot(v,'r'), hold off
% Vectorization
Y1=zeros(size(y));
Y1(2:end)=y(1:end-1);
Y2=zeros(size(y));
Y2(3:end)=y(1:end-2);
V=y-a0-a1*Y1-a2*Y2;
% Comparaison
figure, plot(v), hold , plot(V,'r'), hold off

5 comentarios

dav
dav el 18 de Mzo. de 2013
thank for your reply. However, I need to write this using "sum( .. ) " in matlab. and I call y(i), y(i-1).... from a data set.
Youssef  Khmou
Youssef Khmou el 18 de Mzo. de 2013
Editada: Youssef Khmou el 18 de Mzo. de 2013
sorry, i was completing the code....look at the second part..... it is translation of the loop you posted
dav
dav el 18 de Mzo. de 2013
I am sorry, may be my question wasn't clear...
my ys could grow like
v(i) =y(i) - a0- a1*y(i-1)-a2*y(i-2)-a3*y(i-3)-...-ap*y(i-p)
so I need to write it like when you use a summation notation in math.
Thanks
dav
dav el 19 de Mzo. de 2013
I figured it out. Thanks for your time.
Youssef  Khmou
Youssef Khmou el 19 de Mzo. de 2013
ok

Iniciar sesión para comentar.

Categorías

Más información sobre Mathematics en Centro de ayuda y File Exchange.

Preguntada:

dav
el 18 de Mzo. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by