# The Bisection Method using Scilab

Recently, I have really been enjoying learning to use Scilab which is an open-source mathematics software that works alot like Matlab. Specifically we have run into some design problems in water and wastewater engineering that involved solving for variables that are described implicitly by an equation and cannot be solved through typical algebraic means. For example:

This cannot be solved directly for x. So how can we find the value of x that will satisfy the equation? Well this is where the numerical analysis technique of the bisection method comes in. The bisection method is a bounded or bracketed root-finding method. In other words, it will locate the root of an equation provided you give it the interval in which a root is located.

The search for the root is accomplished by the algorithm by dividing the interval in half and determining if the root is in one half or the other. Then it divides that interval in half and so on until it gets within a specified relative error. Below I have entered my code for this algorithm. If you save this file as Bisection_Method.sci in a scilab/modules/…/macros folder and execute it in the SciNotes editor, you can then use it by entering a command in the console like:

root = Bisection_Method(‘x-sinx-3’,0,6.5,0.5)

The first passed parameter is the function equation itself solved such that f(x) = 0. The second parameter is the lower end of the interval while the third is the upper end. The final parameter is the percent relative error that the algorithm much reach with the solution before it terminates and gives a result.

Please feel free to download Scilab for free here and copy this code and use it. Let me know if you have any troubles or if you find a problem with the code.

```//The Bisection Method is a bracketed root locating method. Therefore
//the user must input the lower and upper bounds on the interval where
//the root is to be located.
//func_expression - must be a string containing the function expression
//with the variable written as 'x'.
//es - is the relative percent error criterion that must be met for the
//method to terminate.
function [root]=Bisection_Method(func_expression, x_lower, x_upper, es)
x = x_upper;
fu = evstr(func_expression); //evaluate the function at x_upper
x = x_lower;
fl = evstr(func_expression); //evaluate the function at x_lower
//***********************************************************
//test to see if there is a root in the interval
if (fu*fl >= 0) then
root = 'no root in interval given';
else
//there is a root in the interval
exact_solution = 'notfound';
ea = 100;
xr_new =(x_upper + x_lower)/2;
//*******************************************************
//iteratively progress toward to root until it is found or it
//is approximated with a relative error less than required
while ea > es & exact_solution == 'notfound',
x = xr_new;
fr = evstr(func_expression);
x = x_lower;
fl = evstr(func_expression);
//***************************************************
if(fl*fr < 0) then
x_upper = xr_new;
elseif(fl*fr > 0) then
x_lower = xr_new;
elseif(fl*fr == 0) then
root = xr_new;
exact_solution = 'found';
end //of if statement
//***************************************************
//calculate the approximate relative error for the iteration
xr_old = xr_new;
xr_new =(x_upper + x_lower)/2;
ea = abs((xr_new - xr_old)/xr_new)*100;
end //of while loop
//*******************************************************
//if error criterion has been met but the exact answer has not
//been found, set the root to the adequate approximation.
if (exact_solution == 'notfound') then
root = xr_new;
end //of if statement
//*******************************************************
end //of if-else statement
//***********************************************************
endfunction```

Forensic Engineer, Math Nerd, Husband of @rebekahmcgehee, Father of 4, Follower of Jesus

Posted on October 21, 2011, in Mathematics, Numerical Analysis and tagged , , , , . Bookmark the permalink. 23 Comments.

1. Khushboo bhatia

hey thx for the code.It is really helpful.bt I am getting error as invalid factor,plz can u guide on this?

• Maybe you could try adjusting the initial interval, I’m not sure. Could you give me some more feedback on what you did and the error you had?

• Khushboo, I think I may have figured out your issue. Better late than never. I received the same error when I copied the “root = …” line out of my post into the Scilab console. For some reason when you copy and paste, the single quotes (‘) that have to go around the function definition don’t translate. If you paste, delete these single quotes and type them back on in the console it then works. Hope this helps.

2. –>exec(‘C:\Users\Bonbon\Desktop\Bisection_Method.sci’, -1)
end function
!–error 34
Incorrect control instruction syntax.

at line 47 of function Bisection_Method called by :
return
at line 55 of exec file called by :
exec(‘C:\Users\Bonbon\Desktop\Bisection_Method.sci’, -1)

3. could you tell me what problem are there?

