Develop a script that implements the bisection method sec in
Develop a script that implements the bisection method (sec instructions below) for solving for the root of a single nonlinear equation and apply it to the following function: f(x) = x *tan (x) - 1 = 0, if a lessthanorequalto x lessthanorequalto b, where a = 0, b = pi/2, To implement bisection method, use the following pseudo-code: Define the following constants: a, b, tol (convergence tolerance) and Nmax (maximum iterations) Begin iterative solution Calculate p=(a + b)/2 If f(p) has the same sign as f(a), set a = p. Otherwise set b = p If the absolute value of (b-a) is less than the convergence tolerance, print the solution and end the program If the number of iterations is equal to the maximum number of iterations allowed, print that the maximum iterations was exceed and the end the program If the number of iterations is less than the maximum number, return to step 2.
Solution
MATLAB SCRIPT
% The function f(x)
f = @(x) x*tan(x)-1;
% Defining the constants
a = 0;
b = pi/2;
tol = 10^-1;
Nmax = 100;
% Begining of the iteration
for i = 1:Nmax
% calculating the point p
p = (a+b)/2;
% Checking the sign of f(p) and f(a)
if((f(a) > 0 && f(p) > 0) || (f(a) < 0 && f(p) < 0))
a = p;
else
b = p;
end
if(abs(a-b)<tol) % printing the result if
% absolute value of (a-b) is less than tolance
fprintf(\'The root, x = %f \ \',p);
break;
end
end
if(i >= Nmax) % Printing if loop exited by reaching limit
fprintf(\'Maximum iteration was exceed\ \');
end
OUTPUT
The root, x = 0.883573
