Seite 1 von 1

Polynom rückwärts lösen (y ist gegeben)

Verfasst: Mittwoch 7. Mai 2014, 10:06
von boletus999
Hallo liebe Gemeinde,

ich habe folgendes Polynom:

y = 53.643*x**3 - 69.877*x**2 + 72.1*x - 5.8153

mit Hilfe der Cardanischen Formel habe ich die Nullstellen berechnet, oder hier in Python mit Sympy

Code: Alles auswählen

#!/usr/bin/env python

from __future__ import division
import sympy as sy
x, y, z, t = sy.symbols('x y z t')
k, m, n = sy.symbols('k m n', integer=True)
f, g, h = sy.symbols('f g h', cls=sy.Function)

print sy.solve(53.643*x**3 - 69.877*x**2 + 72.1*x - 5.8153, x)
Ausgabe ist das:
[0.0875918052106270,
0.607519273652539 - 0.931967757064949*I,
0.607519273652539 + 0.931967757064949*I]

Und nun stehe ich völlig auf dem Schlauch. :oops:
Ich habe eine Liste mit mir bekannten y
z.B y = [129.456, 45.894, 187.041, ... ] etc

wie löse ich in python für jedes gegebene y das Polynom, um x zu erhalten?? :K

Vielen lieben Dank

Re: Polynom rückwärts lösen (y ist gegeben)

Verfasst: Mittwoch 7. Mai 2014, 12:03
von boletus999
so ich beantworte es selbst :lol: :lol:

bzw. habe ich meinen Fuß vom Schlauch genommen
Lösung:

Code: Alles auswählen

#!/usr/bin/env python

from __future__ import division
import sympy as sy
x, y, z, t = sy.symbols('x y z t')
k, m, n = sy.symbols('k m n', integer=True)
f, g, h = sy.symbols('f g h', cls=sy.Function)

y = [99.3, 45.894, 30.041]

for i in range(len(y)):
   print sy.solve(53.643*x**3 - 69.877*x**2 + 72.1*x - 5.8153 -float(y[i]), x)[0]
Weil nach bei sy.solve das Polynom auf "0" gesetzt wird, also -y
:wink:

Re: Polynom rückwärts lösen (y ist gegeben)

Verfasst: Mittwoch 7. Mai 2014, 12:19
von BlackJack
@boletus999: Die Schleife ist so wie sie da steht in Python ein „anti pattern”. Das `i` wird doch gar nicht wirklich gebraucht und über die Elemente von `y` kann man *direkt* iterieren mit ``for y in ys:``. Da habe ich jetzt mal `y` in `ys` umbenannt, weil das etwas ungünstig ist für viele Werte einen Namen zu verwenden der eher zu einem Einzelwert passt.

Re: Polynom rückwärts lösen (y ist gegeben)

Verfasst: Mittwoch 7. Mai 2014, 13:54
von boletus999
Jawoll Meiser!
ich habe mir leider diesen „anti pattern” Stil angewöhnt, da ich zu viel und zu oft über variable Listen-Ranges iteriere.
Blöde Angewohnheit.

So sieht es wahrlich besser aus:

Code: Alles auswählen

#!/usr/bin/env python

from __future__ import division
import sympy as sy
x, y, z, t = sy.symbols('x y z t')
k, m, n = sy.symbols('k m n', integer=True)
f, g, h = sy.symbols('f g h', cls=sy.Function)

ys = [99.3, 45.894, 30.041]

for y in ys:
   print sy.solve(53.643*x**3 - 69.877*x**2 + 72.1*x - 5.8153 -y, x)[0]