Hello guys, im not very good with matlab and i just have to ask a quick question. I need to put a plot of the relative error of the bisection method vs the iteration count. 'You should also include a plot of the relative error as a function of the iteration number. ' is what my teacher said. Here is the code I have at the moment.
if true f = @(x) cos(x)-x; x1 = 0.7; x2 = 0.8; TOL = 1e-4; counter = 0; while abs(x2-x1) > TOL xmid = (x2+x1)/2; if (f(x2)*f(xmid)) <0 x2 = xmid; else x1 = xmid; relerr = abs(x1 - x2) / xmid counter = counter + 1; endfigurehold onplot(counter, relerr)axis ([0 11 0 0.0002])
Now whenever i try to create the plot i get something that is completely empty. And if i put the create plot command inside the while loop, i get 11 empty plots. i use an fprintf command to print out results for error, iterations and the root of the fn and they all work fine, but the only thing i cant get to work is the plot. Anyone got any ideas?
Best Answer