I have a homework problem where I have to code a function for a bisection root finder. One of the inputs to the function is the handle to the function to be searched, but I'm having problems implementing it. I'm gonna simplify this from my homework, but let's say I have a test function thats:
function [y] = f(x)
y = x - cos(x);
end
And my bisection root finder is:
function [root] = bisection(func, upper_bound, lower_bound)
func_ub = @func(upper_bound);
func_lb = @func(lower_bound);
root = (func_up + func_lb)/2;
end
If I enter bisection(f,0,2) in the command window matlab returns the error:
Not enough input arguments.
Error in f (line 2)
y = x - cos(x);
I'm trying to get some values for func_ub and func_lb by plugging in my upper_bound and lower_bound into my function f, some help would be greatly appreciated. Thanks!

답변 (1개)

Rik
Rik 2018년 9월 29일

2 개 추천

Maybe you mean this?
bisection(@f,0,2)

댓글 수: 4

I think you're on the right the right track, but if I enter that now I'm getting the error:
Error: File: test.m Line: 2 Column: 16
Unbalanced or unexpected parenthesis or bracket.
It's not liking the parenthesis in @func(lb).
Rik
Rik 2018년 9월 29일
You don't need the @ in the bisection function
Jarrett Barber
Jarrett Barber 2018년 9월 29일
That worked! Thanks so much
Rik
Rik 2018년 9월 29일
You're welcome. If this solved your problem, please hit the 'accept answer' button. If not, feel free to post your remaining issues in a comment below.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

릴리스

R2017b

질문:

2018년 9월 29일

댓글:

2018년 9월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by