%% Bisection methof for sinking float
%
% First, define a function fallingfloat.m that evaluates the 
% following equation
%
% $f(m)=\sqrt{\frac{g(m-m_w)}{C}}\tanh(\sqrt{\frac{gC(m-m_w)}{m^2}}t)-w(t)$
%
% with
%
% $C = \frac{\rho_wC_DA}{2}$
%
% and with a gravitational acceleration $g=9.81 \mathrm{m s}^{-2}$.
% Then set our parameters.

C = 1035*0.25*0.2/2;
mw = 207;
t = 10;
w = 2;

%%
% First, plot the function and guess the root and an upper and lower limit
% for the bisection method. Note that $m>m_w$, otherwise the float would be
% rising and the arguments in the square roots would be negative.

x = linspace(mw,mw+30);
plot(x,fallingfloat(x,mw,C,t,w),'.-')
grid
xlabel('Mass of the float, m [kg]')
ylabel('f(m)')

%%
% Now, use the bisection script to approximate the root. 

m = bisect(@fallingfloat,mw,mw+20,[],[],mw,C,t,w)
hold on
plot(m,0,'r.')

%%
% Test by inserting the value of m into our function.

fallingfloat(m,mw,C,t,2)+w



