In: Advanced Math
Using the bisection method:
Make a program to use this method using the
following three functions and use it to find the root of this
function f (x) = x * x * x-8.
a) A function so that the user between xlower and xupper that meets
the value of the function has a different sign and if he does not
ask for new values.
b) A function to find the root and call it bisection and perform a
maximum of iterations (maximum approximations of the root). All
estimated root values have to be printed.
c) A function to obtain the values of the function.
%%Matlab code for finding root using newton secant bisection and
false
clear all
close all
%function for which root have to find
fun=@(x) x.*x.*x-8;
xx=linspace(-2,3);
yy=fun(xx);
plot(xx,yy)
xlabel('x')
ylabel('f(x)=x^3-8')
title('x vs. f(x) plot')
[root]=bisection_method(fun,-2,3,1000);
fprintf('\tRoot using Bisection method is %f.\n',root)
hold on
plot(root,fun(root),'r*')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Matlab function for Bisection Method
function [root]=bisection_method(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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%