MATLAB: How to update a matrix after iteration in a for loop and then use it in next iteration

matricesupdating matrix

In Matrix A*F(m) = B(m-1), Here, A is known constant co-efficient matrix, F is to be solved for m = 1 to 5, B is a function of F [In other words for m=2, B = function of F(m=1). for m=3, = function of F(m=2)]
Problem –
  1. Unable to perform calculations using for loop (m=1 to 5)
  2. Unable to update matrix B(m) after calculating F (m-1).
Attempts made – If i try to include iterations in for loop, it considers iteration no as row no. or column no.
Please help
clear all; close all; clc;
% Matrix A(m) * Matrix F(m) = Matrix (B(m-1) where matrix F is to be solved
% Matrix A remains constant for all value of m.
% For m= 1, B (m-1) = Matrix B0 = known
% For m =1, Matrix F(m) = F1 is solution
% above soln (F1) is used to obtain Matrix B1 as elements of B are function of F
% Then B1 will give F2 which will be used to calc B2 ans so on
% Finally i need last values of F
% Consider m = 1 to 5 (simple case)
a = 0.01; % constant
A = [1 0 0 0 0;
-a 1 a 0 0;
0 -a 1 a 0;
0 0 -a 1 a ;
0 0 1 -2 1] ; % assigning co-efficet matrix A
rows=5; % defining no. of rows of matric A, F, B
first_val= 2; % this is to assign value to first row of first Matrix B
%for m= 1
B0 = zeros(rows,1); %preallocate space for output
B0 (1) = first_val; % this is to assign value to first row of first matrix B0
for i=2:rows-1 % this step is to assign values to other elements of matrix B0
B0 (i) = 0.01; %formula current value depends on previous
end
B0(rows) = 0; % Now First matrix B0 has been defined B0 = [2; 0.01; 0.01; 0.01; 0]
F1 = A\B0 (:,:); % solving for F1
%end




%for m=2
B1 = arrayfun(@(F1) (2*F1),F1); % using F1 to calculate B1, B = 2F is the function
F2 = A\B1; % solving for F1
%end
%for m=3
B2 = arrayfun(@(F2) (2*F2),F2); % using F2 to calculate B2, B = 2F is the function
F3 = A\B2; % solving for F3
%end
%for m=4
B3 = arrayfun(@(F3) (2*F3),F3); % using F3 to calculate B3, B = 2F is the function
F4 = A\B3; % solving for F4

%end
%for m=5
B4 = arrayfun(@(F4) (2*F4),F4); % using F4 to calculate B4, B = 2F is the function
F5 = A\B4; % solving for F4
B5 = arrayfun(@(F5) (2*F5),F5);
%end
% final output
B = [B0 B1 B2 B3 B4 B5]
F = [F1 F2 F3 F4 F5]

Best Answer

Hello!
If I've understood your question correctly, then I think your main issue is that you are trying to save each iteration of B and F in new variables. You can preallocate space for your final B and F matrices, do your calculations inside a for loop using the same variables at each iteration, and then add the new values to the preallocated results matrices to save them before the next iteration overwrites them.
From what I can tell, this code produces the same results as yours does and should be generalizable based on the number of rows!
% Constants
a = 0.01;
A = [1 0 0 0 0;
-a 1 a 0 0;
0 -a 1 a 0;
0 0 -a 1 a ;
0 0 1 -2 1] ;
rows=5;
first_val= 2;
% Making Bx (version which is used in the for loop)
Bx = zeros(rows,1);
Bx(1) = first_val;
for i=2:rows-1
Bx (i) = 0.01;
end
Bx(rows) = 0;
% Creating Return Values
B = [B0 zeros(rows, rows)];
F = zeros(rows, rows);
% Performing calculations (Fx is the version of F that changes)
for m = 1:rows
Fx = A\Bx (:,:);
Bx = arrayfun(@(Fx) (2*Fx),Fx);
B(:, m+1) = Bx;
F(:, m) = Fx;
end
% final output
% B == [B0 B1 B2 B3 B4 B5]
% F == [F1 F2 F3 F4 F5]