# Matlab bisection method

• Apr 17th 2010, 07:37 PM
AUCC
Matlab bisection method

Code:

```function bisect(n) syms x; f =  4 * x + 2; n = int32(n); err = 1; b=5; a=-5; while(b-a > err )  m = (a+b)/2 ;   sa=  sign(feval(f,a)) ;   sb = sign(feval(f,b));   sm= sign(feval(f,m));     if( sm == sa ) a=m;   else b=m;           end end```
The error message that i get is

??? Error using ==> feval
Argument must contain a string or function_handle.

Error in ==> bisect at 17
sa= sign(feval(f,a)) ;

• Apr 17th 2010, 10:19 PM
CaptainBlack
Quote:

Originally Posted by AUCC

Code:

```function bisect(n) syms x; f =  4 * x + 2; n = int32(n); err = 1; b=5; a=-5; while(b-a > err )  m = (a+b)/2 ;   sa=  sign(feval(f,a)) ;   sb = sign(feval(f,b));   sm= sign(feval(f,m));     if( sm == sa ) a=m;   else b=m;           end end```
The error message that i get is

??? Error using ==> feval
Argument must contain a string or function_handle.

Error in ==> bisect at 17
sa= sign(feval(f,a)) ;

f has to be a function of some kind, try:

f=@(x) 4*x+2; % anonymous function definintion

or:

f=inline('4*x+2'); %inline function definition

for the function definition.

CB
• Apr 18th 2010, 08:22 AM
AUCC
Now i want to calculate the root's of a equation of degree 2

Code:

`f=inline('x^2 + 2*x + 1');`
The root's are given by the value of m.

So i get m=5 as the answer.

• Apr 18th 2010, 11:21 AM
CaptainBlack
Quote:

Originally Posted by AUCC
Now i want to calculate the root's of a equation of degree 2

Code:

`f=inline('x^2 + 2*x + 1');`
The root's are given by the value of m.

So i get m=5 as the answer.