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

2 views (last 30 days)
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.

Answers (1)

John D'Errico
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.
  3 Comments
John D'Errico
John D'Errico on 23 Oct 2021
Edited: John D'Errico on 23 Oct 2021
Why would a while loop help? It seems to me that WERE I ever be willing to teach a class on programming, when I introduce the concept of loop, I would introduce for and while together. I would explain that a for loop is correct when you KNOW how long the loop will run. A while loop is correct when you don't.
For example, suppose I wanted to loop over the numbers 1 through 100. Would you use a for loop or a while loop? Clearly, a for loop.
Suppose I asked you to find the next prime number after N=100? Here, I don't know how far out I will need to search. I would use a while loop for that problem.
In your case, I told you to break the interval [0,14*pi], into a fixed number of smaller intervals. Linspace will do that well. How many sub-intervals are there? Would you use a for loop or a while loop?
Do as I said. Find each smaller interval that shows a zero crossing. A for loop will do this best. Check the function value for each consecutive pair of function values, inside the for loop. Whenever you see a zero crossing, call your bisection code. Save the most recent result at the end of a vector of all solutions.
Could this all be done in a vectorized way? Yes, but writing that code woud be far beyond what you can handle. Does it need to be done that way? No.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements 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!