-4

I'm currently trying to write a program for error propagation. For this reason I need the partial derivative.

Function = input("Function: ")   

#Function should look something like this: u**2 + 3*x/(v+w)

print ("s = "f"{Function}")


#Derivate by u
k = float(Function.derivatives[u])
print("Partial Derivative by u = "f"{k}")

When I try to run this, there's an error message: "str function has no attribute 'derivatives', but when I try to change the first line to:

Function = float(input("Function: "))

there's an error message as well, it says it can't be transformed to a floating point number.

I'm still quite a beginner, excuse me if my question is too banal.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
sempersmile
  • 481
  • 2
  • 9
  • 2
    Python doesn't have any built-in capabilities for finding the derivatives of a function. You're going to need some kind of symbolic math library to do this. Maybe [Sympy](http://www.sympy.org/en/index.html)? I'm not sure, I haven't used it myself. – Kevin Jun 06 '17 at 12:41

1 Answers1

3

From the scipy docs:

>>> from scipy.misc import derivative
>>> def f(x):
...     return x**3 + x**2
...
>>> derivative(f, 1.0, dx=1e-6)
4.9999999999217337

The first argument is the function, the second argument is the value of x0, and the 3rd is an optional param to specify spacing.

More here: https://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.misc.derivative.html

cs95
  • 379,657
  • 97
  • 704
  • 746