0

I have problem with integration an differentiation. I've tried to plot the differentitaion of x^2

plt.plot(0.5*(x[:-1]+x[1:]), np.diff(x**2, 1)/np.diff(x, 1))

And it worked. After I wanted to find the integral of x^2 times differentiation of x^2:

x = np.linspace(0, 1, 1000)     
result = spi.simps(x**2 * np.diff(x**2/np.diff(x,1)), x)
print(result)

But i got a ValueError: operands could not be broadcast together with shapes (1000,) (999,)

And I've also tried instead of x = np.linspace(0, 1, 1000) use x = 0.5*(x[:-1]+x[1:]), but no chance.

Any help would be appreciated.

1 Answer 1

2

x has a size of 1000 samples, the diff version only has 999 due to boundary differences.

You are actually aware of that fact when you do 0.5*(x[:-1]+x[1:]). Same applies when you do x**2 * np.diff(x**2/np.diff(x,1)). Perhaps what you want to do is:

x_half = 0.5*(x[:-1]+x[1:])
result = spi.simps(x_half **2 * np.diff(x**2), x_half )
Sign up to request clarification or add additional context in comments.

2 Comments

I've tried it , and it gave me the same error: ValueError: operands could not be broadcast together with shapes (1000,) (999,)
Oh yes, last argument should probably also be x_half.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.