How can I use the Bisection method to converge on an arbitrary value?
Show older comments
I am working on coding a function to solve for the water level inside a water tank. The volume of the tank is described by:
V = pi * x^2 *((3*R-x)/3)
I have R = 4 m and I need to find the height (x) where V = 100 m^3. I chose to use the Bisection Method to find this height and use a while loop that should end when the height that gives out Volume equal to 100 m^3. Though I am having a hard time getting the loop to run more than just once and I don't know what could be going wrong. I'm new to Matlab so I might be missing something very obvious but any help would be appreciated.
Here is the code that I have managed to come up with so far:
% Define function
R = 4;
v = @(x) pi.*x.^2.*((3.*R-x)./3);
% Define variables
xL = 0;
xH = 2.*R;
vL = v(xL);
vH = v(xH);
% Define xr
xr = .5.*(xL+xH); % Bisection method
vR = v(xr);
% Define the loop's target value
tar = 100;
while v(xr) == tar
xr = .5.*(xL+xH);
% Update xH and xL
if vH>vR
xH = xr;
else
xL = xr;
end
end
root = xr;
Once again thanks for any help it is very appreciated
Accepted Answer
More Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!