21 Eng 143
21 Eng 143
i. a = 1, c=1
a. Bisection method (Code)
`2
disp(T')
disp('---------------------------------------------------
------------')
Output
%a=1
% Secant Method
clear all
syms x % defining variable x
format short
% inputs
f = 4*sin(exp(-2*x)+8*x+2);
x0 = 2;
x1 = 2.2;
`3
TOL = 0.00001;
% first iteration
y0 = subs(f,x0); % substituting
y1 = subs(f,x1); % substituting
x2 = x1-((x1-x0)/(y1-y0))*y1; % iterative formula for the
first iteration
n = 1; % initializing iteration number
y(n) = x2; % storing iterations
d(n) = abs(x2-x1); % gap between first iteration (x2) and
initial guess(x1)
I(n) = n; % iteration number
% other iterations
while abs(x2-x1) >= TOL
x0 = x1; % updating iteration
x1 = x2; % updating iteration
y0 = subs(f,x0); % substituting
y1 = subs(f,x1); % substituting
x2 = x1-((x1-x0)/(y1-y0))*y1; % iterative formula for
the next iteration
n = n+1; % updating iteration number
y(n) = x2; % storing iterations
d(n) = abs(x2-x1); % gap between new iteration (x2)
and previous iteration (x1)
I(n) = n; % iteration number
end
% display outputs
T = table(double(I'),double(y'),double(d')); % double
for numerical values
T.Properties.VariableNames = {'Iteration_Number'
'Iteration' 'Gap'} % colomn names
fprintf('Approximate Root = %0.10f\n',double(y(n)));
fprintf('Number of iterations =%3.0f\n',n);
`4
Output
%a=1
%Newton's Method
clear all
syms x % defining variable x
format long
% inputs
f = 4*sin(exp(-2*x)+8*x+2);
x0 = 2;
TOL = 0.00001;
df = diff(f,x); % derivative
% first iteration
x1 = x0 - subs(f,x0)/subs(df,x0); % iterative formula for
the first iteration
n = 1; % iteration numbers
y(n) = x1; % storing iterations
d(n) = abs(x1-x0);
I(n) = n; % iteration number
% Other iterations
while abs(x1-x0) >= TOL % for stopping criterion
x0 = x1; % updating iteration
x1 = x0 - subs(f,x0)/subs(df,x0); % iterative formula
for the next iteration
n = n+1; % updating iteration number
y(n) = x1; % storing iterations
d(n) = abs(x1-x0); % gap between new iteration (x1)
and previous iteration (x0)
I(n) = n; % iteration number
`5
end
% output
T = table(double(I'),double(y'),double(d')); % double -
for numerical values
T.Properties.VariableNames = {'Iteration_Number'
'Iteration' 'Gap'} % colomn names
fprintf('Approximate Root = %0.10f\n',double(y(n)))
fprintf('Number of iterations =%3.0f\n',n)
Output
Advantages
This method is simple and it does not require complex calculations.
This method guaranteed that it converge to a root if the function is continuous and
changes sign over the interval.
Disadvantages
In this method it converges to a root relatively slow than some other methods.
This method is not applicable for functions with multiple roots.
Secant method
Advantages
This method converges faster than bisection method.
This is applicable for functions that are not continuous.
`6
Disadvantages
This method does not converge if the initial guesses are not chosen approximately.
This method does not provide information about the multiplicity of the root.
Newton’s method
Advantages
This method is faster than other methods, especially when the initial guess is close to
the actual value.
Information about the multiplicity of the root can be obtained using this method.
Question 2
i. In Newton’s method convergence to a root is highly depend on the initial guess and
the behavior of the method can be significantly vary with respect to the choice.
This method exhibits quadratic convergence near a simple root when the guess is
close to the real value, which means in each iteration it doubles the correct digits. It is
needed to analyze the derivative of the function in order to find the approximation for
the root.
ii. (a)
.
(b)
% Newton's Method
clear all
syms x % defining variable x
format long
% inputs
f = input('Enter the function: ');
`7
x0 = input('Enter the initial guess x0: ');
TOL = input('Enter the tolerance: ');
df = diff(f,x); % derivative
% first iteration
x1 = x0 - subs(f,x0)/subs(df,x0); % iterative formula for
the first iteration
n = 1; % initializing iteration numbers
y(n) = x1; % storing iterations
d(n) = abs(x1-x0); % gap between first iteration (x1) and
initial guess (x0)
I(n) = n; % iteration number
% Other iterations
while abs(x1-x0) >= TOL % for stopping criterion
x0 = x1; % updating iteration
x1 = x0 - subs(f,x0)/subs(df,x0); % iterative formula
for the next iteration
n = n+1; % updating iteration number
y(n) = x1; % storing iterations
d(n) = abs(x1-x0); % gap between new iteration (x1)
and previous iteration (x0)
I(n) = n; % iteration number
end
% output
T = table(double(I'),double(y'),double(d')); % iteration
details; double - for numerical values
T.Properties.VariableNames = {'Iteration_Number'
'Iteration' 'Gap'} % colomn names
fprintf('Approximate Root = %0.10f\n',double(y(n)))
fprintf('Number of iterations =%3.0f\n',n)
.
`8
`9
Question 3
I. A common condition is that the spectral radius of the iteration matrix must be less
than 1.
% Gauss-Seidel Method
clear all
%inputs
A= [3,-6,2;-4,1,-1;1,-3,7];
b= [23;-8;17];
X0= [0.9;-3.1;0.9];
TOL= 0.00001;
maxIterate=20;
% matrices for iterative formula
D = diag(diag(A)); % diagonal matrix
L = -tril(A,-1); % lower triangular matrix
U = -triu(A,1); % upper triangular matrix
k = length(A); % output array
T=inv(D-L)*U;
C=inv(D-L)*b;
n=1;
d=TOL;
x0=X0;
while d>=TOL && n<=maxIterate
I1(n)=n;
x1=T*x0+C;
d=max(abs(x1-x0));
gap1(n)=d;
for i=1:k
y1(I1(n),i)=x1(i);
end
x0=x1;
n=n+1;
end
disp
('____________________________________________________')
fprintf('Iteration No | Iteration | Max gap \n');
disp
('____________________________________________________')
disp([I1',y1,gap1']);
disp
('____________________________________________________')
disp('Approximate solution');
r=[1:k;x1'];
`10
fprintf('Variable %1.0f = %0.4f \n',r); fprintf('Number
of iterations = %0.0f \n',n-1);
Output
This system of equations does not have diagonal dominance. Therefore it does not
converges.
III. Code
% Gauss-Seidel Method
clear all
%inputs
A= [-4,1,-1;3,-6,2;1,-3,7];
b= [-8;23;17];
X0= [0.9;-3.1;0.9];
TOL= 0.00001;
maxIterate=20;
% matrices for iterative formula
`11
D = diag(diag(A)); % diagonal matrix
L = -tril(A,-1); % lower triangular matrix
U = -triu(A,1); % upper triangular matrix
k = length(A); % output array
T=inv(D-L)*U;
C=inv(D-L)*b;
n=1;
d=TOL;
x0=X0;
while d>=TOL && n<=maxIterate
I1(n)=n;
x1=T*x0+C;
d=max(abs(x1-x0));
gap1(n)=d;
for i=1:k
y1(I1(n),i)=x1(i);
end
x0=x1;
n=n+1;
end
disp
('____________________________________________________')
fprintf('Iteration No | Iteration | Max gap \n');
disp
('____________________________________________________')
disp([I1',y1,gap1']);
disp
('____________________________________________________')
disp('Approximate solution');
r=[1:k;x1'];
fprintf('Variable %1.0f = %0.4f \n',r); fprintf('Number
of iterations = %0.0f \n',n-1);
`12
Output
Question 4
1. Necessary condition for the SOR method to converges is 0 < 𝜔 < 2. The matrix is also
must be symmetrically positive definite or diagonally dominant.
T=inv(D)*(L+U);
C=inv(D)*b;
n=1;
d=TOL;
x0=x0;
while d>=TOL & n<=maxn % for stopping criterion
I1(n)=n;
`13
x1=T*x0+C;
d=max(abs(x1-x0));
gap1(n)=d;
for i=1:k
y1(I1(n),i)=x1(i);
end
x0=x1;
n=n+1;
end
disp('---------------------------outputs-----------------
---------');
disp('---------------------------------------------------
---------');
fprintf('iterations no. | iterations | max gap of
iterations\n');
disp('---------------------------------------------------
---------');
disp([I1',y1,gap1']);
disp('approximate solution');
r=[1:k;x1'];
fprintf('variable %1.0f=%0.4f\n',r);
fprintf('number of iterations = %0.0f\n',n-1);
`14
Output
Gauss-Seidel method
% Gauss-Seidel Method
clear all
%inputs
A = [10,-2,-1,-1;-2,10,-1,-1;-1,-1,10,-2;-1,-1,-2,10];
b = [3;15;27;-9];
x0 = [0;0;0;0];
TOL = 0.00001;
maxIterate= 20;
% matrices
D = diag(diag(A)); % diagonal matrix
L = -tril(A,-1); % lower triangular matrix
U = -triu(A,1); % upper triangular matrix
k = length(A); % output array
`15
T=inv(D-L)*U;
C=inv(D-L)*b;
n=1;
d=TOL;
x0=x0;
disp
('____________________________________________________')
fprintf('Iteration No | Iteration | Max gap \n');
disp
('____________________________________________________')
disp([I1',y1,gap1']);
disp
('____________________________________________________')
disp('Approximate solution');
r=[1:k;x1'];
fprintf('Variable %1.0f = %0.4f \n',r);
fprintf('Number of iterations = %0.0f \n',n-1);
`16
Output
SOR method
%SOR method
clear all
A = [10,-2,-1,-1;-2,10,-1,-1;-1,-1,10,-2;-1,-1,-2,10];
b = [3;15;27;-9];
x0 = [0;0;0;0];
TOL = 0.00001;
maxn= 20;
w=1.1;
%Matrices
D=diag(diag(A));
L=-tril(A,-1);
U=-triu(A,1);
k=length(A);
T=inv(D-(L*w))*(((1-w)*D)+(w*U)); C=(inv(D-(w*L))*b)*w;
n=1;
d=TOL;
x0=x0;
while d>=TOL & n<=maxn I1(n)=n;
x1=T*x0+C;
d=max(abs(x1-x0));
gap1(n)=d;
`17
for i=1:k
y1(I1(n),i)=x1(i);
end
x0=x1;
n=n+1;
end
disp('---------------------------outputs-----------------
---------');
disp('---------------------------------------------------
---------');
fprintf('iterations no. | iterations | max gap of
iterations(last column)\n');
disp('---------------------------------------------------
---------');
disp([I1',y1,gap1']);
disp('approximate solution');
r=[1:k;x1'];
fprintf('variable %1.0f=%0.4f\n',r);
fprintf('number of iterations = %0.0f\n',n-1);
Output
`18
Outputs
Variable Gauss-Jecobi Gauss-Seidel SOR
3. Similarities
All are iterative methods using for solving systems of linear equations.
Convergence of these methods are generally based on spectral radius of the iteration
matrix.
These methods are applicable to square systems of linear equations.
Differences
Rate of convergence Slower than other Faster than Gauss Faster than Gauss
two methods. Jacobi method Seidel under certain
because it is using conditions.
updated values
within same
iteration.
Update strategy Uses the values from Updates current This is a
the previous values as soon as modification of
iteration to update they are computed Gauss Seidel
current value. within the same additional relaxation
iteration. parameter(𝜔).
`19