I want to plot the integral of an integral of (singular) function in matplotlib, but my code doesn't work. Mathematically I want this:
Code:
import numpy as np
from scipy import integrate
import matplotlib.pyplot as plt
def g(x):
if (np.abs(x)<1e-10):
res = x
else:
res = x*(np.sin(1.0/x))
return res
X = np.arange(-0.5,0.5,0.001)
plot(X,g(X)) ## Doesn't work
def f(x):
res = np.zeros_like(x)
for i,val in enumerate(x):
y,err = integrate.quad(g,0,val)
res[i]=y
return res
plot(X,f(X)) ## Works
def F(x):
res = np.zeros_like(x)
for i,val in enumerate(x):
y,err = integrate.quad(f,0,val)
res[i]=y
return res
plt.plot(X,F(X)) ## Doesn't work
(Code is an adapted version of https://scicomp.stackexchange.com/a/21871/9417)
So I can't plot the original function g, because it says:
5
6 def g(x):
----> 7 if (np.abs(x)<1e-10):
8 res = x
9 else:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
And also I cannot plot the integrated integral-function as it says:
17 def f(x):
18 res = np.zeros_like(x)
---> 19 for i,val in enumerate(x):
20 y,err = integrate.quad(g,0,val)
21 res[i]=y
TypeError: 'float' object is not iterable
However plotting the first integral f works. How can I fix this? Is there a better method of plotting this with python without running into such problems?