Multiple integrals with Python and Sympy

Here, I would like to write about multiple integrals using Python.

Basic shape

python



from sympy import *

x = symbols('x')
y = symbols('y')

f = x**2 + y**2 + 1

integrate(f,(x, 0, 1),(y,0,1))

By the way

python



x = symbols('x')
y = symbols('y')

Part is

python



x = Symbol('x')
y = Symbol('y')

Or

python



x,y = symbols('x y')

But it's okay.

You can also draw a curved surface with z = f by writing as follows.

python


import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

x = y = np.linspace(-5,5)
X,Y = np.meshgrid(x,y)
f = X**2 + Y**2 + 1
fig = plt.figure(figsize = (10,10))
ax = fig.add_subplot(1,1,1,projection="3d")
ax.plot_surface(X, Y, f)

ダウンロード.png

The integration area is a triangle

python


from sympy import *

x = symbols('x')
y = symbols('y')

f = x**2 + x*y*2 + 1

#Integrate from the variables written on the left.
integrate(f,( x, 0, 2-y),(y, 0, 2))

The integration region is fan-shaped

python


from sympy import *

x = symbols('x')
y = symbols('y')

f = x**2 + y**2 + 1

#Writing the integration region as an inequality does not work.
integrate(f,(x, 0, sqrt(1-y**2)),(y,0,1))

Surface integral of scalar field

python



\vec{r}(u,v) = ( \cos u, \sin u, v)\\
D:0 \leq u \leq \pi,~~0 \leq v \leq 1\\
Scalar field in~f=\sqrt{x^2+y^2+z^2}Find the value for the surface integral of.

python


from sympy import *

u = symbols('u')
v = symbols('v')

r =Matrix([ cos(u), sin(u), v])

A = [0]*3
A[0] = diff(r,u)[0]
A[1] = diff(r,u)[1]
A[2] = diff(r,u)[2]
B = [0]*3
B[0] = diff(r,v)[0]
B[1] = diff(r,v)[1]
B[2] = diff(r,v)[2]

C = np.cross(A,B)
print(C)
#Therefore, the length of the outer product of A and B is 1.

#python is cos(u)**2+sin(u)**2=You can't use 1, so you can transform the formula yourself.
f = sqrt(1+r[2]**2)
integrate(f,(v, 0, 1),(u,0,pi))

Recommended Posts

Multiple integrals with Python and Sympy
Question: Multiple integrals with python don't work
Developed and verified with multiple python versions with direnv
[Python] Solve equations with sympy
Programming with Python and Tkinter
Encryption and decryption with Python
Python and hardware-Using RS232C with Python-
python with pyenv and venv
Works with Python and R
Solve simultaneous ordinary differential equations with Python and SymPy.
Communicate with FX-5204PS with Python and PyUSB
Robot running with Arduino and python
Install Python 2.7.9 and Python 3.4.x with pip.
Neural network with OpenCV 3 and Python 3
AM modulation and demodulation with python
[Python] font family and font with matplotlib
Scraping with Node, Ruby and Python
Scraping with Python, Selenium and Chromedriver
Play with Poincare series and SymPy
JSON encoding and decoding with python
Hadoop introduction and MapReduce with Python
[GUI with Python] PyQt5-Drag and drop-
Post multiple Twitter images with python
Reading and writing NetCDF with Python
Animate multiple still images with Python
I played with PyQt5 and Python3
Coexistence of Python2 and 3 with CircleCI (1.0)
Easy modeling with Blender and Python
[Python] Creating multiple windows with Tkinter
Sugoroku game and addition game with python
FM modulation and demodulation with Python
Data pipeline construction with Python and Luigi
Calculate and display standard weight with python
FM modulation and demodulation with Python Part 3
[Automation] Manipulate mouse and keyboard with Python
Passwordless authentication with RDS and IAM (Python)
Python installation and package management with pip
Using Python and MeCab with Azure Databricks
POST variously with Python and receive with Flask
Capturing images with Pupil, python and OpenCV
Fractal to make and play with Python
A memo with Python2.7 and Python3 on CentOS
CentOS 6.4 with Python 2.7.3 with Apache with mod_wsgi and Django
Dealing with "years and months" in Python
Process multiple lists with for in Python
I installed and used Numba with Python3.5
Tweet analysis with Python, Mecab and CaboCha
Linking python and JavaScript with jupyter notebook
Traffic monitoring with Kibana, ElasticSearch and Python
FM modulation and demodulation with Python Part 2
Easily download mp3 / mp4 with python and youtube-dl!
Operate home appliances with Python and IRKit
Clean python environment with pythonz and virtualenv
Practice web scraping with Python and Selenium
Easy web scraping with Python and Ruby
Importing and exporting GeoTiff images with Python
I'm using tox and Python 3.3 with Travis-CI
Happy GUI construction with electron and python
Use Python and MeCab with Azure Functions
Manage multiple Python versions with update-alternatives (Ubuntu)
Touch AWS with Serverless Framework and Python