How To Plot A Summation With Two Variables In Python 3
I would like to make a function and use pyplot to plot it nicely. The function it self looks like this: I am tasked with plotting C for k = [2, 4, 6, 8, 10] for x in range [-3pi,
Solution 1:
x and k must be the input of your function, to simplify the task we create a mesh with meshgrid and then add in one direction and we will obtain the profile of the curve for each k.
import numpy as np
import matplotlib.pyplot as plt
import math
from scipy import misc
def C_series(x, k):
n = np.arange(k)
X, N = np.meshgrid(x, n)
val =(((-1)**N)*X**(2*N)) / misc.factorial(2*N)
return np.sum(val, axis=0)
x0 = -3*math.pi
xf= 3*math.pi
x = np.linspace(x0, xf, 100)
plt.plot(x, np.cos(x))
for k in [2, 4, 6, 8, 10]:
plt.plot(x, C_series(x, k), label=str(k))
plt.show()
Screenshot:
for x0 = -1
and xf= 1
Post a Comment for "How To Plot A Summation With Two Variables In Python 3"