Am Montag, 25. Dezember 2017 16:56:19 UTC+1 schrieb Chris Angelico: > On Tue, Dec 26, 2017 at 2:04 AM, Nico Vogeli <nicco.9...@gmail.com> wrote: > > Am Montag, 25. Dezember 2017 15:58:26 UTC+1 schrieb Chris Angelico: > >> On Tue, Dec 26, 2017 at 1:48 AM, Nico Vogeli <nicco.9...@gmail.com> wrote: > >> > Am Montag, 25. Dezember 2017 14:51:21 UTC+1 schrieb Chris Angelico: > >> >> On Tue, Dec 26, 2017 at 12:36 AM, Nico Vogeli <nicco.9...@gmail.com> > >> >> wrote: > >> >> > Hi everybody. First ad foremost, happy Christmas! > >> >> > >> >> Same to you! > >> >> > >> >> > I want to let the use input a function (like x**2) and parse it after > >> >> > that through code (for my numeric class) > >> >> > > >> >> > def newton(x0, s, s2, 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 > >> >> > > >> >> > ''' > >> >> > def f(a): > >> >> > y = s > >> >> > return y > >> >> > > >> >> > def fx(a): > >> >> > y = s2 > >> >> > return y > >> >> > >> >> > >> >> > newton(2, 3*x**2, 6*x, 0.1, 2) > >> >> > > >> >> > I notice that the x is not converted to an integer, because of the x > >> >> > = symplos('x') > >> >> > But I don't know how I could possibli change the code to work... > >> >> > >> >> The easiest way is to pass a *function* to newton(). It'd look like > >> >> this: > >> >> > >> >> def newton(x0, f, fx, tol, n=20000): > >> >> ... as before, but without the nested functions > >> >> > >> >> newton(2, lambda x: 3*x**2, lambda x: 6*x, 0.1, 2) > >> >> > >> >> At least, I think that's how you're doing things. Inside the nested > >> >> functions, you use 'a', but outside, you use 'x'. Are those > >> >> representing the same concept? If so, the lambda functions given here > >> >> will have the same effect. > >> >> > >> >> Hope that helps! > >> >> > >> >> ChrisA > >> > > >> > Hi Chris > >> > > >> > Thanks very much for your quick response! > >> > I was in a bit of a rush, so I confused the variables (don't worry, I > >> > just messed arround to try different things, the original code was al > >> > tidy with the variable). > >> > > >> > I tried your input, but now I get another error: > >> > > >> > > >> > File "C:/Users/Nicco ZHAW/Desktop/Test GUI/Test Projekt.py", line 42, > >> > in newton > >> > b = x - f(x)/fx(x) > >> > > >> > TypeError: unsupported operand type(s) for /: 'function' and 'function' > >> > > >> > This did not occure befor I tried to implement the user input.. > >> > >> Did you remove the nested functions? The lambda functions completely > >> replace your "def f(a)" and "def fx(a)" functions. > >> > >> ChrisA > > > > I am so so sorry!! I just did not know what you meant by sested functions.. > > :P > > No need to apologize! That's why we have two-directional communication > - I can ask you to clarify, you can ask me to clarify, and we get to a > solution. > > The concept of passing functions around does take some getting used > to, so there's no shame in not instantly understanding it. > > > Thank you so much for helping me! You're great! > > > > Cheers and all the best! > > My pleasure! Have yourself an awesome holiday season. > > ChrisA
Hello again 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('ssd ')) f2 = eval(input('fd ')) 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 -- https://mail.python.org/mailman/listinfo/python-list