problems using bisection method to find a maximum

while abs(a-b)>.001
c = (a + b)/2;
if (abs(y_int(1,find(abs(((c./x_int)-1))<.0001))-maxP)<.001)
break;
elseif (y_int(1,(find(abs(((a./x_int)-1))))<.001) > y_int(1,(find(abs(((b./x_int)-1))))<.001))
b=c;
else
a=c;
end
end
So, I want a and b to converge upon a max, and yet they always seem to go to the minimum in the domain i specify. I'm using a spline, not a specific equation. Just to clarify the if and elseif lines. The if line says, if the power that I get at point c is really close to the maximum Power then stop. The elseif line says, if the power at A is greater than the power at B, then make b equal to c, otherwise make a=c. Can anybody see whats wrong?

1 件のコメント

Walter Roberson
Walter Roberson 2011 年 9 月 28 日
I recommend you calculate
t = abs(((c./x_int)-1));
right after you assign c in the loop, and that you then replace the occurrences of that expression with the variable.
I also recommend you switch to logical indexing instead of using find().
I think the code will come out cleaner, more readable, and faster.

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

回答 (1 件)

Mathew
Mathew 2011 年 9 月 29 日

0 投票

thank you Walter Robertson. After changing those few things and scratching my head for a few minutes, I figured that some of the brackets were in the wrong position.

カテゴリ

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

質問済み:

2011 年 9 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by