i'm a newbie in python and I tried to plot a (x**2 + 4*x + 4) and it's differential with sympy diff. the first function works fine but the differential value always show 0. is there anyway i can assign value on sympy differential?
import sympy as sym
from math import *
import matplotlib.pyplot as plt
#array penampung titik pada grafik
sets = []
sets2 = []
#membuat turunan dari fungsi x**2 + 4*x +4
x = sym.symbols('x')
a = sym.diff(x**2 + 4*x +4)
#mengisi array
for x in range(0, 6):
sets.append(x**2 + 4*x +4)
for x in range(0, 6):
sets2.append(x)
#just checking
print(sets2)
#menampilkan array dalam grafik
plt.plot(range(0,6),sets,'blue')
plt.plot(range(0,6),sets2,'red')
plt.ylabel('output')
plt.xlabel('input')
plt.show()
i've tried making the function inside for, using while, but it still give me errors
Your variable x
is getting replaced with numerical values by your loops. How about
f = x**2 + 4*x + 4
df = f.diff()
for xi in range(6):
sets.append(f.subs(x, xi))
sets2.append(df.subs(x, xi))