Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TypeError for derivative function

I used sympy to find derivative of a function.

𝑓(π‘₯)=π‘₯βˆ’5+√(4βˆ’π‘₯^2)

import sympy
def f_derivative(x):
  x = sympy.Symbol('x')
  f = x - 5 + (4 - x**2)**0.5
  derivative_f = f.diff(x)
  derivative_f = sympy.lambdify(x, derivative_f)
  print(derivative_f(1))
f_derivative(1)
assert f_derivative(1) - 0.42264973 < 1e-5

However, there is an error when I use assert to check

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

TypeError: unsupported operand type(s) for -: ‘NoneType’ and ‘float’

Please let me know how to fix it

>Solution :

import sympy
def f_derivative(x):
  x = sympy.Symbol('x')
  f = x - 5 + (4 - x**2)**0.5
  derivative_f = f.diff(x)
  derivative_f = sympy.lambdify(x, derivative_f)
  result = derivative_f(1)
  assert result - 0.42264973 < 1e-5
  print(result)
f_derivative(1)

I hope helped you

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading