Need help with my bisection code
3 views (last 30 days)
Show older comments
Laurence Guevremont
on 8 Oct 2016
Answered: Nahid Hasan
on 2 Feb 2022
Basically, I use Newton-Raphson method to find roots but if this one fails, my program calls bisection method which is not working. Something is wrong and I can't seem to figure it out. I need to use bisection method to find the roots and my output will be x3, nrfail. The interval is from 0 to 100. If someone could please help. I think I am missing naming the variables and there is an error before the break.
function [ x3, nrfail ] = bisection( fun,x1,y1,x2,y2,tol )
check=1;
dyold=1e12;
nrfail=0;
tol=1e-12;
while check > tol
x3=(x1+x2)/2;
y3=feval(fun,x3);
if y1*y3 < 0
x2=x3;
y2=y3;
else
x1=x3;
y1=y3;
end % ERROR
dy=abs(yl-yr);
if dy>dyold
nrfail=1;
break
end
check= abs(1-x1/x3);
end
end
0 Comments
Accepted Answer
Marc Jakobi
on 8 Oct 2016
1. You are "missing" the variable "yr". You have to define it somewhere before using this line:
dy=abs(yl-yr);
2. After the if statement
if dy>dyold
nrfail=1;
break
end
you should add the line
dyold = dy;
3 Comments
Marc Jakobi
on 8 Oct 2016
not enough arguments (input or output) means you are using the wrong syntax when calling the function. How do you call the function?
Walter Roberson
on 8 Oct 2016
Often "not enough arguments" is caused by trying to pass a function into another function, such as if were to call
plot_root_performance(bisection)
intending that bisection be received as a function that could then be called inside plot_root_performance . This syntax instead calls bisection with no arguments and passes whatever it returns as the first argument to plot_root_performance . If you are trying to do something like that then you should use @ to signal that you want a handle to the function passed in, like
plot_root_performance(@bisection)
More Answers (1)
Nahid Hasan
on 2 Feb 2022
Write the MATLAB code for finding out the root of this equation using Newton
Raphson method:
x3-x+1 = 0.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!