GUI user input to function

Nico Vogeli nicco.9537 at gmail.com
Thu Dec 28 04:38:30 EST 2017


Hello again

I think my question got lost in all the others raised, so here I am again :P



So, I tried my best, was looking into the eval and exec function, but still no succsess..

Maybe it helps when I provide the code I want to implement:

    def newton_verfahren(self):
        if self.cB_1.currentText() == 'Newton':
           
            x0 = self.newton_x.text()
            f1 = self.func_1.text()
            f2 = self.func_2.text()
            tol = self.newton_tol.text()
            n = self.newton_n.text()
            k = Numerik_Funktionen.newton(int(x0), eval(f1), eval(f2),
                                          int(tol), int(n))
            k1 = str(k[0])
            k2 = str(k[1])
            k3 = str(k[2])
            self.ausgabe.setText('Startwert -> x0: '+ k1 + "\n" +
                                  'Anzahl Iterationen: ' + k2 +"\n" +
                                  'f(x~): ' + k3)  

Notice that cB_1 is a combobox, newton_x, func_1, func_2, newton_tol and newton_n are all QLineEdits.

The code above doesn't work, so I've opend a 'test' file to just play around with the functions.

Withs test, this return a correct value for the two x functions:

from sympy import symbols

x = symbols('x')
f1 = eval(input('function 1 '))
f2 = eval(input('function 2 '))

But now I get and error from the imported module in the line where the abs(f(x)) > tol.

It states that the 'Pow' object is not callable. And now I don't know how to proceed, I can't figure out a way to make the call to the imported module:

x = symbols('x')
def newton(x0, f, fx, tol, n = 20000):
    '''
    Näherung zur lösung einer Gleichung mit dem Newton-Verfahren
    x0 = Startwert
    f = zu lösende Funktion
    fx = Ableitung der Funktion
   
    '''

   
    x = x0
    k = 0
   
    while n > k and abs(f(x)) > tol:
        k += 1    
        x = x - f(x)/fx(x)
        print('x', k, '\n', x)
   
    print('Startwert -> x0', '\n', x0)
    print('Anzahl Iterationen', '\n', k)
    print('f(x~)', '\n', f(x))
    print('Näherungswert -> x~', '\n', x)

 
    return x0, k, f(x), x

Regards

Nicco



More information about the Python-list mailing list