Using the bisection method, why is f(a) not a real integer or logical?

3 views (last 30 days)
I am trying to use the bisection method to optimize the cost of the pipeline, but I can't get a value for f(a)? What am I doing wrong with my while loop or if statement?
function [x] = myPipeBuilder(C_ocean, C_land, L, H)
%L=length of x, C_ocean=cost of pipeline in ocean, C_land=cost of pipeline on land, H=some distance in water
a=0;
b=L;
xM = (a + b)/2;
f(xM)= (C_ocean/sqrt(H^2+xM^2))-C_land;
% derivative of function
while abs(f(xM))>1*10^(-6)
% tolerance
if sign(f(a))==sign(f(xM))
a=xM;
elseif sign(f(b))==sign(f(xM))
b=xM;
end
xM = (a + b)/2;
end
x=xM;
end

Accepted Answer

Walter Roberson
Walter Roberson on 20 Mar 2014
You have the statement
f(xM)= (C_ocean/sqrt(H^2+xM^2))-C_land;
that does not define a function f(xM), it defines an assignment to the array elment f(xM) where xM would have to be a valid subscript. subscripts can only be non-negative integers. You calculated xM on the line above as xM = (a + b)/2; and we have no reason to expect that the result is going to be an integer. For example if L had been passed in as 19 then since b=L and a=0, then (a+b)/2 would be (0+19)/2 which would be 9.5 which would not be an integer.

More Answers (1)

Andrei Bobrov
Andrei Bobrov on 20 Mar 2014
x = sqrt((C_ocean/C_land).^2-H^2);

Categories

Find more on Visualization in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!