0% found this document useful (0 votes)
18 views

Lab Sheet-1

1. The document introduces various numerical methods for solving equations, including graphical methods, incremental search, and bracketing methods like bisection. 2. It provides MATLAB examples to demonstrate finding the square root of 2 using incremental search and the bisection method. 3. Numerical methods are required to solve many engineering problems analytically and yield approximate solutions close to the exact values.

Uploaded by

rj Opu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Lab Sheet-1

1. The document introduces various numerical methods for solving equations, including graphical methods, incremental search, and bracketing methods like bisection. 2. It provides MATLAB examples to demonstrate finding the square root of 2 using incremental search and the bisection method. 3. Numerical methods are required to solve many engineering problems analytically and yield approximate solutions close to the exact values.

Uploaded by

rj Opu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Numerical Techniques Laboratory

Lab sheet-1: Solution of Equations using


graphical method, incremental search and
Bisection Method.

Introduction:
Why Numerical Analysis?

For many engineering problems, we cannot obtain analytical solutions.

Numerical analysis gives us a set of tools to solve those problems.

Numerical methods yield approximate results, results that are close to the exact analytical solution.
We cannot exactly compute the errors associated with numerical methods.

Analytical VS Numerical Solution

Analytical solution involves the procedures of algebra, calculus, differential equations, partial
differential equations, or the like.

Due to the complexity of the equations in modeling the real life system, the exact solutions are often
difficult to be found. Thus require the use of numerical methods. The solution obtained by using
numerical methods is called numerical solution.

Numerical solution is similar in that problems are solved, but now the only procedures that are used
are arithmetic: add, subtract, multiply, divide, and compare.
MATLAB Example: Finding square root of 2

• % x^2 = 2; Lets search values from 1.30


• x=1.30; fprintf('x = %1.2f, x^2 = %1.2f \n', x, x^2);
• while( abs(x^2-2) > .03 )
• x = x + 0.01;
• fprintf('x = %1.2f, x^2 = %1.2f \n', x, x^2);
• end

Matlab Output
x = 1.30, x^2 = 1.69
x = 1.31, x^2 = 1.72
x = 1.32, x^2 = 1.74
x = 1.33, x^2 = 1.77
x = 1.34, x^2 = 1.80
x = 1.35, x^2 = 1.82
x = 1.36, x^2 = 1.85
x = 1.37, x^2 = 1.88
x = 1.38, x^2 = 1.90
x = 1.39, x^2 = 1.93
x = 1.40, x^2 = 1.96
x = 1.41, x^2 = 1.99

Numerical Error

For many applied engineering problems, we cannot obtain analytical solutions.

Therefore, we cannot compute exactly the errors associated with our numerical methods. In these
cases, we must settle for approximations or estimates of the errors.

Case-I (Exact value is known): Absolute error, ε a =|Exact value-Approximate value|


Absolute error
Relative error, ε r =
|Exact value|
Case-II (Exact value is not known):
ε a= ¿|Current approximation - Previous approximation| ¿
|Current approximation - Previous approximation|
ε r=
|Current approximation|

Tasks:
1) Can you rewrite the previous code to show errors?
2) What kind of error we used for while loop to stop iteration?
3) What is the effect of increasing step size?
4) What is the effect of decreasing offset value in while loop?
5) Is there any chance the code will fall into infinite loop?
6) Is there any effect of initial value?

Some Numerical Methods to Find Roots


• Graphical Method

• Incremental Search

• Bracketing Methods

• Bisection

• False Position

• Open Methods

• Fixed Point

• Secant

• Newton

A. Graphical Method to Find the


Root
A simple method for obtaining an estimate of
the root of the equation f (x) = 0 is to make a
plot of the function and observe where it
crosses the x axis.
%% Graphical Method
x = [1.3: 0.01 :1.6];
f=@(x) x.^2-2;
plot(x,f(x), [1.3 1.6],[0 0]);
grid on

Matlab Example:

B. Incremental Search
• Incremental search is a technique of calculating (𝑥) for incremental values of 𝑥 over the
interval where the root lies.
• It starts with an initial value, 𝑥 .
0
• The next value 𝑥 for 𝑛 = 1,2,3, … is calculated by using
𝑛
• 𝑥 =𝑥 +ℎ
𝑛 𝑛−1
• where ℎ is referred to a step size.
• If the sign of two (𝑥) changes or if f (𝑥 ). f ( 𝑥 ) < 0,
𝑛 𝑛−1
• then the root exist over the prescribed interval of the lower bound, 𝑥 and upper
𝑙
bound, 𝑥 .
𝑢
• The root is estimated by using

Matlab Example:
• %% Solution by Inreamental Search Method:
• f=@(x) x^2-2; h=0.01;
• x1=1.30; x2=x1+h; fprintf('x2 = %1.2f, f(x2) = %1.2f \n', x2,
f(x2));
• while( f(x1)*f(x2) > 0 )
• x1=x2; x2=x1+h; fprintf('x2 = %1.2f, f(x2) = %1.2f \n', x2,
f(x2));
• end
• fprintf('Final solution is (x1+x2)/2 = %1.2f \n', (x1+x2)/2);

x2 = 1.31, f(x2) = -0.28


x2 = 1.32, f(x2) = -0.26
x2 = 1.33, f(x2) = -0.23
x2 = 1.34, f(x2) = -0.20
x2 = 1.35, f(x2) = -0.18
x2 = 1.36, f(x2) = -0.15
x2 = 1.37, f(x2) = -0.12
x2 = 1.38, f(x2) = -0.10
x2 = 1.39, f(x2) = -0.07
x2 = 1.40, f(x2) = -0.04
x2 = 1.41, f(x2) = -0.01
x2 = 1.42, f(x2) = 0.02
Final solution is (x1+x2)/2 = 1.42

C. Bracketing Methods: Bisection


“Let, f be a continuous function on [a,b], satisfying f (a)Xf (b) < 0. Then f has a root between a and
b, that is, there exists a number r satisfying a < r <b and f (r) = 0. “
Algorithm of Bisection Method

MATLAB Example
%% Bisection method
f=@(x) x.^2-2; i=0;%iteration
xl = 1.3; % Lower limit of x
xu = 1.6; % Upper limit of x
tol = 10^-2; % Root is correct upto 3 decimal place

if(f(xl)*f(xu)>0)
disp('No root in given bracket'); break;
else
disp(' xl, f(xl), xu, f(xu), xr, f(xr)');
while (abs((xu-xl)/2) > tol)
xr = (xl + xu)/2 ;
fprintf('%1.3f %1.3f %1.3f %1.3f %1.3f %1.3f \n',...
xl,f(xl),xu,f(xu),xr,f(xr));
if(f(xr)==0) break; end

if(f(xl)*f(xr)>0)
xl = xr;
else
xu = xr;
end
i=i+1;
end% end while
disp(['Root is ' num2str(xr) ', Iterations taken ' num2str(i)]);
end % end main if
Task:

You might also like