Concetti Chiave
- The bisection method function determines an approximate solution for a given function within a specified interval.
- Input parameters include interval endpoints, the function to evaluate, and a tolerance level for error acceptance.
- Outputs generated are the approximate solution, the function value at this point, the number of iterations, and the error in approximation.
- The code employs graphical plotting for function visualization and error tracking across iterations.
- Conditions to apply the method are checked, ensuring the function changes sign over the interval.
function[c,fc,it,err]= bisezioni(a,b,fun,toll)
%%%METODO DELLE BISEZIONI
%%% %%%Dati di input %%% a,b = estremi dell'intervallo %%% fun = funzione f %%% toll = tolleranza sull'errore per accettare la soluzione %%% %%%Dati di output %%% c = soluzione approssimata %%% fc = valore di f in c %%% it = iterate necessarie per approssimare c %%% err = errore commesso nell'approssimazionesubplot(1,2,1),
fplot(fun,[a b]), hold on,it=0;
%i valori della funzione negli estremi sono fa = feval(fun,a); fb = feval(fun,b);subplot(1,2,1)
plot(a,fa,'r*',b,fb,'r*'),hold on,%verifico se sono soddisfatte le ipotesi del teorema di esistenza degli zeri
if(fa*fb)>=0 disp('Il metodo non è applicabile o lo zero è un estremo dell''intervallo'); else err = abs(a-b); subplot(1,2,2), semilogy(it,err,'r*'), hold on, %blocco iterativo della funzione while(ittoll) it = it+1; c = (a+b)/2; fc = feval(fun,c); if(fa*fc) b=c; else a=c; end err = abs(a-b); subplot(1,2,2), semilogy(it,err,'g*'), hold on, end fa = feval(fun,a); fb = feval(fun,b); subplot(1,2,1), plot(a,fa,'bo',b,fb,'bo'), subplot(1,2,2) semilogy(it,err,'g*'), hold on end subplot(1,2,1), hold off subplot(1,2,2), hold off