# Automated Bisection Method on a function tan(a)-tan(b)=0

3 views (last 30 days)
Hanaa Bouf Tah on 23 Oct 2021
Commented: Hanaa Bouf Tah on 23 Oct 2021
Edit: I am not looking for a full code, just tips on how to improve the code below.
Hello I am relatively new to MATLAB, and I was wondering if somebody can help me solve this equation using bissection method. I only know how to do the bisection method manually(where I have to pick the points myself and find the roots from there), and I wanted to know if there is a way to automate the function for bigger intervals.
If we have a function
and we want to find all the solutions satisfying this relation using the bisection method given that , how can I automate the function so that it gives me solutions automatically in the form of a matrix with the first colum being a values and the 2nd column being the complementary b values, or vice versa?
This is the function code:
function ftan = ftan(a,b)
ftan = tan(a)-tan(b);
end
and this the function for the manual bissection:
function [y1 y2] = bisecftan(a,y1,y2,tol)
err=abs(y1-y2);
while err>tol
x = (y1+y2)/2;
if ftan(a,x)>0
y1 = x;
elseif ftan(a,x)<0
y2 = x;
else
break
end
err = abs((y1-y2)/x);
end
end
Edit: I want to fix a starting from and going up till solving for b in a while loop and get the results in the form of a matrix.

John D'Errico on 23 Oct 2021
Edited: John D'Errico on 23 Oct 2021
I'm sorry, but it is provably impossible to make bisection (or any scheme like it) always find all solutions of an arbitrarily complicated problem. Sorry, but that is true.
However, what you might do is to evaluate your function at a regular list of points on that interval. If the function changes sign between a consecutive pair of those points, then you would use your bisection scheme, returning a root on that sub-interval. Keep a list of all the roots you have found, and you are done. That will not insure that you find all roots, but if the list of points is fine enough, then it will get most of them.
Will I write the code for you to do this? Of course not. This is your homework, not mine.
Hanaa Bouf Tah on 23 Oct 2021
Thanks :D. Will try it out.