Código Scilab – Método de la Bisección



function y=f(x)
y=exp(-x^2)-x;
endfunction


function xr=biseccion(xai,xbi,tol)
i=1;
ea(1)=100;
if f(xai)*f(xbi) < 0
    xa(1)=xai;
    xb(1)=xbi;
    xr(1)=(xa(1)+xb(1))/2;
    printf('It.\t\t Xa\t\t Xr\t\t Xb\t  Error  \n');
    printf('%2d \t %11.7f \t %11.7f \t %11.7f \n',i,xa(i),xr(i),xb(i));
    while abs(ea(i)) >= tol
      if f(xa(i))*f(xr(i))< 0
         xa(i+1)=xa(i);
         xb(i+1)=xr(i);
      end
      if f(xa(i))*f(xr(i))> 0
         xa(i+1)=xr(i);
         xb(i+1)=xb(i);
	  end      
      xr(i+1)=(xa(i+1)+xb(i+1))/2;
      ea(i+1)=abs((xr(i+1)-xr(i))/(xr(i+1))*100);
      printf('%2d \t %11.7f \t %11.7f \t %11.7f \t %7.3f \n',i+1,xa(i+1),xr(i+1),xb(i+1),ea(i+1));
      i=i+1;
   end
else
   printf('No existe una raíz en ese intervalo');
end
endfunction
Anuncios