Search code examples
matlabloopsplotbisection

Plot loop bisection method matlab


I have the next script in Matlab it's the bisection method

function root = biseccion(f,a,b,epsilon, max)

if (f(a)*f(b) >= 0)
        disp('f(a) *f(b) >= 0');
        root = 'Can't find the root';
        return;
    else
        fprintf('Iter\t\t\ta\t\t\tb\t\t\tm\t\t\tf(a)\t\t\tf(b)\t\t\tf(m)\n');
        fprintf('=========\t\t ======\t\t ======\t\t ======\t\t   ======\t\t   ======\t\t   ======\n');
        m = (a + b)/2;
        if (abs(f(m)) <= epsilon)
            root = m;
            return;
        else
            Iter = 0;
            hold on;
            while (abs(f(m)) >= epsilon  || Iter == max)
                Iter = Iter + 1; 
                m = (a + b)/2;
                if(f(a)*f(m) > 0)
                    a = m;
                else
                    b = m;
                end

                fprintf('%3d',Iter);
                fprintf('%20.4f',a);
                fprintf('%12.4f',b);
                fprintf('%12.4f',m);
                fprintf('%14.4f',f(a));
                fprintf('%16.4f',f(b));
                fprintf('%16.4f',f(m));
                plot(f(m));
                fprintf('\n');
            end
            hold off;
            root = m;
            title('Plot of error')
            xlabel('Number of iterations')
            ylabel('Error')
            grid on;
        end
 end

I need to graphic first the function (Variable f) and then plot the error (f(m)) in differents images, I tried some things but no results :/


Solution

  • Solved:

     function root = biseccion(f,a,b,epsilon, max)
    
    if (f(a)*f(b) >= 0)
            disp( f(a) *f(b) >= 0');
            root = 'Can't find root';
            return;
        else
            fprintf('Iterations\t\t\ta\t\t\tb\t\t\tm\t\t\tf(a)\t\t\tf(b)\t\t\tf(m)\n');
            fprintf('=========\t\t ======\t\t ======\t\t ======\t\t   ======\t\t   ======\t\t   ======\n');
            m = (a + b)/2;
            if (abs(f(m)) <= epsilon)
                root = m;
                return;
            else
                Iter = 0;
                figure
                hold on
                while (abs(f(m)) >= epsilon  && Iter < max)
                    Iter = Iter + 1; 
                    m = (a + b)/2;
                    if(f(a)*f(m) > 0)
                        a = m;
                    else
                        b = m;
                    end   
                    plot(Iter,f(m),'--gs',...
                    'LineWidth',2,...
                    'MarkerSize',10,...
                    'MarkerEdgeColor','b',...
                    'MarkerFaceColor',[0.5,0.5,0.5])
                    fprintf('%3d',Iter);
                    fprintf('%20.4f',a);
                    fprintf('%12.4f',b);
                    fprintf('%12.4f',m);
                    fprintf('%14.4f',f(a));
                    fprintf('%16.4f',f(b));
                    fprintf('%16.4f',f(m));
                    fprintf('\n');
                end
                 root = m;
                 title('Error graphic')
                 xlabel('Iterations')
                 ylabel('Error')
                 grid on;
            end
     end