In: Advanced Math
Write a script that uses the function below to find root brackets for ?(?) = cos(ex) + sin(?), between ? = 0 ??? ? = ? with ns=100. Plot the output by first plotting the function and then plotting ‘*’ at each bracket point (on the x-axis). You may either give the plot() function two sets of inputs, or you can use hold on ... hold off to add plots to your figure.
function xb = incsearchv(func,xmin,xmax,ns)
clear all
close all
%function for which root have to find
func=@(x) cos(exp(x))+sin(x);
fprintf('function for which root have to find f(x)=')
disp(func)
%upper and lower limit
x0=0; x1=pi;
xx=linspace(x0,x1,1000);
yy=func(xx);
plot(xx,yy)
xlabel('x')
ylabel('f(x)')
title('x vs. f(x) plot')
[root]=incsearchv(func,x0,x1,100);
hold on
plot(root,func(root),'r*')
%Matlab function for Bisection Method
function [root]=incsearchv(fun,x0,x1,maxit)
if fun(x0)<=0
t=x0;
x0=x1;
x1=t;
end
fprintf('\nRoot using Bisection method\n')
%f(x1) should be positive
%f(x0) should be negative
k=10; count=0;
while k>5*10^-10
count=count+1;
xx(count)=(x0+x1)/2;
mm=double(fun(xx(count)));
if mm>=0
x0=xx(count);
else
x1=xx(count);
end
err(count)=abs(fun(x1));
k=abs(fun(x1));
if count>=maxit
break
end
%
end
fprintf('\tAfter %d iteration root using Bisection method is
%f\n',count,xx(count))
root=xx(end);
end
%%%%%%%%%%%% End of Code %%%%%%%%%%%%%