What is the Bisect Function and How Does it Work?

  • Thread starter jdawg
  • Start date
  • Tags
    Function
In summary, the conversation is about defining a bisect function in MATLAB. The individual is not sure how to format the code to make it work with their problem. They have provided their attempt at a solution and included the function and its parameters.
  • #1
jdawg
367
2

Homework Statement


I'm not really sure how to define my bisect function.. I included my work and question in the images attached.

Homework Equations

The Attempt at a Solution

 

Attachments

  • pitch.png
    pitch.png
    53 KB · Views: 457
  • w2.png
    w2.png
    9.2 KB · Views: 404
Last edited:
Physics news on Phys.org
  • #2
I guess I was thinking it was a built in function in matlab, I've tried to look at a few examples of bisection codes and I'm not super sure how to format it to make it work with this problem.
 
  • #3
function [root,fx,ea,iter]=bisect(func,xl,xu,es,maxit,varargin)
% bisect: root location zeroes
% [root,fx,ea,iter]=bisect(func,xl,xu,es,maxit,p1,p2,...):
% uses bisection method to find the root of func
% input:
% func = name of function
% xl, xu = lower and upper guesses
% es = desired relative error (default = 0.0001%)
% maxit = maximum allowable iterations (default = 50)
% p1,p2,... = additional parameters used by func
% output:
% root = real root
% fx = function value at root
% ea = approximate relative error (%)
% iter = number of iterations
if nargin<3,error('at least 3 input arguments required'),end
test = func(xl,varargin{:})*func(xu,varargin{:});
if test>0,error('no sign change'),end
if nargin<4|isempty(es), es=0.0001;end
if nargin<5|isempty(maxit), maxit=50;end
iter = 0; xr = xl; ea = 100;
while (1)
xrold = xr;
xr = (xl + xu)/2;
iter = iter + 1;
if xr ~= 0,ea = abs((xr - xrold)/xr) * 100;end
test = func(xl,varargin{:})*func(xr,varargin{:});
if test < 0
xu = xr;
elseif test > 0
xl = xr;
else
ea = 0;
end
if ea <= es | iter >= maxit,break,end
end
root = xr; fx = func(xr, varargin{:});
 

1. What is the bisect function used for?

The bisect function is used in computer science and mathematics to find the root of a function or to divide a list of values into two parts based on a certain condition.

2. What are some common errors when using the bisect function?

Some common errors when using the bisect function include not specifying the correct parameters, using the function on a non-sorted list, and not handling edge cases properly.

3. How does the bisect function work?

The bisect function uses a binary search algorithm to divide a list into two parts and then recursively searches the appropriate part until it finds the desired value or meets a certain condition.

4. Can the bisect function be used for more complex data types?

Yes, the bisect function can be used for more complex data types such as custom objects, as long as the objects have a defined comparison function.

5. Are there any alternatives to the bisect function?

Yes, there are alternatives to the bisect function such as using a for loop to iterate through a list, using the built-in functions max() and min() to find the maximum and minimum values, or using the sorted() function to sort a list before using the bisect function.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
993
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
783
Replies
1
Views
9K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
839
Back
Top