Bisection Method Piecewise function

5 views (last 30 days)
Ewynne on 16 Feb 2017
Answered: Walter Roberson on 17 Feb 2017
So this is my code to try to bisect the piecewise function. But, when I run the code that was in the editor then I do
[r, iters] = bisectionF('f_feb15(x)', -2, .1)
matlab says
Undefined function or variable 'bisectionF'.
Sorry for such an elementary question. I am just beginning a scientific computing class and I am a math major so the only computer experience I have is LaTeX! Thank you so much for helping! I think it is something silly that I am missing. One of my friends said "I think it might have to do with your function file being saved with a different name then you actually called it." but I have no idea what this even means.
if x<=0
else if x>0
end %ends else if
end %ends else
clear all;
function[r, iters] = bisection(f, xL, xR)
for j=1:10
fM=feval(f, xM)
fL=feval(f, xL)
fR=feval(f, xR)
if fM*fL<0
end %end if
end %end for
if abs(fM)<10^(-5)
end %end if
end %function

Answers (2)

Rahul Kalampattel
Rahul Kalampattel on 16 Feb 2017
Edited: Rahul Kalampattel on 16 Feb 2017
The function you have declared is called bisection, not bisectionF. When you want to call or use this function, you need to use the name you declared.
Rahul Kalampattel
Rahul Kalampattel on 16 Feb 2017
Are you using the R2016b release of Matlab? If not, your function needs to be saved in a file called bisectionF.m (more info).
The file should also be in your current working directory.

Sign in to comment.

Walter Roberson
Walter Roberson on 17 Feb 2017
Your bisectionF should be in bisectionF.m
Your f_feb15 should be in f_feb15.m
You should delete the "clear all" and you should never use it inside a function again. If you use "clear all" inside a script, it should be the very last thing in the script, and it should be a script dedicated to trying to get your MATLAB session unstuck. "clear all" is the "blow EVERYTHING up, especially yourself" command.
Your call
[r, iters] = bisectionF('f_feb15(x)', -2, .1)
should be
[r, iters] = bisectionF(@f_feb15, -2, .1)
[r, iters] = bisectionF('f_feb15', -2, .1) %not really recommended

Community Treasure Hunt

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

Start Hunting!