How to update a matrix after iteration in a for loop and then use it in next iteration
Show older comments
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 -
- Unable to perform calculations using for loop (m=1 to 5)
- 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]
Accepted Answer
More Answers (0)
Categories
Find more on Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!