Help trying to write a function - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Help trying to write a function (/thread-17472.html) |
Help trying to write a function - Rochense - Apr-12-2019 Hello, I have some trouble trying to define a function using the math module to write a logarithm and then derivating it in python 2.7. I want to write the function xo=log(yo) and then being able to derivate it to obatin as output 1/yo. Here's the code: from math import * from sympy import * xo,yo,zo=symbols('xo yo zo', real=True) xo=log(yo) dyo1=diff(xo, yo) print ("dxo/dyo="),dyo1When I execute the programme I get this error in line 5:
RE: Help trying to write a function - Gribouillis - Apr-12-2019 It works for me >>> from math import * >>> from sympy import * >>> ... xo,yo,zo=symbols('xo yo zo', real=True) >>> xo=log(yo) >>> ... dyo1=diff(xo, yo) >>> print ("dxo/dyo="),dyo1 dxo/dyo= 1/yo RE: Help trying to write a function - Rochense - Apr-12-2019 (Apr-12-2019, 11:32 AM)Rochense Wrote: Hello, I have some trouble trying to define a function using the math module to write a logarithm and then derivating it in python 2.7. I found that the error appeared if I import the pylab module. If I have this, the error appears: from math import * from sympy import * from pylab import * xo,yo,zo=symbols('xo yo zo', real=True) xo=log(yo) dyo1=diff(xo, yo) print ("dxo/dyo="),dyo1And I have just discovered that with numpy module happens the same, any idea of how to fix it? RE: Help trying to write a function - Gribouillis - Apr-12-2019 Don't use the import * construct, it leads to inconsistencies in the current namespace. Use this for exampleimport math import sympy as sy import pylab xo,yo,zo=sy.symbols('xo yo zo', real=True) xo=sy.log(yo) dyo1=sy.diff(xo, yo) print ("dxo/dyo="),dyo1Then you're sure that the log() function from module sympy is used and not the log() function from the numpy module imported in pylab. |