Sie sind auf Seite 1von 7

Programa bisec

function [xr, iter]=bisec(fun, xl, xu, es, imax)


fl= fun(xl)
fu = fun(xu)

if fl*fu > 0 then


error("Error, los valores iniciales no parecen encerrar una raiz")
end
iter = 1
xr = xl;

while iter < imax

xold = xr;

xr = (xl + xu)/2;

printf("Iter: %d, xr = %e, error = %e\n", iter, xr,abs((xr-xold)/xr))

if abs(xr-xold) < abs(xr*es) then


return
end;

iter = iter+1;
fr = fun(xr)
test = fr * fl;

if test < 0 then


xu = xr;
fu = fr;
elseif test > 0 then
xl = xr;
fl = fr;
else // f(xr) = 0, tenemos la respuesta
return
end;
end;

function fun=cuad1(x)
fun = x^2-4
endfunction
function fun=cuad1(x)
fun = x^2-4
endfunction

Resultado:
falta error

paso 3:
Iteración 200
Paso 4:

Das könnte Ihnen auch gefallen