0

How can I analytically differentiate in Python?

E.g:

d/dx (x^3 * L * lambda /(pi*d))

Additional: Screenshot of an attempt to install sympy

Ewran
  • 328
  • 4
  • 15
Karl
  • 59
  • 7
  • You could [use sympy](https://docs.sympy.org/latest/tutorial/calculus.html#derivatives) for symbolic differentiation – Cory Kramer Apr 13 '21 at 19:10
  • Thank you for your fast response. I tryed the first example but the error: ModuleNotFoundError: No module named 'sympy' appears... Why is it so difficult to just simply calculate the derivative of a function... – Karl Apr 13 '21 at 19:26
  • I tryed: pip install sympy - but this doesnt work either. – Karl Apr 13 '21 at 19:28
  • Do you want the symbolic derivative or the numerical derivative (in which case scipy or numpy can be used to approximate) – Cory Kramer Apr 13 '21 at 19:29
  • simply the symbolic derivative – Karl Apr 13 '21 at 19:30
  • I only have RUN, TODO, Problems, Terminal, Pyhton Console. There is no command window – Karl Apr 13 '21 at 19:31
  • I write 'pip install sympy' in 'Python Console', the error 'Syntax Error: invalid syntax' occures – Karl Apr 13 '21 at 19:32
  • Ok. I unistalled all related python application. Than i reinstalled python 3.9 from microsoft store and pycharm - community edition. I opened IDLE Shell and wrote 'pip install sympy' - but i didnt worked. Message:Syntax Error: 'invalid syntax' appeard. I opened python3.9.exe (I assume command window) and i tryed the same, but the same result. – Karl Apr 13 '21 at 19:56
  • Please note that your question is not about Pycharm. Pycharm is just a program helping you to write your code. Your question is about the programming language itself - Python – Tomerikoo Apr 13 '21 at 19:56
  • 1
    Please Google "How to install libraries in Python". As Cory told you a few times, this is not a Python command, it is a shell command that you're supposed to run in a terminal, not a Python console – Tomerikoo Apr 13 '21 at 19:57
  • I added a picture. The right blackish window is the command line... – Karl Apr 13 '21 at 20:00
  • [Install, uninstall, and upgrade packages](https://www.jetbrains.com/help/pycharm/installing-uninstalling-and-upgrading-packages.html) – Tomerikoo Apr 13 '21 at 20:02
  • Thank you for you patience. I got it, but starting with python is very confusing. – Karl Apr 13 '21 at 20:08
  • A good place to start is [The Python Tutorial](https://docs.python.org/3/tutorial/index.html). Stack Overflow is not really a place to learn, is a place to get help. Take your time and learn gradually starting from the basics and moving up. Working with advanced libraries when you still don't grasp the basics can surely be confusing... – Tomerikoo Apr 13 '21 at 20:21

1 Answers1

1

You can use sympy to differentiate a function symbolically

>>> from sympy import *
>>> x, L, lamb, d = symbols('x L lamb d')
>>> f = x**3 * L * lamb / (pi * d)
>>> f
L*lamb*x**3/(pi*d)
>>> diff(f, x)
3*L*lamb*x**2/(pi*d)
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218