Info

This question is closed. Reopen it to edit or answer.

How to use the result from a step as an input for the next step?

1 view (last 30 days)
Dear all,
Considering the following example, I would like to ask 2 questions:
function example1
Yinitial=zeros(3,1);
A=rand(3,3);
B=rand(3,5);
for nt=1:5
F1=B(:,nt);
F2=Yinitial;
F=F1+F2;
Y=A\F;
end
Yinital=Y;
end
1. Do you know how can I use the result vector Y1={a1,b1,c1} from nt=1 as an input ( initial condition) for the next step, nt=2? Then again the result from nt=2, Y2={a2,b2,c2} as an initial for nt=3 and so on.
2. How can I store the result vectors Ynt into the common two dimensional array: Z={res.Y1,res. Y2, ....,res.Ynt} ?
Thank you in advance!
Regards
  2 Comments
Jan
Jan on 21 Apr 2012
What is "a1", "b1" and "c1"? And what is "res.Y1"?
I guess, that you want to move the "Yinital=Y" line inside the FOR loop.
D.Chehov
D.Chehov on 22 Apr 2012
for nt=1, the vector with the results:
res.Y1=
[ a1= -2.5775
b1= 3.0365
c1= 1.0462],
a1,b1,c1 are only symbols in the explanation to the above example. res Y1, res. Y2,...,res. Y5 are the results from each step nt=1:5.
I woulk like to use the result from the step nt-1, as an input for the next step nt.....
the moving of "Yinital=Y" line inside the FOR loop doesn't do this

Answers (1)

Andrei Bobrov
Andrei Bobrov on 21 Apr 2012
[EDIT]
A=rand(3,3);
B=rand(3,5);
Y = zeros(size(B));
n = 1;
for nt=1:5
if nt > 1, n = nt - 1; end
Y(:,nt)=A\(B(:,nt) + Y(:,n));
end
added after Chervov's comments
Y = zeros(size(B,1),1);
n = 1;
for nt=1:5
Y=A\(B(:,nt) + Y);
end
  1 Comment
D.Chehov
D.Chehov on 22 Apr 2012
thank you for the idea, but in this case the result is:
Y =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
Y =
1.7560 0 0 0 0
-1.8530 0 0 0 0
-0.5933 0 0 0 0
Y =
1.7560 1.4655 0 0 0
-1.8530 -1.2227 0 0 0
-0.5933 0.2546 0 0 0
Y =
1.7560 1.4655 0.7791 0 0
-1.8530 -1.2227 -0.2028 0 0
-0.5933 0.2546 -0.5875 0 0
Y =
1.7560 1.4655 0.7791 -0.2933 0
-1.8530 -1.2227 -0.2028 1.5270 0
-0.5933 0.2546 -0.5875 0.5178 0
Y =
1.7560 1.4655 0.7791 -0.2933 0.9684
-1.8530 -1.2227 -0.2028 1.5270 -0.4400
-0.5933 0.2546 -0.5875 0.5178 0.0072
i.e. for every step I obtain the array Y=size (B), in fact I need only the last one, that contain all results.
Thank you very much for your help, I appreciate it!

Community Treasure Hunt

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

Start Hunting!