![]() |
Bisektionsverfahren
Hallo!
ich soll für die schule n programm schreiben, das mit dem bisektionsverfahren nullstellen errechnet. ich hab bisher alles so eingerichtet, dass das programm mit gleichungen bis achten grades rechnen kann. wie sollte es jedoch anders sein, bekomme ich die prozedure für den "ermitteln"-button nich hin. bisher sieht das so aus. ich seh selber, was falsch is, aba ich weiß nich, wie ichs anders machn soll, da ich grad erst mit delphi angefangen hab...(also bidde habt verständnis)
Code:
kann mir jmd helfen?
procedure TForm1.SpeedButton1Click(Sender: TObject);
var Hoch1, Hoch2, Hoch3, Hoch4, Hoch5, Hoch6, Hoch7, Hoch8, n, e, a, b, fa, fb, a2, a3, a4, a5, a6, a7, a8, b2, b3, b4, b5, b6, b7, b8, c, fc, c2, c3, c4, c5, c6, c7, c8, d, fd, d2, d3, d4, d5, d6, d7, d8, Betragfd : real; begin Hoch1:=strtofloat(edit8.text); Hoch2:=strtofloat(edit7.text); Hoch3:=strtofloat(edit6.text); Hoch4:=strtofloat(edit5.text); Hoch5:=strtofloat(edit4.text); Hoch6:=strtofloat(edit3.text); Hoch7:=strtofloat(edit2.text); Hoch8:=strtofloat(edit1.text); n:=strtofloat(edit9.text); e:=strtofloat(edit10.text); a:=strtofloat(edit11.text); b:=strtofloat(edit12.text); a2:=a * a; a3:=Power(a, 3); a4:=Power(a, 4); a5:=Power(a, 5); a6:=Power(a, 6); a7:=Power(a, 7); a8:=Power(a, 8); b2:=b * b; b3:=Power(b, 3); b4:=Power(b, 4); b5:=Power(b, 5); b6:=Power(b, 6); b7:=Power(b, 7); b8:=Power(b, 8); fa:=(Hoch1*a)+(Hoch2*a2)+(Hoch3*a3)+(Hoch4*a4)+(Hoch5*a5)+(Hoch6*a6)+(Hoch7*a7)+(Hoch8*a8)+n; fb:=(Hoch1*b)+(Hoch2*b2)+(Hoch3*b3)+(Hoch4*b4)+(Hoch5*b5)+(Hoch6*b6)+(Hoch7*b7)+(Hoch8*b8)+n; if fa = 0 then edit13.Text:=floattostr(a); if fb = 0 then edit13.Text:=floattostr(b); if (fa <> 0) and (fb <>0) then begin if fa > 0 then begin repeat c:=(a+b)/2; c2:=c * c; c3:=Power(c, 3); c4:=Power(c, 4); c5:=Power(c, 5); c6:=Power(c, 6); c7:=Power(c, 7); c8:=Power(c, 8); fc:=(Hoch1*c)+(Hoch2*c2)+(Hoch3*c3)+(Hoch4*c4)+(Hoch5*c5)+(Hoch6*c6)+(Hoch7*c7)+(Hoch8*c8)+n; if fc > 0 then begin d:=(a+c)/2; d2:=d * d; d3:=Power(d, 3); d4:=Power(d, 4); d5:=Power(d, 5); d6:=Power(d, 6); d7:=Power(d, 7); d8:=Power(d, 8); fd:=(Hoch1*d)+(Hoch2*d2)+(Hoch3*d3)+(Hoch4*d4)+(Hoch5*d5)+(Hoch6*d6)+(Hoch7*d7)+(Hoch8*d8)+n; if fd > 0 then Betragfd:=fd else Betragfd:=-fd; end; until Betragfd < e; edit13.Text:=floattostr(d) end; end; if fb > 0 then begin repeat c:=(a+b)/2; c2:=c * c; c3:=Power(c, 3); c4:=Power(c, 4); c5:=Power(c, 5); c6:=Power(c, 6); c7:=Power(c, 7); c8:=Power(c, 8); fc:=(Hoch1*c)+(Hoch2*c2)+(Hoch3*c3)+(Hoch4*c4)+(Hoch5*c5)+(Hoch6*c6)+(Hoch7*c7)+(Hoch8*c8)+n; if fc > 0 then begin d:=(b+c)/2; d2:=d * d; d3:=Power(d, 3); d4:=Power(d, 4); d5:=Power(d, 5); d6:=Power(d, 6); d7:=Power(d, 7); d8:=Power(d, 8); fd:=(Hoch1*d)+(Hoch2*d2)+(Hoch3*d3)+(Hoch4*d4)+(Hoch5*d5)+(Hoch6*d6)+(Hoch7*d7)+(Hoch8*d8)+n; if fd > 0 then Betragfd:=fd else Betragfd:=-fd; end; until Betragfd < e; edit13.Text:=floattostr(d) end; end; (Hoch1 bis Hoch8 stehn für die zahlen, welche bei der funktionsgleichung vor x bis x^8 stehn und e steht für epsylon...) |
Re: Bisektionsverfahren
Hallo KeyBe,
hier ist mein Lösungs-Ansatz:
Delphi-Quellcode:
Damit es klappt, muss für SameValue die Unit Math eingebunden werden.
// Diese Delphi-Funktion stellt deine mathematische Funktion dar.
// Ich habe hier das Beispiel von 2x² - 14x + 4 genommen. // Diesen Teil musst du dann selber anpassen. function f(x: Double): Double; begin Result := 2 * sqr(x) - 14 * x + 4; end; // Diese Funktion ist das Bisektions-Verfahren. function Bisection(a, b: Double; aEpsilon: Double): Double; var fa, fb: Double; lm: Double; fm: Double; begin // Funktionswerte für a und b berechnen fa := f(a); fb := f(b); // Nachschauen ob fa als Null angesehen werden kann. if (SameValue(fa, 0, aEpsilon)) then begin Result := a; {x} Exit; end; // Nachschauen ob fb als Null angesehen werden kann. if (SameValue(fb, 0, aEpsilon)) then begin Result := b; {x} Exit; end; // Wenn nicht genau eine Nullstelle zwischen a und b dann Fehler erzeugen. // Führt zu Problemen, falls eine gerade Anzahl Nullstellen vorliegt. if ((fa * fb) > 0) then begin raise Exception.Create('keine Nullstelle'); end; // Fuktionswert der Mitte des Intervalls von [a..b] berechnen. lm := (a + b) / 2; fm := f(lm); // Nachschauen, ob die Nullstelle im linken oder rechten Teilintervall liegt. // Dann in dem passenden Teilintervall weitersuchen. if ((fa * fm) < 0) then begin Result := Bisection(a, lm, aEpsilon); end else begin Result := Bisection(lm, b, aEpsilon); end; end; // Hier ist ein Beispiel-Aufruf. procedure TForm1.Button1Click(Sender: TObject); var lX0: Double; lMsg: string; begin try // Nullstelle berechnen lX0 := Bisection(0, 5, 0.000001); // Meldung anzeigen. lMsg :=Format('An der Stelle %4.6f hat die Funktion den Wert %4.6f', [lX0, F(lX0)]); MessageDlg(lMsg, mtInformation, [mbOK], 0); except MessageDlg('Im angegebenen Intervall gibt es keine oder mehr als eine ' + 'Nullstelle', mtWarning, [mbOK], 0); end; // except end; Viele Grüße, pszopp |
Re: Bisektionsverfahren
Vielen dank! werds gleich mal ausprobiern
mfg keybe |
Alle Zeitangaben in WEZ +1. Es ist jetzt 22:13 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz