Lab Sheet-1
Lab Sheet-1
Introduction:
Why Numerical Analysis?
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 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
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
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.
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?
• Incremental Search
• Bracketing Methods
• Bisection
• False Position
• Open Methods
• Fixed Point
• Secant
• Newton
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);
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: