Autor Beitrag
01Detlef
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 85



BeitragVerfasst: Do 19.06.03 17:36 
hi,

ich möchte ein Näherungsverfahren zur Nullstellenbestimmung programmieren!
f(x)=1/16*x³+x-2

nun sollen zwei werte eingelesen werden und wenn sie nicht gleich null sind sollen diese x-Werte gemittelt werden (x1+x2)/2=x und dann soll wieder geguckt werden wie der Kunktionswert ist. Danach soll dann (x1+x)/2 oder (x2+x)/2 je nach dem ob x näher an x1 oder an x2 liegt! Dieser Vorgang soll dann z.B 30 durchgeführt werden, wenn vorher nicht glatt null herauskommt!

Wie mache ich das mit einer Schleife, da komme ich nicht so klar mit, alles einzelnd habe ich schon hinbekommen mit zig if-sätzen!
ausblenden Quelltext
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:
{erstmal würde ich eine Funktion programmieren, damit ich den Funktionwert berechnen kann}
function f(x:real): real;
BEGIN
  result:=1/16*x*x*x+x-2;
END;

//dann kommt die eingabe, rechnung

procedure TForm1.Button1Click(Sender: TObject); 
var i:integer; 
x,x1,x2,d : real; 
begin 
 x1:= strtoint (edit1.text); 
 x2:= strtoint (edit2.text);

 WHILE f(x) <> 0 DO
 BEGIN
 x:= (x1+x2)/2


Ich weiss nicht wie es weiter gehen soll, so dass es höchstens auch nur 30 mal durchläuft?

if f(x) > 0 THEN DO
BEGIN
 x1:= x
ELSE
 x2:= x;
END;
// aber dass halbieren und umschreiben muss ja dann 30 mal klappen und nicht nur mit x1 und x2!
mars
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 238

Debian Woody, Win 2000, Win XP
D7 Ent, Kylix 3
BeitragVerfasst: Do 19.06.03 17:49 
So vorneweg: Beim Intervallhalbierungsverfahren wird nicht geprüft, woram f(x) näher ist, sondern auf welcher Seite der x-Achse. Aber sonst stimmt der Code:
ausblenden Delphi-Quelltext
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:
function f(x:real): real; 
BEGIN 
  result:=1/16*x*x*x+x-2; 
END; 

procedure TForm1.Button1Click(Sender: TObject); 
var i:integer; 
x,x1,x2,d : real; 
begin 
x1:= strtoint (edit1.text); 
x2:= strtoint (edit2.text); 

i:=1;    <<-------------- i wird initiert und dann hochgezählt bis es 30 ist

WHILE (f(x) <> 0) and (i<=30) DO <<----
BEGIN 
 x:= (x1+x2)/2 
if f(x) > 0 THEN DO 
BEGIN 
 x1:= x 
ELSE 
 x2:= x; 
END; 
inc(i);  <<--------------
end;
Ausgabe von x bzw. f(x)
end;


Ungefähr so...
01Detlef Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 85



BeitragVerfasst: Do 19.06.03 18:01 
ok, das programm läuft jetzt halb!

wenn ich jetzt für x1=1 eingebe und x2=2 dann kommt für x eins raus und f(x) ist -0,9375

aber wenn ich für x1=2 und x2=1 eingebe kommt f(x)=0 und x=1,69!!

woran liegt das ??

Detlef
mars
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 238

Debian Woody, Win 2000, Win XP
D7 Ent, Kylix 3
BeitragVerfasst: Do 19.06.03 18:37 
Du hast es so definiert. Analytisch wäre die Abfrage folgendermassen:
ausblenden Quelltext
1:
2:
3:
4:
if sign(f(x1))*sign(f(x))<0 then
 x2:=x
else
 x1:=x;


Da du aber von Anfang an festlegst, welche Intervallgrenze genommen werden soll, musst du f(x1) grösser wählen als f(x2).
01Detlef Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 85



BeitragVerfasst: Do 19.06.03 20:58 
axo, vielen dank!

was heißt denn dieses sign(f(x1))*sign(f(x))<0??

Detlef
mars
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 238

Debian Woody, Win 2000, Win XP
D7 Ent, Kylix 3
BeitragVerfasst: Fr 20.06.03 01:07 
Sign heisst Vorzeichen. Deshalb wäre sign(x)*sign(y)<0, wenn x und y verschiedene Vorzeichen haben. Das ist ja genau, was getestet werden soll.

Gern geschehen :D
01Detlef Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 85



BeitragVerfasst: Fr 20.06.03 14:08 
alles klar!

vielen dank!
Detlef