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

Plotting the summation over different values

I have the following summation function:

import numpy as np
from sympy import var, Sum, pi, factorial, limit
import matplotlib.pyplot as plt
import math 
import scipy.misc


def n_series(x, k):
    n = np.arange(k)
    X, N = np.meshgrid(x, n)
    val =(((-1)**N)*X**(2*N + 1)) / factorial(2*N + 1)
    return np.sum(val, axis=0)

x0 = np.pi/2
x1= 2 * np.pi

x = np.linspace(x0, x1, 100)
  
for k in [0, 1, 5]:
    plt.plot(x, n_series(x, k), label=str(k))

plt.show()

and I would like to plot it for the following values: [0, 1, 5]
but unfortunately I get this error message:
enter image description here

and this:

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

enter image description here

any ideas on the this problem would be appreciated.

>Solution :

You are using sympy’s factorial on numerical data types: if possible, avoind mixing symbolic functions with numerical data.

Replace sympy’s factorial with the one from scipy:

from scipy.special import factorial

After that you are going to get the plot:

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