0

I am trying to generate a plot for double integral using python, matplotlib. X and Y ranges are defined and Z is the double integral of X and Y.

import matplotlib.pyplot as plt
import numpy as np
import scipy
from scipy.integrate import dblquad

fig, ax = plt.subplots(subplot_kw={"projection": "3d"})

def function_I(x, y):
    return np.sin(x)*np.cos(y/5)

#Make data
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
Z = scipy.integrate.dblquad(function_I, -5, 5, -5, 5)

# plot the surface
ax.plot_surface(X, Y, Z)

fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()

line

ax.plot_surface(X, Y, Z)

gives

AttributeError: 'tuple' object has no attribute 'ndim'

some similar question provided solutions related to .shape and .reshape? Doesn't make a whole lot sense. Any comments appreciated!

8
  • Will you please provide the full error traceback, in the question? Commented Mar 11, 2022 at 0:37
  • 2
    Check the documentation for scipy.integrate.dblquad. It returns more than an array. I found this page by copy/pasting "scipy.integrate.dblquad" into google and clicking on the first result: docs.scipy.org/doc/scipy/reference/generated/… Commented Mar 11, 2022 at 0:41
  • 1
    Check the type of the 3 arguments, X, Y, Z. Before searching the web or asking us, you should have a clear idea of what each of the variables is. Commented Mar 11, 2022 at 0:42
  • Try ax.plot_surface(X, Y, np.array(Z)) Commented Mar 11, 2022 at 0:45
  • 1
    Thx for the comments, ans, err = scipy.integrate.dblquad(function_I(X,Y), -5, 5, -5, 5) is indeed more accurate, still trying to figure this out... Commented Mar 11, 2022 at 0:59

1 Answer 1

1

dblquad(function_I, -5, 5, -5, 5) calculates the integral of one complete area, so resulting in a single value.

The following approach intents to calculate the integral for each little patch between successive coordinates. And then draw a surface using the mean XY position of each patch.

import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import dblquad

def function_I(x, y):
    return np.sin(x) * np.cos(y / 5)

# Make data
X = np.linspace(-5, 5, 41)
Y = np.linspace(-5, 5, 41)
X, Y = np.meshgrid(X, Y)
Z = np.zeros_like(X)
for i in range(1, X.shape[0]):
    for j in range(1, X.shape[1]):
        Z[i, j], _ = dblquad(function_I, X[i - 1, j - 1], X[i, j], Y[i - 1, j - 1], Y[i, j])

# plot the surface
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.plot_surface((X[:-1, :-1] + X[1:, 1:]) / 2, (Y[:-1, :-1] + Y[1:, 1:]) / 2, Z[1:, 1:])

plt.show()

plot_surface of dblquad

Sign up to request clarification or add additional context in comments.

Comments

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.