Need help with my bisection code

3 次查看(过去 30 天)
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

采纳的回答

Marc Jakobi
Marc Jakobi 2016-10-8
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 个评论
Marc Jakobi
Marc Jakobi 2016-10-8
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
Walter Roberson 2016-10-8
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)

请先登录,再进行评论。

更多回答(1 个)

Nahid Hasan
Nahid Hasan 2022-2-2
Write the MATLAB code for finding out the root of this equation using Newton
Raphson method:
x3-x+1 = 0.

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by