How to use Bisection Program to locate Roots of given function

The problem gives a differential equation and asks to find the roots using Bisection Method implimented into a MatLab function. I believe I have the correct program typed in MatLab for finding roots using bisection but I am struggling with how to input the given equation and find results. The problem is attached as a pdf and my code for the program is below: Any help is appriciated, Thank you!
function[root, fx, ez, iter] = bisect(func, x1, xu, es, maxit, varargin)
% bisect: root location zeros
%% input
%func = name of function
%x1, xu = lower and upper guesses
%es = desired relative error
%maxit = max iterations
%p1,p2 = additional parameters used by func
%%output
%root = real root
%fx = function value at root
%ea = approx. relative error
%iter = number of iterations
if nargin<3, error('at least 3 input arguments required'), end
test = func(x1,varargin{:})*func(xu,varargin{:});
if test>0, error('no sign change'), end
iter=0; xr=x1; ea=100;
while(1)
xrold=xr;
xr=(x1+xu)/2;
iter=iter+1;
if xr~=0, ea=abs((xr-xrold)/xr)*100;end
test=func(x1,varargin{:})*func(xr,varargin{:})
if test<0
xu=xr;
elseif test>0
x1=xr;
else
ea=0;
end
if ea<=es | iter>=maxit,break,end
end
root=xr; fx=func(xr, varargin{:})

7 Comments

What does the bisection method, which finds roots of an equation, have to do with solving a differential equation? Can you post the actual wording of the assignment you have been given?
Sorry what I meant was finding the roots of the differental equation. Thank you!
What is a root of a differential equation?
Maybe I am using incorrect vocabulary, looking for the input values that make the function equal to zero
Still not clear. Differential equations describe rate of change of something with respect to another variable. E.g., rate of change of position with respect to time. Finding a root of this doesn't really make sense. Can you post the complete actual wording of your assignment so we can figure out what is being asked of you?
We are looking for when the amount of liquid in the tank is at a maximun/minimum. Since the differential equation given to us describes the rate at which water enters/leaves the tank as a function of time, the zeros of the equation corrospond to the time when there is maximun/minimun water in the tank. The .pdf attached to my question shows the diagram and wording of the problem
"... when the amount of liquid in the tank is at a maximun/minimum ..."
OK, now I see what you are trying to do.

Sign in to comment.

Answers (2)

One thing I could see is that you are trying to check exactly when the function is zero
else
ea = 0
Numerically it's unlikely that an aritrary function will yield zero. You need to check when the function is approximately zero. A 11 line implementation of the bisection method is found here:
If the derivative depended only on the time t then your strategy for finding the maximum/minimum points would be a good one. Just finding the roots of the derivative function would get you what you want.
However, this approach probably isn't going to give you anything useful in your particular situation because your derivative depends on both the time t and the state y. You would be solving this equation:
0 = 3*(Q/A)*sin(t)^2 - (alpha/A)*(1+y)^1.5
Or, equivalently with Q = 600 and alpha = 120
sin(t)^2 = (1/15)*(1+y)^1.5
The thing is, there are an infinite number of solutions to the above equation. It is not clear from the drawing how far negative y can be, but for a large percentage of t values you can find a y value that satisfies the above equation. You wouldn't even need the bisection method for this because you can solve for the solution directly (i.e., given a t value you can solve directly for y immediately). So a large percentage of t values would have a corresponding y value that makes the derivative 0. But do these infinite number of solutions actually mean anything to you? Probably not. Whether your system actually gets into one of those infinite solution states will depend entirely on initial conditions. Of the infinite solutions available, only some of those states get achieved. The only thing the above equation tells you is that if at time t the tank level is a particular y value, then the tank level will be at a local maximum/minimum. What it doesn't tell you is if that particular state will ever be achieved, because actually getting into that state will depend on the initial conditions. It also doesn't tell you what the initial conditions have to be to get into a particular maximum/minimum state at a specific time t.
What you probably need to do instead is run simulations of this with specific initial conditions, e.g. with ode45( ), and then look for the maximum/minimum points in the numeric solutions. Were you given initial conditions for this problem? I.e., a starting y value?

4 Comments

The .pdf attached to my initial post shows the problem statement. Part of the problem reads
"Assume that the coordinate y is selected so as y=1 corresponds to a local maximum of depth. Find the smallest positive value of time t, for which Eq. (2) is satisfied"
So we really are looking for the time when the first minimum occurs. In addition the problem statement requires the use of bisection method with stopping criterion of Error=0.05%. I tried to write a generalized code that could perform bisection method on any input function. I think my code is correct (any tips are appriciated if it is not) but I am struggling with actually using the code to solve my problem.
Thank you!
They are telling you that y=1 is a local maximum, so just plug in y=1 into the above equation I showed you and solve for t. I.e., find the smallest t that satisfies this:
sin(t)^2 = (1/15)*(1+1)^1.5
I still have no clue why the bisection method is being specified since you can solve this equation directly for t, but I guess this is an exercise in coding the bisection method.
Do you have any insight on how I would be able to use my code to find this value of t? Or if I have coded the program correctly to perform bisection method? I understand it is not the most efficient method to solve this particular problem, but it is required by the problem statement.
I would assume you simply pass a function handle that you are trying to drive to 0. E.g.,
func = @(t) sin(t).^2 - (1/15)*(1+1)^1.5

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Products

Release

R2018b

Asked:

on 4 Feb 2021

Commented:

on 8 Feb 2021

Community Treasure Hunt

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

Start Hunting!