Gauß-Algorithmus

    • VB.NET

    Es gibt 3 Antworten in diesem Thema. Der letzte Beitrag () ist von RodFromGermany.

      Gauß-Algorithmus

      Hatte vor einiger Zeit mal den Gauß-Algorithmus nachprogrammiert und vllt kanns ja jmd brauchen:

      VB.NET-Quellcode

      1. Public Structure QuadratFunction
      2. Public Sub New(ByVal a As Double, ByVal b As Double, ByVal c As Double, ByVal y As Double)
      3. Me.x = a
      4. Me.y = b
      5. Me.z = c
      6. Me.f = y
      7. End Sub
      8. Public x As Double
      9. Public y As Double
      10. Public z As Double
      11. Public f As Double
      12. End Structure
      13. Public Class Gauß
      14. Public Shared Function Berechne(ByVal x As QuadratFunction, ByVal x2 As QuadratFunction, ByVal x3 As QuadratFunction) As QuadratFunction
      15. Dim coEffMatrix As Double()() = {New Double() {x.x, x.y, x.z, x.f},
      16. New Double() {x2.x, x2.y, x2.z, x2.f},
      17. New Double() {x3.x, x3.y, x3.z, x3.f}}
      18. Dim quotient As Double = coEffMatrix(0)(0)
      19. Dim potentax As Double = coEffMatrix(0)(0) / quotient
      20. Dim potentbx As Double = coEffMatrix(0)(1) / quotient
      21. Dim potentc As Double = coEffMatrix(0)(2) / quotient
      22. Dim potenty As Double = coEffMatrix(0)(3) / quotient
      23. Dim multiplicate As Double = -(coEffMatrix(1)(0) / potentax)
      24. Dim potentaxerg As Double = potentax * multiplicate
      25. Dim potentbxerg As Double = potentbx * multiplicate
      26. Dim potentcerg As Double = potentc * multiplicate
      27. Dim potentyerg As Double = potenty * multiplicate
      28. Dim multiplicate2 As Double = -(coEffMatrix(2)(0) / potentax)
      29. Dim potentaxerg2 As Double = potentax * multiplicate2
      30. Dim potentbxerg2 As Double = potentbx * multiplicate2
      31. Dim potentcerg2 As Double = potentc * multiplicate2
      32. Dim potentyerg2 As Double = potenty * multiplicate2
      33. Dim newMatrix As Double()() = {New Double() {potentax, potentbx, potentc, potenty},
      34. New Double() {potentaxerg + coEffMatrix(1)(0), potentbxerg + coEffMatrix(1)(1), potentcerg + coEffMatrix(1)(2), potentyerg + coEffMatrix(1)(3)},
      35. New Double() {potentaxerg2 + coEffMatrix(2)(0), potentbxerg2 + coEffMatrix(2)(1), potentcerg2 + coEffMatrix(2)(2), potentyerg2 + coEffMatrix(2)(3)}}
      36. Dim newMultiplication As Double = newMatrix(1)(1)
      37. Dim newPotentbx As Double = newMatrix(1)(1) / newMultiplication
      38. Dim newPotentc As Double = newMatrix(1)(2) / newMultiplication
      39. Dim newPotenty As Double = newMatrix(1)(3) / newMultiplication
      40. Dim expo As Double = -(newMatrix(0)(1) / newPotentbx)
      41. Dim newExpobx As Double = newMatrix(0)(1) + (newPotentbx * expo)
      42. Dim newExpoc As Double = newMatrix(0)(2) + (newPotentc * expo)
      43. Dim newExpoy As Double = newMatrix(0)(3) + (newPotenty * expo)
      44. Dim expo3 As Double = -(newMatrix(2)(1) / newPotentbx)
      45. Dim newExpobx3 As Double = newMatrix(2)(1) + (newPotentbx * expo3)
      46. Dim newExpoc3 As Double = newMatrix(2)(2) + (newPotentc * expo3)
      47. Dim newExpoy3 As Double = newMatrix(2)(3) + (newPotenty * expo3)
      48. Dim endMatrix As Double()() = {New Double() {newMatrix(0)(0), newExpobx, newExpoc, newExpoy},
      49. New Double() {newMatrix(1)(0), newPotentbx, newPotentc, newPotenty},
      50. New Double() {newMatrix(2)(0), newExpobx3, newExpoc3, newExpoy3}}
      51. Dim func As Double = endMatrix(2)(2) / endMatrix(2)(2)
      52. Dim CErgebniss As Double = endMatrix(2)(3) / endMatrix(2)(2)
      53. Dim poterstzeile As Double = -endMatrix(0)(2)
      54. Dim axErgebniss As Double = CErgebniss * poterstzeile + endMatrix(0)(3)
      55. Dim bxErgebniss As Double = -endMatrix(1)(2) * CErgebniss + endMatrix(1)(3)
      56. Return New QuadratFunction(axErgebniss, bxErgebniss, CErgebniss, 0)
      57. End Function
      Netter Kode,

      Aber wo führ währe er rein teoretisch einsetzbar in einer Computer Software?

      LG, Herbrich

      RushDen schrieb:

      vllt kanns ja jmd brauchen
      Wozu sollte jemand diesen Deinen Code brauchen können?
      Statt der Information, dass das System nicht lösbar ist, kommt mehrfach Double.NaN & Co raus:

      VB.NET-Quellcode

      1. Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
      2. Dim q1 = New QuadratFunction(0, 1, 2, 1)
      3. Dim q2 = New QuadratFunction(0, 1, 4, 2)
      4. Dim q3 = New QuadratFunction(0, 1, 9, 3)
      5. Dim qr = Gauß.Berechne(q1, q2, q3)
      6. Dim txt = String.Format("{0}{4}{1}{4}{2}{4}{3}{4}", qr.x, qr.y, qr.z, qr.f, Environment.NewLine)
      7. MessageBox.Show(txt)
      8. End Sub
      1. Poste mal ein Beispiel sinnvoller Daten, um Deinen Ansatz verfolgen zu können
      2. überprüf Deinen Algorithmus dahingehend, dass Du bei Division durch Null sofort abbrichst, verwende ein sinnvolles Epsilon (1e-10 oder so).
      3. gib ggf. ein Boolean zurück, das sagt, dass die Lösung korrekt ist.
      Bilder
      • Nan.png

        2,9 kB, 144×189, 1.133 mal angesehen
      Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
      Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
      Ein guter .NET-Snippetkonverter (der ist verfügbar).
      Programmierfragen über PN / Konversation werden ignoriert!