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
댓글 수: 0
채택된 답변
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
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
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
2022년 2월 2일
Write the MATLAB code for finding out the root of this equation using Newton
Raphson method:
x3-x+1 = 0.
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!