iterative method for matrix inversion

hi all iam trying to apply some iterative method to have matrix inverse the method steps are
the iterations i do is not working
AA= [ 2 4 -3 1 0 5 -7 8;
3 2 10 -4 -1 -6 4 1;
9 7 3 2 0 0 -4 2;
6 4 0 -1 -1 3 10 5;
5 2 -3 -7 -5 4 8 -8];
bb= [38;-20;39;-16;-30];
z0=[ 2 0 -1 2 0 0 -3 1]';
z(:,1)=z0;
tol=1.e-3;
error=2*tol;
if abs(bb-AA*z0)<error xxsol=z0;
else
while (error>tol)
k=1;
for i=1:m
zk=z(:,k);
numerator= (bb(i)-(AA(i,:)*zk))*S(:,i);
denumerator=norm(AA(i,:),1);
secondterm= numerator/denumerator;
% x= zk+ secondterm;
x(:,i)=zk+ secondterm;
error=abs(bb-A*x(:,i));
if error<tol xxsol=x(:,i);
else
u=sum (x(:,i));
z(:,i+1)= 1/m * u;
error=abs(bb-AA*z(:,i+1));
end
k=k+1;
end
end
end
can any body check what is wrong thanks

1 Commento

It seems like you have a reason to assume, that something is going wrong. So why don't you share the important information with the readers?

Accedi per commentare.

Risposte (1)

Jan
Jan il 23 Gen 2018
Modificato: Jan il 23 Gen 2018
Start with applying an auto-indentation: Ctrl-A Ctrl-I. This improves the readability.
k = 1
for i = 1:m
zk = z(:,k);
...
k = k + 1;
end
could be simplified to:
for k = 1:m
zk = z(:,k);
...
end
This looks fancy:
numerator= (bb(i)-(AA(i,:)*zk))*S(:,i);
denumerator=norm(AA(i,:),1);
secondterm= numerator/denumerator;
Do you mean "denominator"? Anyway, prefer simpler code:
t = (bb(i) - AA(i,:) * zk) * S(:,i) / norm(AA(i,:), 1);
If "error<tol" is reached, shouldn't the loop be left by using a break? Currently the loop is not stop in the case of success.
"error" is an important Matlab function. Do not shadow it by using the name for a variable.
In if abs(bb-AA*z0)<error, are you aware, that bb-AA*z0 is a vector? In the text of the question you find clearly, that the norm of (bb-AA*z0) is wanted. If the argument of an if command is an array, this is applied internally
if all(arg(:)) && ~isempty(arg)

Richiesto:

il 22 Gen 2018

Modificato:

Jan
il 23 Gen 2018

Community Treasure Hunt

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

Start Hunting!

Translated by