Autor Beitrag
goog
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 37



BeitragVerfasst: Mi 16.02.05 19:46 
Hi Leute!

Ich bastle grade an einem Prog für Kurvendiskussion ganzrationaler Funktionen.

Ich hab mal den Source von ChristianS. zum Thema Nullstellenberechnung aufgenommen und arbeite zur Eingabe mit ner Inputbox.
Leider will dat aber noch nicht funzen :cry: , was klappt da nicht?
ausblenden volle Höhe 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:
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:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
var
  Form1: TForm1;
  n:           integer;
  a:           array[0..5] of real;
  max,min :    real;
  startpunkt : array[0..4] OF real;
  nullstelle : array[0..4] OF real;


implementation

{$R *.DFM}


function potenz (basis : Real; exponent : Integer) : Real;  //damit wir uns Math sparen können
VAR ergebnis : Real;
begin
  if exponent = 1 then result:=basis  
  else begin  
    if exponent = 0 then result:=1  
    else begin
      if exponent mod 2 = 0 then  
      begin  
        ergebnis:=potenz(basis, exponent div 2);  
        result:=ergebnis*ergebnis;  
      end else result:=potenz(basis, exponent-1)*basis;  
    end;  
  end;  
end;

function f(x : Real) : Real;  //Die FUnktion  
VAR i : INTEGER;  
begin  
  result:=0;  
  for i:=0 TO 5 DO  
    result:=result+a[i]*potenz(x,i);  
end;  


function diff_f(x : Real) : Real;   //1. Ableitung der Funktion  
VAR i : INTEGER;  
begin  
  result:=0;  
  for i:=1 TO 5 DO  
    result:=result+i*a[i]*potenz(x,i-1);
end;

function nullstelle_berechnen(genauigkeit, start : Real) :Real;  //Newtonverfahren
VAR x_neu,x_alt : Real;
    stop, fehler : Boolean;
begin
  x_neu:=start;
  stop:=false;
  fehler:=false;
  while not stop do
  begin
    x_alt:=x_neu;
    x_neu:=x_alt-f(x_alt)/diff_f(x_alt);
    if abs(x_neu-x_alt)<=genauigkeit then stop:=true;
    if (Abs(max)-Abs(x_neu)<genauigkeit) or (Abs(min)-Abs(x_neu)<genauigkeit) then  //noch im Bereich?
    begin
      stop:=true;
      fehler:=true;
    end;
  end;
  if fehler then result:=max+1//Alle nicht verwendeten Felder, mit einem Wert ausserhalb
                              //des zulässigen Bereichs füllen, damit sie markiert sind.


  else result:=x_neu;
end;

procedure nullstellen_finden (genauigkeit : Real);
VAR i: Integer;
begin
  for i:=0 to 4 do
    if startpunkt[i]<=max then nullstelle[i]:=nullstelle_berechnen(genauigkeit, startpunkt[i])
    else nullstelle[i]:=max+1;
end;

procedure startpunkte_finden (schrittweite : Real);
var i : INTEGER;
    gefunden : Integer;
    x_left, alt_x_left : Real;
    x_right, alt_x_right : Real;
