2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PythonとSympyで重積分

Last updated at Posted at 2020-04-23

ここではPythonを使った重積分について書いていきたいと思います。

基本の形


from sympy import *

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

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

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

因みに


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

の部分は


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

または


x,y = symbols('x y')

でも大丈夫です。

また以下の様に書けばz=fの曲面を描けます。

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

積分領域が三角形

from sympy import *

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

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

#左に書いてある変数から積分する。
integrate(f,( x, 0, 2-y),(y, 0, 2))

##積分領域が扇形

from sympy import *

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

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

#積分領域を不等式で書いてもうまくいかない。
integrate(f,(x, 0, sqrt(1-y**2)),(y,0,1))

スカラー場の面積分


\vec{r}(u,v) = ( \cos u, \sin u, v)\\
D:0 \leq u \leq \pi,~~0 \leq v \leq 1\\
におけるスカラー場~f=\sqrt{x^2+y^2+z^2}の面積分の値を求めよ。
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)
#よってAとBの外積の長さは1

#pythonはcos(u)**2+sin(u)**2=1を使ってはくれないので自分で式変形。
f = sqrt(1+r[2]**2)
integrate(f,(v, 0, 1),(u,0,pi))
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?