ricerca soluzione con bisezione

esci algebraturbo.htm

program bise3b;
(* calcolo radici equazione con metodo bisezione *)
uses crt;
var A,B,MEDIO,AMPIEZZA,ERRORE,X,Y: real;

function f(X: real): real;
begin
(* scrivere la funzione che interessa e racchiudere altre *)
(* f := X*X*X*X-7*X*X*X+2*X*X-100*X+610 *)
(* prova sinistro 4 destro 5 errore 0.001 *)
f:=x*x*x-9*x+1
(* prova sinistro -4 destro -3 errore 0.0001 *)
end;



procedure legge_dati;

begin
repeat
writeln('esempio funzione:x^3-9x+1...prova valori indicati');
gotoxy (25,13); write ('Estremo sinistro -4: '); read (A);
gotoxy (25,14); write ('Estremo destro -3: ');read(B);
until f(A)*f(B) < 0; {si accettano solo valori discordi di f(x)}
gotoxy (25,15); write ('Errore 0.0001= ');read(ERRORE);
end;


procedure calcola (ESTREMO_S, ESTREMO_D: real);

begin

MEDIO := (ESTREMO_S+ESTREMO_D)/2;


if f(ESTREMO_S)*f(MEDIO) <= 0 then

ESTREMO_D := MEDIO
else
ESTREMO_S := MEDIO;


AMPIEZZA := ESTREMO_D - ESTREMO_S;

write('Estremo sin.: ', ESTREMO_S :5:8);
writeln(' Estremo des.:',ESTREMO_D :12:8);


if AMPIEZZA < ERRORE then
begin
writeln;
write ('Radice approssimata: ',MEDIO :0:8);
writeln (' Errore: ',ERRORE :0:9)
end
else
calcola (ESTREMO_S,ESTREMO_D);

end;

begin
clrscr;
legge_dati;
clrscr;
writeln('A= ',A:5:6,' B= ', B :12:6);
readln;
calcola (A,B);
writeln('premi enter');
readln;
end.