-1

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 :/

Edgar Pisxid
  • 69
  • 1
  • 10
  • I am not really sure what "no results" mean. However you can show images with `image()`. Try to describe why things gets wrong. Also, MATLAB is a weakly typed language and it is not (for example) sure that `f` really is a function handle (since you say you have a problem). This means that you must describe input. Also, what does "graphic the function" mean? – patrik Apr 15 '16 at 04:50

1 Answers1

0

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
Edgar Pisxid
  • 69
  • 1
  • 10