MATLAB: I am having a difficult time figuring out how to write a script file (non-function) to compute the “Bisection Method” root solution for a given function.

bisection methodroot solutionscript

I am having a difficult time figuring out how to write a script file (non-function) to compute the "Bisection Method" root solution for the function: "f(x)"=x*sin(3*x)+2. given that X_L(first lower boundary)=3, X_U(first upper boundary)=4, Es=0.001,Nmax=4. The two possible conditions for stopping execution are: 1) when "|Xrnew-Xrold|<Es" 2)when "iteration count>Nmax" Clearly indicate which of the two stop execution conditions has been met.
I would greatly appreciate any help, advice or anything at all.Thank you guys so much for your consideration.

Best Answer

Lets look at your problem. The function is in f, and we are trying to find a root that lies in the interval [3,4]. I'll put a . in there, so the function can be used in a vectorized form. That will make ezplot happier. I'm happy to see that you knew how to make a function handle.
f=@(x) x.*sin(3*x)+2;
ezplot(f,[3 4])
grid on
A good piece of advice is to always plot EVERYTHING. Look at what you are doing, to see if it makes sense.
Indeed, there is a root in the interval [3,4]. We almost had two roots, which would have been a problem, because intervals with two roots will usually have you missing the fact that there is any root at all, since the end points of the interval will have the same sign for the function value. Then schemes like bisection will fail.
X_L=3;
X_U=4;
fX_L=f(X_L);
fX_U=f(X_U);
Use the function f to evaluate the function! In your code, you did not do that. You hard coded the function evaluations. The point is, if you wanted to change your code to work for a different function, you want to be able to change only ONE line.
To be honest, I'll follow the code you started as a template, but I'd not write it like that myself. I'd create a vector of size 1x2 to hold the endpoints of the interval. When you create multiple variables with names like that, you are asking for buggy code. Use vectors and arrays wherever it makes sense. Your code will improve for doing so.
You had a for loop AND a while loop there. So I'll assume you wanted to set a maximum number of iterations.
itermax = 10;
iter = 0;
tol = 0.001;
fX_r = inf; % make it do at least one iteration
while (abs(fX_r) > tol) && (iter < itermax)
% compute the interval midpoint
X_r = (X_L+X_U)/2;
% and evaluate f at the new point
fX_r = f(X_r);
% keep track of the iteration count
iter = iter + 1;
% which half of the interval does the root lie in?
if (fX_r*fX_L) < 0
% it must be below X_r, because there is a sign change
% therefore, replace x_U
X_U = X_r;
fX_U = fX_r;
else
% it must be above X_r, because there is a sign change
% therefore, replace x_L
% This is the case that would catch fX_r EXACTLY 0
X_L = X_r;
fX_L = fX_r;
end
end
This should work. It is reasonably close to where you were.
Somethings I did, I added comments. LOTS of comments are tremendously valuable. They make your code readable. They make it possible to debug.
When you replaced the end point, you needed to also save the function value at that endpoint. You also needed to update the correct endpoint. In your original script, that was a problem.
In the end though, what I did was not that far from the script you had already. You need to learn to think through the logic carefully. Sometimes it can help to write a flowchart on something that is really confusing to work though.
Related Question