For this question we must use MATLAB and please show all cod
For this question we must use MATLAB and please show all code, thank you
Refer to lab experiment 6.4 part (a) and find the roots of the equation below f_1 (x) = cos (x) - x = 0, for 0Solution
Matlab function bisect.m to find root of the function using bisection method
function [Root,Iter] = bisect(f,a,b,tol) % The function definition bisect.m
 if nargin ~= 4 %Checking number of arguments to the function bisect
 % if number of arguments not equal to 4 print error message
 error(\'Number of arguments must be four\');
 end
 if tol < 0 % is the input tol value is negative
 % print error message
 error(\'Error tolerence must be a positive quantity\');
 end
 Max_Iter = 100; % maximum number of iterations allowded
 fa = f(a); % functional at a
 fb = f(b);% functional at b
 if(fa>0 && fb <0) % checking fafb <0 or not
 xplus = a;
 xminus = b;
 elseif(fa<0&&fb>0)
 xplus = b;
 xminus = a;
 else
 error(\'Not a valid bracketing interval\');
 end
 Iter = 0; % iteration counter
 xnew=(xplus+xminus)*0.5; % new point in the intervel
 err=abs((xplus-xminus)/(xplus+xminus))*100; % estimated error at new x value
 while err>tol && Iter<Max_Iter % iterate loop unitil error < tol or Iter > Max_iter
 fnew=f(xnew); % function evaluating at newly computed x value
 if(fnew>0) % determin the sign of f at new x value
 xplus=xnew; % if f is positive replave x+ with new x
 else
 xminus=xnew; % if f is negative replace x- with new x
 end
 Iter = Iter +1; % Increment Iteration counter
 xnew=(xplus+xminus)*0.5; % new x value
 err=abs((xplus-xminus)/(xplus+xminus))*100; % estimated error at new x value
 end
 if(Iter>=Max_Iter) % faild to converge
 error(\'Faild to converge\');
 else
 Root = xnew; % after the iterations the approximated root will be in xnew
 end
 end
   
Calling the function get root of the given function
>> f = @(x) cos(x)-x; % The given function
 >> a = 0; b = pi/2; % two boundaries
 >> tol = 0.1; % percentage of tolerence
 >> [Root,Iter] = bisect(f,a,b,tol) % Calling the function to get the root
Root =
0.7390
 Iter =
11
>> f(Root)% Validating the result by finding f(root)
ans =
1.5044e-04
>>


