Hi!
I think this would work:
clear all
close all
%A=[1,1,1;1,-2,2;1,2,-1]
%B=[0;4;2];
A=[2,1,-1,4,2,5;-3,-1,2,5,9,4;-2,1,2,6,6,3;1,2,3,5,4,6;8,9,4,4,1,3;2,3,5,1,9,7]
B=[8;-11;-3;5;7;8]
%A=[1,0,0;1,0,0;1,0,0]
%B=[1;1;1];
% SET UP COUNTERS
COUNTER_AMS=0
COUNTER_DIVISION=0
% GET SIZE OF MATRIX
N=size(A,1);
for k=1:N-1
i_test=k;
CONDITION=0;
SINGULAR_MATRIX=0;
while CONDITION==0
if (A(i_test,k)~=0)
CONDITION=1;
i=i_test;
else
i_test=i_test+1;
COUNTER_AMS=COUNTER_AMS+1;
end
if i_test>N
'ERROR MATRIX NON SINGULAR'
pause
%exit
end
end
% PERMUTE RAW IF i different then k
if i~=k
TEMP=A(i,:);
A(i,:)=A(k,:);
A(k,:)=TEMP;
TEMP=B(i);
B(i)=B(k);
B(k)=TEMP;
end
for j=k+1:N
p_jk=A(j,k)/A(k,k);
COUNTER_DIVISION=COUNTER_DIVISION+1;
A(j,:)=A(j,:)-p_jk*A(k,:);
B(j)=B(j)-p_jk*B(k);
COUNTER_AMS=COUNTER_AMS+4;
end
end
if A(N,N)==0
'MATRIX SINGULAR'
pause
%exit
end
% ALGORITHM OF BACKWARD SUBSTITUTION
X(N)=B(N)/A(N,N);
COUNTER_DIVISION=COUNTER_DIVISION+1;
for i=N-1:-1:1
SUM=0;
for j=i+1:N
SUM=SUM+A(i,j)*X(j);
COUNTER_AMS=COUNTER_AMS+2;
end
X(i)=(B(i)-SUM)/A(i,i);
COUNTER_AMS=COUNTER_AMS+1;
COUNTER_DIVISION=COUNTER_DIVISION+1;
end
X'
COUNTER_AMS
COUNTER_DIVISION
N*(N+1)/2
N^3+N^2-5*N/6
This would work for a matrix, but not for this matrix in the picture. Because equations aren't provided in the proper format. Line 4 has : F3-10=0. So the code would have to move the 10 so that it will be F3=10
Do you know how I could do this? There's another equation where it has to move the 10 to the right.