# Mathlab help Bisection Method

• Jun 12th 2008, 12:26 PM
Dream
Mathlab help Bisection Method
Hello, I am trying to figure out how to use the bisection methode to do this problem and matlab, but I keep having problems with inputing my formula. Here is it below:

My formula: ln(x+1)+tan(2x) = 0

Here is my bisection method m-file:

function ww=bisect(f,a0,b0,tol)
%a and b are the end of the interval, c is the midpoint, n is iterations
%tol is our tolerence which is our stopping criteria
a=a0; b=b0;
sfb = sign(feval(f,b));
width = b-a;
n=0;
while width > tol
n=n+1;
width = width/2;
x = a + width;
sfx = sign(feval(f,x));
if sfx ==0
a = x; b = x;
return
elseif sfx == sfb, b = x;
else a = x; end
end
c=0.5*(a+b);
ww=[a,b,c,n];
return

Could someone please tell me who to apply the above formula so that the bisection method is performed then I have to show the graph? I am new to matlab and it will not read the party in when I put in y = ln(x+1)+tan(2x) it keeps saying error.
• Jun 15th 2008, 03:41 AM
CaptainBlack
Quote:

Originally Posted by Dream
Hello, I am trying to figure out how to use the bisection methode to do this problem and matlab, but I keep having problems with inputing my formula. Here is it below:

My formula: ln(x+1)+tan(2x) = 0

Here is my bisection method m-file:

function ww=bisect(f,a0,b0,tol)
%a and b are the end of the interval, c is the midpoint, n is iterations
%tol is our tolerence which is our stopping criteria
a=a0; b=b0;
sfb = sign(feval(f,b));
width = b-a;
n=0;
while width > tol
n=n+1;
width = width/2;
x = a + width;
sfx = sign(feval(f,x));
if sfx ==0
a = x; b = x; return
elseif sfx == sfb, b = x;
else a = x; end
end
c=0.5*(a+b);
ww=[a,b,c,n];
return

Could someone please tell me who to apply the above formula so that the bisection method is performed then I have to show the graph? I am new to matlab and it will not read the party in when I put in y = ln(x+1)+tan(2x) it keeps saying error.

Well the following seems to work for me:

Code:

```» » » » bisect('ff',0,10,0.001) ans =     2.6831    2.6837    2.6834  14.0000 »```
where ff.m is on the search path and defined the function:

Code:

```function rv=ff(x)   rv=log(x+1)+tan(2*x);```
But this is on a very old version of Matlab so this may no longer be the prefered format for passing a function as an argument to a function.

RonL
• Jun 15th 2008, 04:03 AM
CaptainBlack
Quote:

Originally Posted by Dream
in when I put in y = ln(x+1)+tan(2x) it keeps saying error.

Unless they have changed a few basic things while I was looking the other way (entirly possible as there are at least four Matlab updates sitting in my bookshelves at work that I have never installed or read the notes of) natural log is "log" not "ln" and you have to have a multiply operator between the 2 and the x. So the correct form of the function you want is:

log(x)+tan(2*x)

RonL
• Jun 15th 2008, 05:52 PM
Dream
Bisection method
Hey try the problem with the initial interval a0=-1, and b0=1 with the following equation using the same bisection method m-file. I am having problems with it acknowledging the sign. Maybe you can figure it out. Here is the function to be used below:

6((e^(x) -x) - 2x^(3) - 3x^(2) -6 between -1 and 1

use the bisection method for this function.
• Jun 16th 2008, 06:05 AM
CaptainBlack
Quote:

Originally Posted by Dream
Hey try the problem with the initial interval a0=-1, and b0=1 with the following equation using the same bisection method m-file. I am having problems with it acknowledging the sign. Maybe you can figure it out. Here is the function to be used below:

6((e^(x) -x) - 2x^(3) - 3x^(2) -6 between -1 and 1

use the bisection method for this function.

First you have to code it right:

6*(exp(x)-x) - 2*x^3 -3*x^2-6.

Now this has a root at x=0, but the bisection method has trouble finding it accurately since this is zero (that is its value is down in the rounding noise) over an interval of length several* 10^{-6}.

However the bisection method should find a root at ~+/- 10^-6. Except when I run your code it returns nothing, presumably it does not recognise this as convergence.

RonL