File Exchange

image thumbnail

Root of functions by Bisection method

version 1.0.0.0 (1.17 KB) by carolina franco
It obtains the root of a function

0 Downloads

Updated 01 Feb 2017

View License

clc, clear, close all
% f is the function to be used
disp('Obtening the root of the function Y=X^3-2');
ao= input('Enter the first possible limit: ');
bo= input('Enter the second possible limit: ');
Y1=f(ao);
Y2=f(bo);
Uo=(ao+bo)/2;
Yo=f(Uo);
zo=Y1*Y2;
iter=0;
tic()
while abs(Yo)>0.0001
iter=iter+1;
if zo>0
if abs(Y1)> abs(Y2)
Uo=bo;
else
Uo=ao;
end
Yo=f(Uo);
Y2=Yo;
if Y2<0
U1= Uo+abs(Uo)*100*rand(1);
Y1=f(U1);
else
U1= Uo-abs(Uo)*100*rand(1);
Y1=f(U1);
end
zo=Y1*Y2;
U=(U1+Uo)/2;
Yo=f(U);
else

if abs(Y1)> abs(Y2)
ao=bo;
else
end
Uo=(ao+Uo)/2;
ao=Uo;
Yo=f(Uo);
Y2=f(Uo);
Y1=Yo;
zo=Y1*Y2;
Uo=(ao+Uo)/2;
end
end
y=toc;
disp(['The root of the function Y=X^3-2 is approximately: ', num2str(U,6)]);
disp(['Checking the Y value: ', num2str(Yo,6)]);
disp(['The number of iteration was: ', num2str(iter)]);
disp(['Duration of the process: ', num2str(y)]);
% f function from here

function y=f(a)
y=a.^3-2;
end

Comments and Ratings (0)

MATLAB Release Compatibility
Created with R2015a
Compatible with any release
Platform Compatibility
Windows macOS Linux