problem while applying bisection method "Undefined function or variable 'vbr'

Hi
I am trying to apply bisection method and i did all the appropriate steps but it still give me this error
(Undefined function or variable 'vbr'. Error in Untitled>bisectf (line 32)
fa = vbr(a); fb =vbr(b); )
my codes
root1 = bisectf(1.70,1.75,eps,100)
root2= bisectf(4.640,4.650,eps,100)
root3= bisectf(7.80,7.83,eps,100)
function root=bisectf(a0,b0,ep,max_iterate)
if a0 >= b0
disp('a0 < b0 is not true. Stop!')
return
end
format short e
a = a0; b = b0;
fa = vbr(a); fb =vbr(b);
if sign(fa)*sign(fb) > 0
disp('f(a0) and f(b0) are of the same sign. Stop!')
return
end
c = (a+b)/2;
it_count = 0;
while b-c > ep & it_count < max_iterate
it_count = it_count + 1;
fc = vbr(c);
% Internal print of bisection method. Tap the carriage
% return key to continue the computation.
iteration = [it_count a b c fc b-c]
if sign(fb)*sign(fc) <= 0
a = c;
fa = fc;
else
b = c;
fb = fc;
end
c = (a+b)/2;
pause
end
format long
root = c
format short e
error_bound = b-c
format short
it_count
end

Answers (1)

vbr needs to either be a function file (i.e., a file named vbr.m on your MATLAB path), or more likely you should pass that into your bisectf function as a function handle. E.g.,
function root=bisectf(vbr,a0,b0,ep,max_iterate) % <-- added vbr as input argument
Then when you call bisectf, put a function handle for the function you are trying to find the root of as the first argument. E.g.,
f = @(x)x-1.72;
root1 = bisectf(f,1.70,1.75,eps,100)

2 Comments

i applied the modification that you said put it another error came up * * * * _(
Error using Untitled>bisectf Too many input arguments.
Error in Untitled (line 20) root1 = bisectf(y,1.70,1.75,eps,100);
)_**** this is my full codes to find roots with bisection mthod
y=x.*(cos(x).*cosh(x)+1)+ 0.25*(cos(x).*sinh(x)- cosh(x).*sin(x))
root1 = bisectf(y,1.70,1.75,eps,100)
root2= bisectf(y, 4.640,4.650,eps,100)
root3= bisectf(y,7.80,7.83,eps,100)
function root=bisectf(a0,b0,ep,max_iterate)
if a0 >= b0
disp('a0 < b0 is not true. Stop!')
return
end
format short e
a = a0; b = b0;
fa = vbr(a); fb =vbr(b);
if sign(fa)*sign(fb) > 0
disp('f(a0) and f(b0) are of the same sign. Stop!')
return
end
c = (a+b)/2;
it_count = 0;
while b-c > ep & it_count < max_iterate
it_count = it_count + 1;
fc = vbr(c);
% Internal print of bisection method. Tap the carriage
% return key to continue the computation.
iteration = [it_count a b c fc b-c]
if sign(fb)*sign(fc) <= 0
a = c;
fa = fc;
else
b = c;
fb = fc;
end
c = (a+b)/2;
pause
end
format long
root = c
format short e
error_bound = b-c
format short
it_count
end
I advised you to do two things. First, add vbr as the first input argument to your bisectf function definition. And second, to pass in a function handle as the first argument when you call bisectf. You did neither of these things. You still don't have vbr in your bisectf input argument list, and the y you pass in is not a function handle. You need to fix those two things. Read my initial post above carefully. E.g., to form a function handle you need to use the @(x) notation:
y = @(x)x.*(cos(x).*cosh(x)+1)+ 0.25*(cos(x).*sinh(x)- cosh(x).*sin(x))

Sign in to comment.

Categories

Asked:

on 5 Apr 2018

Edited:

on 6 Apr 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!