bisection method - can't see error in code

I am unable to pin point the error in my code of my funtion for the bisection method, any help would be greatly appreciated
function [rootx]=bisect(fhandle,a,b,epsilon)
rootx=[];
Fa=fhandle(a);
Fb=fhandle(b);
m=((b-a)/2);
Fm=fhandle(m);
while abs(m)>epsilon ;
if Fb*Fm <0
a=m
else
b=m
end
end
return m=((a+b)/2)
end
rootx

2 件のコメント

Matt J
Matt J 2012 年 10 月 3 日
編集済み: Matt J 2012 年 10 月 3 日
Not pasting in the error messages or other symptoms of malfunction makes it hard for us to pinpoint, too.

サインインしてコメントする。

 採用された回答

James Tursa
James Tursa 2012 年 10 月 3 日

2 投票

1) Google "bisection method"
2) Go to the 1st Wiki link
3) Look at the pseudo-code posted there
4) Compare that to your code to discover your errors, paying special attention to what Matt J said about not updating your variables within your loop.

その他の回答 (2 件)

Matt J
Matt J 2012 年 10 月 3 日
編集済み: Matt J 2012 年 10 月 3 日

0 投票

One idea:
m=((b-a)/2);
Shouldn't it be + instead of - ?
Also, you don't assign anything to rootx in the code except the initialization rootx=[]. Also, throughout the loop, you are not updating, m, Fb, or Fm.
Babak
Babak 2012 年 10 月 3 日

0 投票

In the while loop
while abs(m)>epsilon ;
if Fb*Fm <0
a=m
else
b=m
end
end
you are not changing m and you are trying to check the abs(m)
I think you meant
m=a
and
m=b
instead...

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by