begin
  gefunden:=0;
  alt_x_left:=(max+min)/2;
  alt_x_right:=(max+min)/2;
  schrittweite:=abs(schrittweite);
  x_left:=alt_x_left-schrittweite;
  x_right:=alt_x_right+schrittweite;


  while (gefunden <= 4) and (Abs(max)-Abs(x_right)>(-schrittweite)) and (Abs(min)-Abs(x_left)>(-schrittweite)) do
  begin
    if Abs(max)-Abs(x_right)>(-schrittweite) then //noch im Bereich?
    begin
      if f(x_right) = 0 then   //direkt schon die Nullstelle gefunden
      begin
        startpunkt[gefunden]:=x_right;
        inc(gefunden);
      end;
      if (f(x_right)>0) and (f(alt_x_right)<0) then  //Vorzeichenwechsel
      begin
        startpunkt[gefunden]:=(x_right+alt_x_right)/2;
        inc(gefunden);
      end;


      if (f(x_right)<0) and (f(alt_x_right)>0) then  //Vorzeichenwechsel
      begin
        startpunkt[gefunden]:=(x_right+alt_x_right)/2;
        inc(gefunden);
      end;
      alt_x_right:=x_right;
      x_right:=x_right+schrittweite;
    end;




    if Abs(min)-Abs(x_left)>(-schrittweite) then  //noch im Bereich?
    begin
      if f(x_left) = 0 then        //direkt schon die Nullstelle gefunden
      begin
        startpunkt[gefunden]:=x_left;
        inc(gefunden);


      end else
      if (f(x_left)>0) and (f(alt_x_left)<0) then  //Vorzeichenwechsel
      begin
        startpunkt[gefunden]:=(x_left+alt_x_left)/2;
        inc(gefunden);
      end;


      if (f(x_left)<0) and (f(alt_x_left)>0) then  //Vorzeichenwechsel
      begin
        startpunkt[gefunden]:=(x_left+alt_x_left)/2;
        inc(gefunden);
      end;
      alt_x_left:=x_left;
      x_left:=x_left-schrittweite;
    end;
    Application.ProcessMessages;
  end;
  for i:=gefunden TO 4 DO startpunkt[i]:=max+1;  //Alle nicht verwendeten Felder, mit einem Wert ausserhalb
                                                 //des zulässigen Bereichs füllen, damit sie markiert sind.
end;



procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
begin
  max:=StrToFloat(edit1.text);  //Der Bereich wird festgelegt, damit das Programm nicht ins Leere laufen kann
  min:=StrToFloat(edit2.text);


  if max<min then   //Maximum soll größer Minimum sein
  begin
    max:=min;
    min:=StrToFloat(edit1.text);
  end;


  memo1.Lines.Clear;


  startpunkte_finden(0.001);  //Newtonverfahren funktioniert nur in der Umgebung der Nullstellen


  memo1.lines.add('STARTPUNKTE:');
  for i:=0 TO 4 DO
  if startpunkt[i]<=max then memo1.lines.add(FloatToStr(startpunkt[i]));


  nullstellen_finden(0.000001);

  memo1.lines.add('NULLSTELLEN:');
  for i:=0 TO 4 DO
  if nullstelle[i]<=max then memo1.Lines.Add(FloatToStr(nullstelle[i]));

end;




procedure TForm1.FormCreate(Sender: TObject);
var i:integer;
begin
n:=StrtoInt(Inputbox('Grad der Funktion','Grad','1'));
 for i:=0 to n do
  begin
   a[i]:=StrtoInt(Inputbox('Eingabe der Koeffizienten','Koeffizient von a'+InttoStr(i),''));
  end;
end;

end.


gruß goog
MitschL
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 211

Win 98 SE, Win 2000
D5 Pers, D6 Pers und D7 Pro
BeitragVerfasst: Do 17.02.05 10:06 
Ähm,

was will da nicht funzen? Will er nicht kompilieren? Macht er einen Rechenfehler?
So kann ich nur raten und deshalb rate ich, daß du einen Breakpoint setzt und durchstepst und zwar mit einem auf dem Papier gerechneten Beispiel.

gegrüßt!

_________________
"Bloßes Ignorieren ist noch keine Toleranz." (Theodor Fontane)
goog Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 37



BeitragVerfasst: Do 17.02.05 11:51 
Hi MitschL!

Also compilen tut er aber er zeigt mir nichts im Memo an.
Ich werd erst mal deinen Rat befolgen und das ganze auf Papier exemplarisch durchrechnen :les: !

gruß goog!
MitschL
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 211

Win 98 SE, Win 2000
D5 Pers, D6 Pers und D7 Pro
BeitragVerfasst: Do 17.02.05 17:10 
ohne mich zu sehr in die Materie einzuhäkeln, irritiert mich folgendes:
ausblenden Delphi-Quelltext
1:
while (gefunden <= 4) and (Abs(max)-Abs(x_right)>(-schrittweite)) and (Abs(min)-Abs(x_left)>(-schrittweite)) do					


gegrüßt!

_________________
"Bloßes Ignorieren ist noch keine Toleranz." (Theodor Fontane)