Hallo,
ich habe es mit Lazarus getestest.
Dabei werden einfach nur mal die Startwerte x1,x2 getauscht.
Eine Form mit Button1 und Memo1( schön breit).
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82:
| const absDELTA = 1e-6;
type
TForm1 = class(TForm) Button1: TButton; Memo1: TMemo; procedure Button1Click(Sender: TObject); private public end;
var Form1: TForm1;
implementation
function f(x:double):double; begin result := exp(x)*sin(x); end;
function RegulaFalsi(x1,x2:double):double; var x0,f_x0,f_x1,f_x2: double; begin f_x1:= f(x1); f_x2:= f(x2); If f_x1*f_x2 > 0 then exit; repeat X0 := X1 - f_x1* ( (X2 - X1) /(f_x2-f_x1) ); f_x0 := f(X0); FOrm1.Memo1.lines.Add(Format('f(%0.2f) = %0.3f | f(%0.2f) =%0.3f |f(%0.4f) = %e',[x1,f_x1,x2,f_x2,x0,f_x0]));; if f_x0 = 0 then Break else if f_x0*f_x2 > 0 then begin X2 := X0; f_x2 := f_x0; end else begin x1 := x0; f_x1 := f_x0; end; until ABS(f_x0) < absDELTA;
result := x0; end;
procedure TForm1.Button1Click(Sender: TObject); Var x1,x2:double; begin x1 := 2.0; x2 := pi+1.0; Memo1.lines.Add(Format('%5.2f %5.2f %e',[x1,x2,RegulaFalsi(x1,x2)])); x1 := x2; x2 := 2.0; Memo1.lines.Add(Format('%5.2f %5.2f %e',[x1,x2,RegulaFalsi(x1,x2)])); end; |
das scheint zu funktionieren.
Man sieht das Regula falsi sich nur von einer Seite annähert und deshalb langsam konvergiert.
[code}
Memo1
f(2,00) = 6,719 | f(4,14) =-52,931 |f(2,2412) = 7,36921735523644E+000
f(2,24) = 7,369 | f(4,14) =-52,931 |f(2,4735) = 7,34962934533402E+000
f(2,47) = 7,350 | f(4,14) =-52,931 |f(2,6769) = 6,51636966422036E+000
f(2,6

= 6,516 | f(4,14) =-52,931 |f(2,8374) = 5,11314642176185E+000
f(2,84) = 5,113 | f(4,14) =-52,931 |f(2,9523) = 3,60339686804760E+000
f(2,95) = 3,603 | f(4,14) =-52,931 |f(3,0281) = 2,33950391238947E+000
f(3,03) = 2,340 | f(4,14) =-52,931 |f(3,0752) = 1,43598425173683E+000
f(3,0

= 1,436 | f(4,14) =-52,931 |f(3,1034) = 8,50536385191576E-001
f(3,10) = 0,851 | f(4,14) =-52,931 |f(3,1198) = 4,93031846255074E-001
f(3,12) = 0,493 | f(4,14) =-52,931 |f(3,1292) = 2,82204720371674E-001
f(3,13) = 0,282 | f(4,14) =-52,931 |f(3,1346) = 1,60356794200141E-001
f(3,13) = 0,160 | f(4,14) =-52,931 |f(3,1377) = 9,07411042540247E-002
f(3,14) = 0,091 | f(4,14) =-52,931 |f(3,1394) = 5,12266817552414E-002
f(3,14) = 0,051 | f(4,14) =-52,931 |f(3,1403) = 2,88808024246137E-002
f(3,14) = 0,029 | f(4,14) =-52,931 |f(3,1409) = 1,62702992213408E-002
f(3,14) = 0,016 | f(4,14) =-52,931 |f(3,1412) = 9,16215580365822E-003
f(3,14) = 0,009 | f(4,14) =-52,931 |f(3,1414) = 5,15817511144527E-003
f(3,14) = 0,005 | f(4,14) =-52,931 |f(3,1415) = 2,90359531954751E-003
f(3,14) = 0,003 | f(4,14) =-52,931 |f(3,1415) = 1,63434303164253E-003
f(3,14) = 0,002 | f(4,14) =-52,931 |f(3,1416) = 9,19881398648164E-004
f(3,14) = 0,001 | f(4,14) =-52,931 |f(3,1416) = 5,17737999613273E-004
f(3,14) = 0,001 | f(4,14) =-52,931 |f(3,1416) = 2,91395192888954E-004
f(3,14) = 0,000 | f(4,14) =-52,931 |f(3,1416) = 1,64002861180245E-004
f(3,14) = 0,000 | f(4,14) =-52,931 |f(3,1416) = 9,23035935701717E-005
f(3,14) = 0,000 | f(4,14) =-52,931 |f(3,1416) = 5,19499039168691E-005
f(3,14) = 0,000 | f(4,14) =-52,931 |f(3,1416) = 2,92381775942261E-005
f(3,14) = 0,000 | f(4,14) =-52,931 |f(3,1416) = 1,64556681050840E-005
f(3,14) = 0,000 | f(4,14) =-52,931 |f(3,1416) = 9,26148341247565E-006
f(3,14) = 0,000 | f(4,14) =-52,931 |f(3,1416) = 5,21249296329956E-006
f(3,14) = 0,000 | f(4,14) =-52,931 |f(3,1416) = 2,93366386434583E-006
f(3,14) = 0,000 | f(4,14) =-52,931 |f(3,1416) = 1,65110688736126E-006
f(3,14) = 0,000 | f(4,14) =-52,931 |f(3,1416) = 9,29265917176359E-007
2,00 4,14 3,14159261343257E+000
f(4,14) = -52,931 | f(2,00) =6,719 |f(2,2412) = 7,36921735523644E+000
f(4,14) = -52,931 | f(2,24) =7,369 |f(2,4735) = 7,34962934533402E+000
f(4,14) = -52,931 | f(2,47) =7,350 |f(2,6769) = 6,51636966422036E+000
f(4,14) = -52,931 | f(2,6

=6,516 |f(2,8374) = 5,11314642176185E+000
f(4,14) = -52,931 | f(2,84) =5,113 |f(2,9523) = 3,60339686804760E+000
f(4,14) = -52,931 | f(2,95) =3,603 |f(3,0281) = 2,33950391238947E+000
f(4,14) = -52,931 | f(3,03) =2,340 |f(3,0752) = 1,43598425173683E+000
f(4,14) = -52,931 | f(3,0

=1,436 |f(3,1034) = 8,50536385191576E-001
f(4,14) = -52,931 | f(3,10) =0,851 |f(3,1198) = 4,93031846255074E-001
f(4,14) = -52,931 | f(3,12) =0,493 |f(3,1292) = 2,82204720371674E-001
f(4,14) = -52,931 | f(3,13) =0,282 |f(3,1346) = 1,60356794200141E-001
f(4,14) = -52,931 | f(3,13) =0,160 |f(3,1377) = 9,07411042540247E-002
f(4,14) = -52,931 | f(3,14) =0,091 |f(3,1394) = 5,12266817552414E-002
f(4,14) = -52,931 | f(3,14) =0,051 |f(3,1403) = 2,88808024246137E-002
f(4,14) = -52,931 | f(3,14) =0,029 |f(3,1409) = 1,62702992213408E-002
f(4,14) = -52,931 | f(3,14) =0,016 |f(3,1412) = 9,16215580365822E-003
f(4,14) = -52,931 | f(3,14) =0,009 |f(3,1414) = 5,15817511144527E-003
f(4,14) = -52,931 | f(3,14) =0,005 |f(3,1415) = 2,90359531954751E-003
f(4,14) = -52,931 | f(3,14) =0,003 |f(3,1415) = 1,63434303164253E-003
f(4,14) = -52,931 | f(3,14) =0,002 |f(3,1416) = 9,19881398648164E-004
f(4,14) = -52,931 | f(3,14) =0,001 |f(3,1416) = 5,17737999613273E-004
f(4,14) = -52,931 | f(3,14) =0,001 |f(3,1416) = 2,91395192888954E-004
f(4,14) = -52,931 | f(3,14) =0,000 |f(3,1416) = 1,64002861180245E-004
f(4,14) = -52,931 | f(3,14) =0,000 |f(3,1416) = 9,23035935701717E-005
f(4,14) = -52,931 | f(3,14) =0,000 |f(3,1416) = 5,19499039168691E-005
f(4,14) = -52,931 | f(3,14) =0,000 |f(3,1416) = 2,92381775942261E-005
f(4,14) = -52,931 | f(3,14) =0,000 |f(3,1416) = 1,64556681050840E-005
f(4,14) = -52,931 | f(3,14) =0,000 |f(3,1416) = 9,26148341247565E-006
f(4,14) = -52,931 | f(3,14) =0,000 |f(3,1416) = 5,21249296329956E-006
f(4,14) = -52,931 | f(3,14) =0,000 |f(3,1416) = 2,93366386434583E-006
f(4,14) = -52,931 | f(3,14) =0,000 |f(3,1416) = 1,65110688736126E-006
f(4,14) = -52,931 | f(3,14) =0,000 |f(3,1416) = 9,29265917176359E-007
4,14 2,00 3,14159261343257E+000
[/code]
Dein Verfahren eignet sich nur zur Bestimmung von den Startwerten von x1 und x2.
Es sagt nichts über f(x0) aus, nur zwischen x1,x2 eine Nullstelle ist.
Bei Funktionen wie sin(1/x) in der Nähe von 0 wird es aber immer schwierig...
Gruß Horst