0% found this document useful (0 votes)
40 views19 pages

21 Eng 143

MATLAB codes are presented to solve nonlinear equations using bisection, secant, and Newton's methods, and to solve systems of linear equations using Gauss-Seidel and Gauss-Jacobi iterative techniques. The codes calculate iterations to approximate roots or

Uploaded by

Udana Thenuwara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views19 pages

21 Eng 143

MATLAB codes are presented to solve nonlinear equations using bisection, secant, and Newton's methods, and to solve systems of linear equations using Gauss-Seidel and Gauss-Jacobi iterative techniques. The codes calculate iterations to approximate roots or

Uploaded by

Udana Thenuwara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Numerical solution of Nonlinear

and systems of linear equations

INSTRUCTED BY: NAME : THENUWARA TAUO


INDEX NO. : 21/ENG/143
GROUP : G3
CONDUCTED DATE: 26/10/2023
SUBMISSION DATE: 03/11/2023
`1
Question 01

i. a = 1, c=1
a. Bisection method (Code)

%Index number = 143


%a=1, c=3
%Bisection method code
clear all
syms x
format short ; %display 4 decimal places
%inputs
f= 4*sin(exp(-2*x)+8*x+2);
r1 = 2;
r2 = 2.2;
TOL = 0.00001;
%First iteration
n = 1;
c = (r1+r2)/2;
y(n) = c; %array of mid values
d(n) = abs(r1-r2)/2; %Length of the interval
I(n) = n; %Iteration number

while abs(r1-r2)/2 >= TOL %stopping criterion


if subs(f,r1)*subs(f,c) <0
r2=c;
else
r1=c;
end
c = (r1+r2)/2; % mid value
n = n+1; %update iteration
y(n) = c; % array of mid values
d(n) = abs(r1-r2)/2; % array of lengths
I(n) = n;% array of iteration number
end
%output
% Constructing the table of results
T = [ I ; y ; d ];
%display it
Approximate_Root = c
No_of_iterations = n
disp('---------------------------------------------------
------------')
fprintf('iteration nu, mid values, iteration gap with
previous iteration \n')
disp('---------------------------------------------------
------------')

`2
disp(T')
disp('---------------------------------------------------
------------')

Output

Secant method (Code)

%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

Newton’s Method (Code)

%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

ii. Bisection method

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 (Code)

% 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)
.

For approximation x0 = 0.15 Matlab does not provide an answer.


Therefore worked calculations can be done as below,

`8
`9
Question 3

I. A common condition is that the spectral radius of the iteration matrix must be less
than 1.

II. Gauss-Seidel method (Code)

% 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.

2. Gauss Jacobi method

%Gauss Jecobi method


clear all
format short
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;
%non convergent situation
%matrices
D=diag (diag(A));
L=-tril(A,-1);
U=-tril(A,1);
k=length(A);

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;

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);

`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

X1 -28.3789 1.0000 1.0000

X2 61.6145 2.0000 2.0000

X3 -115.7479 3.0000 3.0000

X4 141.1341 -0.0000 -0.0000

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

Gauss Jacobi Gauss Seidel SOR

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy