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

'Subs' is not defined error when trying to graph the tangent line of the derivative of my function

I’ve graphed my original function and I want to graph the tangent line at the point of interest.

import numpy as np
from sympy import lambdify, symbols, diff, Abs

point_of_interest = 8
graphRange = [1,15]

# define the variable and the function
xsym = symbols('x')

# With a chord length of 60, plot the circle diameters
origFunction = 2 * ((60 ** 2) / (8 * Abs(xsym)) + Abs(xsym) / 2)

# define the derivative
derivative = diff(origFunction, xsym)

# define the tangent line at point of interest
tangentLine = derivative.subs(xsym, point_of_interest) * (xsym - point_of_interest) + origFunction.subs(xsym, point_of_interest)

# Convert the SymPy function to a lambda function
origF = lambdify(xsym, origFunction, "numpy")

# Generate x values
x_values = np.linspace(graphRange[0], graphRange[1], 100)

# Generate y-values for the original function
y_values = origF(x_values)

# Plot the original function
plt.plot(x_values, y_values, label='Original Function')

# THIS SECTION DOESN'T WORK YET
# Convert the SymPy function to a lambda function
diffF = lambdify(xsym, tangentLine, "numpy")

# Generate y-values for the tangent line
y_tang_values = diffF(x_values)

# Plot the tangent line
plt.plot(x_values, y_tang_values, label='Tangent Line')

#plot the point of interest
plt.plot(point_of_interest, origF(point_of_interest), 'ro', label='Point of Interest')

# Add labels and legend
plt.xlabel('x')
plt.ylabel('y')
plt.title('Graph of Original Function and Tangent Line')
plt.legend()

# Show the plot
plt.show()

The error I get is this

Traceback (most recent call last):
File "x:\Python Projects\Chord calc.py", line 37, in
y_tang_values = diffF(x_values)
^^^^^^^^^^^^^^^
File "", line 4, in _lambdifygenerated
NameError: name ‘Subs’ is not defined

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

I don’t know what to do to fix this error.

>Solution :

At the moment, you are using a complex symbol, xsym: it doesn’t have any assumptions, hence it is as general as it gets.

Your function contains Abs(xsym): because xsym is complex, its derivative will be quite convoluted:

print(Abs(xsym).diff(xsym))
# (re(x)*Derivative(re(x), x) + im(x)*Derivative(im(x), x))*sign(x)/x

Once you substitutes a numeric value into that expression, Subs appears, and you get the error later on.

The solution is simple: create a real symbol. Replace this line:

xsym = symbols('x')

with:

xsym = symbols('x', real=True)

Then, everything will work as expected.

enter image description here

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