Matlab code to perform a loop

1 view (last 30 days)
segun egbekunle
segun egbekunle on 8 Jul 2017
Edited: Walter Roberson on 9 Jul 2017
Please I need matlab code to transform a matrix A(nxn) to another matrix B following the steps shown
function [x]=semitn(A,b,n)
n = size(A,1);
%splitting matrix A into the three matrices L, U and E
L=tril(A,-1);
U = triu(A,1);
D = diag(diag(A));
%obtaining a new matrix Ex= C(1)
P=L+D;
C(1)=P\b;
M(1) =P\U;
I=eye(n,n);
E= I+M(1);
E*x= C(1)
% partitioning matrix Ex= C(1)
A1=E(1,1);
A2=E(1,2:n);
A3=E(2:n,1);
A4=E(2:n,2:n);
x(1)=x(2:n)
b(1)= C(1)(2:n)
F=[A1 A2;A3 A4];
G=[x(1),x(1)];
H=[C(1)(1),b(1)];
F*G=H;
A1* x(1)+A2* x(1)= C(1)(1) eqn (1a)
A3* x(1)+A4* x(1)= b(1) eqn (1b)
From equation 1b we have
A4* x(1)= b(1)
A4 =A(1)
A(1)* x(1)= b(1)
L(1)=tril(A(1),-1);
U(1) = triu(A(1),1);
D(1) = diag(diag(A(1)));
%obtaining a new matrix E(1) x(1)= C(2)
P(1)= L(1)+ D(1);
C(2)= P(1)\ b(1);
M(2) = P(1)\ U(1);
I=eye(n-1,n-1);
E(1)= I+M(2);
E(1)* x(1)= C(2)
% partitioning matrix E(1)* x(1)= C(2)
A1(1)= E(1) (1,1);
A2(1)= E(1)(1,2:n);
A3(1)= E(1)(2:n,1);
A4(1)= E(1)(2:n,2:n);
x(2)= x(1)(2:n)
b(2)= C(2)(2:n)
F(1)=[ A1(1) A2(1); A3(1) A4(1)];
G(1)=[x(2),x(3)];
H(1)=[C(2)(1),b(2)];
F(1)* G(1)= H(1);
A1(1) * x(2)+ A2(1)* x(2)= C(2)(1) eqn (2a)
A3(1) * x(2)+ A4(1)* x(2)= b(2) eqn (2b)
From equation 2b we have
A4(1)* x(2)= b(2)
A4(1) =A(2)
A(2)* x(2)= b(2)
.
.
.
A(n 2) x(n 2) = b(n 2)
A(n-2)* x(n-2)= b(n-2)
L(n-2)=tril(A(n-2),-1);
U(n-2)= triu(A(n-2),1);
D(n-2)= diag(diag(A(n-2)));
%obtaining a new matrix E(n-2)x(n-2)= C(n-1)
P(n-2)= L(n-2)+ D(n-2);
C(n-1)= P(n-2)\ b(n-2);
M(n-1)= P(n-2)\ U(n-2);
I=eye(2,2);
E(n-2)= I+M(n-1);
E(n-2)* x(n-2)= C(n-1)
A1(n-2)= E(n-2)(1,1);
A2(n-2)= E(n-2)(1,2);
A3(n-2)= E(n-2)(2,1);
A4(n-2)= E(n-2)(2,2);
x(2)= x(1)(2:n)
b(2)= C(2)(2:n)
F(n-2)=[ A1(n-2) A2(n-2); A3(n-2) A4(n-2)];
G(n-2)=[x(n-1),x(n)];
H(n-2)=[C(n-1)(1), C(n-1)(2)];
F(n-2)* G(n-2)= H(n-2);
A1(n-2) * x(n-1)+ A2(n-2)* x(n)= C(n-1)(1)eqn ((n-1)a)
A3(n-2) * x(n-1)+ A4(n-2)* x(n)= C(n-1)(2)eqn ((n-1)b)
From equation ((n-1)b) we have
A4(n-2)* x(n)= C(n-1)(2)
x(n)= C(n-1)(2)/ A4(n-2)
perform a back substitution to obtain
x(n-1),,x(1)

Answers (0)

Categories

Find more on Mathematics and Optimization 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!