Hi everybody! im trying to solve a boiling problem with bisection method. but there is something wrong.
Question is:
To solve it, i wrote these codes: but there is an error (too many input arguments)
function bisection(a, b, eps)
iter=0;
global k A B C
k=[0.05 0.15 0.5 0.3];
A=[15.8333 15.8366 15.8737 15.9426];
B=[2477.07 2697.55 2911.32 3120.29];
C=[-39.94 -48.78 -56.51 -63.63];
xm = (a+b)/2;
x=[a b xm];
f = fonk(x);
for i=1:4
exp(A-B/(x+C))*k*133.32-1e5;
end
while abs(a-b)>eps
if f(1)*f(3) <=0
b = xm;
elseif f(2)*f(3)<=0
a = xm;
else
disp('You have a ill conditioned function or multiple root');
end
iter=iter+1;
end
fprintf('Bulunan kök değeri (sürtünme katsayısı) = %8.5f \n', (a+b)/2);
fprintf('%i. iterasyonda sonuca ulaşıldı. \n', iter);
end
function [f] = fonk(x)
global k A B C
f=exp(A-B/(x+C))*k*133.32-1e5;
end

5 件のコメント

darova
darova 2020 年 4 月 18 日
Why are using x here instead of T?
function [f] = fonk(x)
global k A B C
f=exp(A-B/(x+C))*k*133.32-1e5;
end
I don't see you defined T somewhere
Arctgx
Arctgx 2020 年 4 月 20 日
I think it doesn't matter using x or T variable. The problem is not x or T. The error is (too many input arguments) in Matlab.
darova
darova 2020 年 4 月 20 日
編集済み: darova 2020 年 4 月 20 日
I don't understand which value you are trying to find
Ameer Hamza
Ameer Hamza 2020 年 4 月 20 日
Which line gives this error? What are the values of a, b, and eps when you call this function? vector A, B, and C are 1x4, but x is 1x3 so this expression
exp(A-B/(x+C))*k*133.32-1e5;
will also give error.
Arctgx
Arctgx 2020 年 4 月 20 日
darova, i used "k" for x. and i used "x" for T. i want to find mixture's boiling point.
Ameer, what is the solution?

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

 採用された回答

Ameer Hamza
Ameer Hamza 2020 年 4 月 20 日
編集済み: Ameer Hamza 2020 年 4 月 20 日

2 投票

Try this code. Check the differences with your code to see the mistakes
x = bisection(300, 400, 0.001);
function xm = bisection(a, b, eps)
iter = 0;
global k A B C
k=[0.05 0.15 0.5 0.3];
A=[15.8333 15.8366 15.8737 15.9426];
B=[2477.07 2697.55 2911.32 3120.29];
C=[-39.94 -48.78 -56.51 -63.63];
while abs(a-b)>eps
xm = (a+b)/2;
if fonk(a)*fonk(xm) <=0
b = xm;
elseif fonk(xm)*fonk(b)<=0
a = xm;
else
disp('You have a ill conditioned function or multiple root');
end
iter=iter+1;
end
fprintf('Bulunan kök değeri (sürtünme katsayısı) = %8.5f \n', (a+b)/2);
fprintf('%i. iterasyonda sonuca ulaşıldı. \n', iter);
end
function [f] = fonk(x)
global k A B C
f=sum(exp(A-B./(x+C)).*k*133.32)-1e5;
end

9 件のコメント

Arctgx
Arctgx 2020 年 4 月 20 日
Thank you so much Ameer! Now i saw the mistakes.
1) First mistake, scaler and vector multiplication.
2) Second mistake, about fonk(x)
3) Third mistake, forgetting to use sum function
Ameer Hamza
Ameer Hamza 2020 年 4 月 20 日
Correct. I am glad that my code is helpful.
GeotechnicalEngr
GeotechnicalEngr 2020 年 4 月 23 日
Hi,i have a question. When we define 'while' part and use the code that Arctgx shared,
while abs(a-b)>eps
if f(1)*f(3) <=0
b = xm;
elseif f(2)*f(3)<=0
a = xm;
else
what can i do for not to get the 'Matrix dimensions must agree' error?
Ameer Hamza
Ameer Hamza 2020 年 4 月 23 日
How is the function f defined?
GeotechnicalEngr
GeotechnicalEngr 2020 年 4 月 23 日
編集済み: GeotechnicalEngr 2020 年 4 月 23 日
function bisection(a, b, eps)
iter=0;
a=330;
b=398.75;
eps=1e-5;
iter=0;
global k A B C
k=[0.05 0.15 0.50 0.30];
A=[15.8333 15.8366 15.8737 15.9426];
B=[2477.07 2697.55 2911.32 3120.29];
C=[-39.94 -48.78 -56.51 -63.63];
while abs(a-b)>eps
xm = (a+b)/2;
x=[a b xm];
f = fonk(x);
if f(1)*f(3) <=0
b = xm;
elseif f(2)*f(3)<=0
a = xm;
else
disp('You have a ill conditioned function or multiple root');
end
iter=iter+1;
end
fprintf('Root value) = %8.5f \n', (a+b)/2);
end
function [f] = fonk(x)
global k A B C
f=sum(exp(A-B./(x+C)).*k*133.32)-1e5;
end
I wrote these code and get Matrix dimensions must agree error.
Ameer Hamza
Ameer Hamza 2020 年 4 月 23 日
GeotechnicalEngr, you are using Arctgx code. Check the code in my answer to see how to correct this issue.
GeotechnicalEngr
GeotechnicalEngr 2020 年 4 月 23 日
Thanks for your answer. But i know that i am using Arctgx code. What my teacher want is not to do any changes when coding bisection method part. That's why i am trying to solve the problem this way, perhaps he has overlooked something.
By the way i tried your code and it works. Thanks.
Ameer Hamza
Ameer Hamza 2020 年 4 月 23 日
try this
x = bisection(300, 400, 0.001);
function xm = bisection(a, b, eps)
iter=0;
a=330;
b=398.75;
eps=1e-5;
iter=0;
global k A B C
k=[0.05 0.15 0.50 0.30];
A=[15.8333 15.8366 15.8737 15.9426];
B=[2477.07 2697.55 2911.32 3120.29];
C=[-39.94 -48.78 -56.51 -63.63];
while abs(a-b)>eps
xm = (a+b)/2;
x=[a b xm];
f(1) = fonk(x(1));
f(2) = fonk(x(2));
f(3) = fonk(x(3));
if f(1)*f(3) <=0
b = xm;
elseif f(2)*f(3)<=0
a = xm;
else
disp('You have a ill conditioned function or multiple root');
end
iter=iter+1;
end
fprintf('Root value) = %8.5f \n', (a+b)/2);
end
function [f] = fonk(x)
global k A B C
f=sum(exp(A-B./(x+C)).*k*133.32)-1e5;
end
GeotechnicalEngr
GeotechnicalEngr 2020 年 4 月 23 日
Code is working now. Thank you so much for your help!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Startup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by