- Codice:
if f(a)*f(b)>0
error('non ci sono zeri');
else
c = (a+b)/2;
while(iter<NMAX && (((abs(x0(2)-x0(1))/max(abs(x0(1)),abs(x0(2))))>TOL) && (abs(f(c)) > eps)))
if(f(c)*f(a) < 0)
b = c;
else
a = c;
end
c = (a +b)/2;
iter = iter +1;
end
x = c;
out = [ f(c), iter ];
end
Mentre per il controllo del risultato rispetto al valore vero, ho usato fzero:
- Codice:
disp(strcat('bisection result = ',sx));
disp(strcat('fzero result = ',sx2));
disp(strcat('difference = ',num2str(abs(x-x2),'%10.15e\n')));
L'errore relativo sarebbe
- Codice:
disp(strcat('errore bisection = ',num2str(abs(3-1)/x,'%10.15e\n')));
disp(strcat('errore fzero = ',num2str(abs(3-1)/x2,'%10.15e\n')));
e vedo quale delle due funzioni restituisce l'errore più basso?