Why is slow?

12 ビュー (過去 30 日間)
Eryk Fernandes
Eryk Fernandes 2016 年 5 月 21 日
コメント済み: the cyclist 2016 年 5 月 22 日
Hey guys, i'm trying to run this script but it takes too long. Can i change anything to make it faster?
Note: I've already tried in another computer.
a=1560;
b=6360;
tol=500;
it= 50;
g=@(P)(3*10^7*(P^(-0.6)+7/P)+800*P^0.7+400000);
x1=a+(1/4)*((b-a)/4);
x2=a+(2/4)*((b-a)/4);
x3=a+(3/4)*((b-a)/4);
g_x1=g(x1);
g_x2=g(x2);
g_x3=g(x3);
while (abs(b-a)>tol)
if g_x1<g_x2
b=x2;
x2=x1;
x1=a+(1/4)*((b-a)/4);
g_x1=g(x1);
g_x2=g(x2);
g_x3=g(x3);
elseif g_x3<g_x2
a=x2;
x2=x3;
x3=a+(3/4)*((b-a)/4);
g_x1=g(x1);
g_x2=g(x2);
g_x3=g(x3);
else g_x3>g_x2
a=x1;
b=x2;
g_x1=g(x1);
g_x2=g(x2);
g_x3=g(x3);
end
end

採用された回答

the cyclist
the cyclist 2016 年 5 月 21 日
The problem is not that your code is slow, but that your algorithm doesn't converge.
abs(b-a) reaches 1925.3906, and stays there, so the while loop doesn't end.
  2 件のコメント
Eryk Fernandes
Eryk Fernandes 2016 年 5 月 21 日
Thank you, but i can't see why it don't converge
the cyclist
the cyclist 2016 年 5 月 22 日
Do you have a reference for the algorithm you are using? Maybe you coded it incorrectly.
Also, I am quite sure that this line ...
else g_x3>g_x2
is not what you intended. That line is not going to check any condition. It is going to execute the code
g_x3>g_x2
and just display 1 or 0 (i.e. true or false).

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by