100% working code for Steepest Descent iterative algo for finding solution of Ax=b
%%100_working code for Steepest Descent iterative algo for finding
%%solution of Ax=b, where A is symmetric Positive definite
%%written by vikram bhayyA on 18-6-26 ~14:37-14:42
%%general.
clc
clear all
A=input('enter a Symmetric Positive Definite matrix:');
%A=[4,1,1;1,3,-1;1,-1,3]; %has to be symmetric Positve definite
[m,n]=size(A);
n
b=input('enter the output vector of size n,1:');
%b=[6,3 3]';
%x0=zeros(n,1);
x0=[1 -1 0]';
%x0=[0 0 0]' ; % initial guess
%initialize
r0=b-A*x0; %residual
if norm(r0)<0.01
display('solution of Ax=b is')
x0
else
%p0=r0; %search direction
k=1;
while(k)
alpha0=(r0'*r0)/(r0'*A*r0); %step size
x1=x0+alpha0*r0; %update x1
r1=b-(A*x1); % update r1
if norm(r1)<0.01
k=0;
else
k=k+1;
end
r0=r1;
x0=x1;
end
display('solution of Ax=b is')
x1
end
In the command window one can see this
enter a Symmetric Positive Definite matrix:[1 0 0;0 2 0; 0 0 3]
n =
3
enter the output vector of size n,1:[1 4 12]'
x1 =
1.0000
1.9980
3.9973
To crosscheck, if the solution is correct:
>> A*x1
ans =
1.0000
3.9960
11.9919
n =
3
enter the output vector of size n,1:[1 4 12]'
x1 =
1.0000
1.9980
3.9973
To crosscheck, if the solution is correct:
>> A*x1
ans =
1.0000
3.9960
11.9919
కామెంట్లు
కామెంట్ను పోస్ట్ చేయండి
దయచేసి మీ సలహాలను సూచనలను స్పష్టంగా పేర్కొనగలరు. plz see that ur comments are 'acceptable' in a value based society.