2 variables numerical integration integral in python

264 Views Asked by At

In the following test, i want to integrate between a and b for each freq step. i need to extract the data in an array so i can display the result of the integration for each freq step. Ultimately i want to reproduce that for a complex integrale where i will plot the real and imaginary part.Since i dont have the same variables dimensions i get a broadcast error. i am not sure how to write this piece just using numpy.

f = np.sin(x)*freq  # for each freq calculate the integrale and store the result 
ValueError: operands could not be broadcast together with shapes (11,) (5,)
import numpy as np
a = 0
b = 10
n = 11
h = (b - a) / (n - 1)
x = np.linspace(a, b, n)
freq = np.linspace(0.001, 100, 5)

f = np.exp(x*freq)  # for each freq calculate the integrale and store the result of whatever function of 2 variables

#integration
for f in freq:
    I_simp = ((h/3) * (f[x[0],freq[0]] + 2*sum(f[x[:n-2:2],freq[f]]) \
                + 4*sum(f[x[1:n-1:2],freq[f]]) + f[x[n-1],freq[-1]]))

print(I_simp)    #print the array , in case of complex i will then extract real and imag
1

There are 1 best solutions below

2
duffymo On

I would recommend that you plot the function before integrating.

This plot from Wolfram, if correct, shows that the function is singular. You'll have problems with this integration.