• Erwin,
It would be helpful to debug this problem if you could provide me with the command line entry you made that generated this outcome. I did however take a preliminary look at things and am wondering if it could be because the bracketed range is symmetrical about zero. The reason this could be a problem is because in the algorithm you see that the relative error is calculated using the “xr_new” in the denominator. Also note that “xr_new” is calculated just prior as the average of “x_upper” and “x_lower”. My initial suggestion would be to try a different starting range trying to avoid symmetry about zero. If this doesn’t work and you’d like then send me the command line entry as I stated previously and I’ll try and take a look at it. Thanks for the feedback.

4. Nathan

Thank you so much for this!
I was able to run it. No errors at all! Thumbs up!
Do you have any codes for the Newton-rhapson Method for SciLab?

• I don’t currently have code for that posted. Since you requested it, I’ll try and put something up when I get a chance. Thanks for checking us out!

5. unKnown

tnx., ^_^

6. –>root((x^2)+1, 2, 3)
!–error 4
Undefined variable: x

how are we suppose to define x?

• Dale,
You don’t need to define x, but as you have the command listed it is incorrect. The command should be along the lines of:
–>root = Bisection_Method(‘(x^2)+1’,2,3,0.5)
after executing the bisection method code. However, as stated in the body of this post, this is a bracketed method which means your bounds must include a root or you will receive the result, “no root in interval given”.

7. i was trying to input something like this: root((x^2)+1, 2, 3, 1.0) but then the error says the variable x is not defined..

8. thanks a lot.. it’s big help..

9. Juhasz Andor

Hi!

I need some help with the bisection. I don’t know how to type the function into this form.
Shall I use brackets or any character to separate the function in this form?
I tried this way but doesnt works anyway.: function intfel(x.^3-2*x-1,1,2,0.000005)
it always calls error 37! incorrect function. And i dont know how to solve.
Intfel means bisection in hungarian. ( Half interval)

Sorry for my english!

Thanks for helping 🙂

Andor

• Andor,
First, I’m not sure how you defined the .sci file in the SciNotes editor. If you started the function:

function [root]=intfel(func_expression, x_lower, x_upper, es)

and you titled the file “intfel.sci” then using “intfel” is correct. However if you just copied what I have then you would use “Bisection_Method”. Basically you have to be consistent with whatever you named the function.

Second, assuming you used the intfel definition, the way to call the function from the console is like this:

root = intfel(‘x^3-2*x-1’,1,2,0.000005)

When I run this, I obtain the following result:

root =

1.6180339

Thanks for the comment! I hope this helps.

• shivani

->deff(‘y=f(x)’,[‘y=x-sin(x)-3’]);
Warning : redefining function: f . Use funcprot(0) to avoid this message

–> Bisection_Method(f,0,6.5,0.5);

–>ans
ans =

3.0595703 shivani

10. Ana S.

where do i put the function f(x)=x^3+4x^2-10=0 that has a root in [1,2] in this type of code?

• Just follow the instructions above. Copy the above code into the scinotes editor. (The scinotes editor is opened from the applications menu) Execute the code. (hit f5 or click on the ‘play’ button) Then in the scilab console (This should be the original scilab window. The scinotes editor should open in a new window) type in what I have listed above: root = Bisection_Method(‘x-sinx-3′,0,6.5,0.5) except you will put your function (x^3+4*x^2-10) in place of the x-sinx-3 that I have, and you will change your interval to 1,2 instead of my 0,6.5. hit enter and it should say: root = your approximate numeric answer. That should do it. Let me know if you have any further questions.

11. Ana S.

i followed your instructions. then this happens:
!–error 4
Undefined variable: x

does this code include the graphing of the function? If not do you have any codes to help me?

• Ana,
When you executed the code in the SciNotes editor, you should get something similar to this at the console prompt:

–>exec(‘C:\Program Files (x86)\scilab-5.3.3\modules\scinotes\macros\Bisection_Method.sci’, -1)

Then at the following prompt type in:

–>root=Bisection_Method(‘x^3+4*x^2-10’,1,2,0.5)

I’ve done this twice now without any issues.
As far as your second question is concerned, this program does not currently plot the function, but Scilab has the ability to plot functions. Here is how you can plot yours. Type in the following commands and you should get a nice plot:

–>x = -4:0.1:4;

–>y=x^3+4*x^2-10;

–>plot(x,y); set(gca(),”grid”, [1 1]); a = gca(); a.x_location = “origin”; a.y_location = “origin”;

Hope this helps!