How to plot convergence graph using bisection method code

23 次查看(过去 30 天)
I am having trouble getting the convergence graph to plot using this code for bisection method.
function xc=bijection(f,a,b,tol)
f=@(x) x-cos(x);
a=0;
b=1;
fa=f(a);
fb=f(b);
tol=(0.5).*10.^(-12);
if sign(fa)*sign(fb)>0
error('f(a)f(b)<0 not satisfied')
end
while (b-a)/2>tol
c=(a+b)/2;
fc=f(c);
if fc==0
break;
elseif sign(fc)*sign(fa)<0
b=c;
fb=fc;
else
a=c;
fa=fc;
end
end
xc=(a+b)/2;
disp(c)
format long
plot(???)
I do not exactly know what I am suppose to graph. I have yet to learn this in class, I'm just trying to do a little extra. Any help would be greatly appreciated. Thank you.

回答(1 个)

Geoff Hayes
Geoff Hayes 2015-9-27
Sarah - as the bisection method is a root finding algorithm, I suspect that what you want to plot (from each iteration of the while loop) is either c or f(c) as you would want to show convergence to either the the root, c, or zero.
A couple of things - you have named your function bijection rather than bisection, and you have initialized your inputs in the function body whereas they should be initialized prior to calling the function and passed in as the inputs:
f=@(x) x-cos(x);
a=0;
b=1;
fa=f(a);
fb=f(b);
tol=(0.5).*10.^(-12);
Since the above variables are declared in the base workspace, now call your function from the command line as
bisection(f,a,b,tol)
Now, you will want to collect your c and f(c) at each iteration of the while loop. So you can do this by either creating an array (for both) and re-sizing it at each iteration of the loop, or you can pre-size the arrays given the maximum number of iterations for your while loop. You don't currently have one, but it is a good thing to have so that you don't get stuck in the loop. Try the following
maxIters = 100;
atIter = 0;
cVals = zeros(1,maxIters);
while (b-a)/2>tol && atIter < maxIters
c=(a+b)/2;
fc=f(c);
atIter = atIter + 1;
cVals(atIter) = c;
if fc==0
break;
elseif sign(fc)*sign(fa)<0
b=c;
fb=fc;
else
a=c;
fa=fc;
end
end
xc=(a+b)/2;
disp(c)
format long
cVals = cVals(1:atIter);
See in the above how we create the array of cVals, pre-sizing it for 100 elements (or the max 100 iterations of the while loop). At each iteration of the loop, we update the cVals array and increment the atIter counter. In the iteration condition, we also check to see if we have reached the maximum number of iterations - if so, then the loop is finished.
Outside of the loop, we do
cVals = cVals(1:atIter);
so that we remove all unused elements of cVals (for the case where we exit the loop prior to iterating maxIters times.
You can now plot the convergence to the root as
plot(cVals)
or the convergence to zero as
plot(f(cVals))
or both.
One final thing to consider, since you are using doubles, your equality check for
fc==0
is not guaranteed to succeed. The above condition is fine for integers, but when it comes to using floating precision values, you need to do a tolerance check instead (like you are doing in the condition of the while loop). Something like
if abs(fc)<eps
is probably sufficient.
  6 个评论
Sarah Harvey
Sarah Harvey 2015-9-27
Ok, I think I get it now. Thank you again for all the help. I'll keep mine without the plot, for the presentation, and I will try the other one, with plot, using what you have said, for my own practice. Thank you!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by