How to do a matrix from a loop for several values of a constant?

So I am trying to have a 4x10 matrix called Y by using a for loop for each value of a. I just don't know how to do it. I know I could do this manually by defining y1 with a=.5, y2 with a=.9,...y4 with a=1.1 and running the loop for each one but I want the computer do it all. This for solving a more complex problem. Thanks in advance!
e=rand(1,10);
a=[.5,.9,1,1.1]
y=ones(1,10);
for i=2:10;
y(i)=a*y(i-1)+e(i-1); %I want a matrix of Y which contains this loop for each value
of a stated above.
end

Answers (1)

Hello Germán Loredo! Try this:
clear all;
close all;
clc;
e=rand(1,10)
a=[.5,.9,1,1.1]
y=ones(4,10);
for i=1:10;
for k=1:4
y(k,i)=a(k)*y(i)+e(i); %I want a matrix of Y which contains this loop for each value
end
end

10 Comments

Does not work. I tried this also. Those values are wrong because if you do it "manually" you'll have other values. Check it out!
Can you give more information about the matrix y? what each element are?
that " y(i-1) " is correct?
For example,a=.5 then y(2)=a*1-e(1) then y(3)=a*y(2)-e(2) ... y(10)=a+y(9)-e(9) All this results should be the 10 columns of the first line of Y. Then you do the same but for a=.9 and that should be the 10 columns of line 2 of Y. ... Finally, for a=1.1 you do the same and that should be the 10 columns of matrix Y.
By theory, when a>=1 y(n)should be very high when n goes very large. As when a<1 E(y(n)) should converge to a number.
I have changed "y(k,i)=a(k)*y(i)+e(i)" to "y(k,i)=a(k)*y(i-1)-e(i-1);".
e=rand(1,10);
a=[.5,.9,1,1.1]
y=ones(4,10);
for i=2:10;
for k=1:4
y(k,i)=a(k)*y(i-1)-e(i-1); %I want a matrix of Y which contains this loop for each value
end
end
Still wrong, it makes no sense because, for example, when a=1.1 and we know e is always positive,
y(2)=1.1*y(1)+e=1.1*1+e(1)
y(3)=1.1*y(2)+e(2)=(1.1^2)*1+1.1*e(1)+e(2)
y(4)=(1.1^2)*1+(1.1^2)*e(1)+1.1*e(2)+e(1).
As you can see Y(n) is always bigger and bigger when n increases. Your results do not show that.
But check this out, I think I just solve it (we were needed to put dimensions to some variables).
e=rand(4,1000);
a=[.5,.9,1,1.1];
y=ones(4,1000);
for n=1:4
for i=2:1000;
y(n,i)=a(n)*y(n,i-1)+e(n,i-1);
end
end
This result goes along with: When a>=1, E(y)should go to infinite when n goes infinite. As when a<1 E(y) converge to a number when n goes to infinity.

This question is closed.

Asked:

on 9 Sep 2017

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!