我已经画出了我的原始函数,我想画出关注点的切线.
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()
我得到的错误是
回溯(最近一次呼叫): 文件"x:\Python Projects\chord calc.py",第37行,中 Y_TANG_VALUES=DIFF(X_VALUES) ^^ 文件"",第4行,in_lambdify已生成 NameError:未定义名称‘Subs’
我不知道该怎么做才能修复这个错误.