What is wrong with my code for the bisection method?

조회 수: 2 (최근 30일)
Britney
Britney 2014년 10월 7일
편집: Britney 2014년 10월 8일
x=my_bisect(@(x)x-2.5).*exp(-0.5*(x-2).^2)+0.2;[-1,0],1e-5)
a=I(-1);
b=I(0);
msg=sprintf('invalid choice of interval');
if f(a)*f(b)>0
disp(msg)
m=[(a+b)/2];
return
end
Can I just write my function in x=... above as is or do I have to have it saved in another .m file?
My syntax above is x = my_bisect(f,I,tol)
The error message that comes up is: Error: Unbalanced or unexpected parenthesis or bracket.
edit 1: changed my typo. Thanks Image Analyst.

답변 (1개)

Image Analyst
Image Analyst 2014년 10월 8일
min_bisect() is different than my_bisect(). Did you make a typo?
  댓글 수: 2
Image Analyst
Image Analyst 2014년 10월 8일
And it barely looks anything like the corrected anonymous function I gave you in http://www.mathworks.com/matlabcentral/answers/157702#answer_154243
Britney
Britney 2014년 10월 8일
Thank you for replying.
I'm doing a school task and I need to make a function that calculates f(x) on an interval [-1,0] where f(a)*f(b) changes sign and a given accuracy of the answer. So I started with making this code. The purpose of this code is to make a error message when f is not changing sign in the interval.
I'm new to Matlab. And yeah that was typo from me.

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

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by