코딩 수학


sympy ¸ðµâÀ» ºÒ·¯¿Â´Ù. ¶ÇÇÑ, ÀÚÁÖ ¾²´Â º¯¼ö¸¦ ¼±¾ðÇÑ´Ù.

In [1]:
from sympy import *
init_printing()             # ¼ö½ÄÀ» º¸±âÁÁ°Ô Ãâ·ÂÇÏ´Â ÇÔ¼ö¸¦ È£ÃâÇÑ´Ù

x, y, z, t = symbols('x y z t')

±×·¡ÇÁ¸¦ ±×·ÁÁÖ´Â ¸ËÇ÷Ը³ ¸ðµâÀ» ºÒ·¯¿Â´Ù.

In [2]:
%matplotlib inline      

직선의 방정식

Á÷¼± $y=2x-1$ À» ±×¸°´Ù.

In [3]:
plot( 2*x - 1 )
Out[3]:
<sympy.plotting.plot.Plot at 0x780d710>

Ưº°È÷ $x$ ÀÇ ¹üÀ§¸¦ ÁöÁ¤ÇÏÁö ¾ÊÀ¸¸é, $(-10,10)$ ¹üÀ§¿¡¼­ ±×·ÁÁø´Ù.

$x$ ÀÇ ¹üÀ§¸¦ $-2$ ¿¡¼­ $3$ ±îÁö ÁöÁ¤ÇÏ¿© ±×·Áº¸ÀÚ.

In [4]:
plot( 2*x-1, (x,-2,3) )
Out[4]:
<sympy.plotting.plot.Plot at 0x78c44b0>

´ÙÀ½ ÀÏÂ÷ ¹æÁ¤½ÄÀÌ ³ªÅ¸³»´Â Á÷¼±À» ±×¸°´Ù.

$\qquad 2x+3y-5=0$

À½ÇÔ¼ö ÇüÅ·ΠÁÖ¾îÁø À§ ¹æÁ¤½ÄÀ» $y$ ¿¡ ´ëÇÏ¿© Ç®¾î¼­, ¾çÇÔ¼ö ÇüÅ·Π¾ò´Â´Ù.

In [5]:
soln = solve( Eq(2*x+3*y-5,0), y )
soln
Out[5]:
$$\left [ - \frac{2 x}{3} + \frac{5}{3}\right ]$$

°ýÈ£ [ ] ¸¦ ¹þ°Ü³»±â À§Çؼ­, ù¹øÂ° ¿ø¼Ò¸¦ ÃëÇÑ´Ù. ( [0] °¡ ù¹øÂ° ¿ø¼Ò¸¦ ¶æÇÔ )

In [6]:
soln[0]
Out[6]:
$$- \frac{2 x}{3} + \frac{5}{3}$$

À§ ¼ö½ÄÀÌ ¾çÇÔ¼ö ÇüÅÂÀÇ $y$ À̰í, À̸¦ ±×·¡ÇÁ·Î ±×¸°´Ù.

In [7]:
plot( _ )
Out[7]:
<sympy.plotting.plot.Plot at 0x580ff50>

À½ÇÔ¼ö (implicit function) ÇüÅ·ΠÁÖ¾îÁø ¹æÁ¤½ÄÀ» Á÷Á¢ ±×¸± ¼öµµ ÀÖ´Ù.

$\qquad x-2y+2=0$

plot_imiplicit( ) À» ÀÌ¿ëÇÑ´Ù.

In [8]:
plot_implicit( Eq( 2*x+3*y-5, 0 ) )
Out[8]:
<sympy.plotting.plot.Plot at 0x7b9d190>

원의 방정식

¿øÀÇ ¹æÁ¤½ÄÀ» ±×·¡ÇÁ·Î ±×·Áº¸ÀÚ.

$\qquad x^2+y^2=1$

$y$ ¸¦ ¾çÇÔ¼ö ÇüŸ¦ ¾ò±â À§Çؼ­, ¹æÁ¤½ÄÀ» Ç®¸é

In [9]:
Ys = solve( Eq( x**2 + y**2, 1 ), y )
Ys
Out[9]:
$$\left [ - \sqrt{- x^{2} + 1}, \quad \sqrt{- x^{2} + 1}\right ]$$

µÎ ½ÄÀº $x$ ÃàÀÇ À§¿Í ¾Æ·¡¿¡ °¢°¢ ¹Ý¿øÀ» ³ªÅ¸³ª´Â ÇÔ¼öµéÀÌ´Ù. µÎ ½ÄÀ» ÇÔ²² ±×¸®¸é

In [10]:
plot( Ys[0], Ys[1], xlim=(-3,3), ylim=(-2,2) )
Out[10]:
<sympy.plotting.plot.Plot at 0x7c27af0>

xlim °ú ylim Àº °¢°¢ $x$ Ãà°ú $y$ ÃàÀÇ ¹üÀ§¸¦ ÁöÁ¤ÇÏ´Â ÀÎÀÚÀÌ´Ù.

À½ÇÔ¼ö ÇüÅ·Π¿øÀÇ ¹æÁ¤½ÄÀ» Á÷Á¢ ±×¸± ¼öµµ ÀÖ´Ù.

$\qquad x^2+y^2=1$

plot_implicit( ) À» ÀÌ¿ëÇÑ´Ù.

In [11]:
plot_implicit( Eq( x**2 + y**2, 1 ), (x,-3,3), (y,-2,2) )
Out[11]:
<sympy.plotting.plot.Plot at 0x7902270>

¿øÀÇ ¹æÁ¤½Ä $ x^2+y^2=1$ ¿¡ ´ëÇÏ¿©, ¸Å°³º¯¼ö $t$ ¸¦ ÀÌ¿ëÇÏ¿© $x, y$ ¸¦ Ç¥ÇöÇϸé

$\qquad x = \cos t \,, \quad y = \sin t $

¸Å°³º¯¼ö·Î Ç¥ÇöÇÑ ÇÔ¼ö¸¦ ±×·¡ÇÁ¸¦ ±×¸®·Á¸é, plot_parametric( ) À» ÀÌ¿ëÇÑ´Ù.

In [12]:
from sympy.plotting import plot_parametric

plot_parametric( cos(t), sin(t), (t, 0, 2*pi),  xlim=(-3,3), ylim=(-2,2) )
Out[12]:
<sympy.plotting.plot.Plot at 0x7bc67f0>

원과 직선

¿ø°ú Á÷¼±ÀÇ ±³Â÷Á¡À» ±×·¡ÇÁ·Î ã´Â´Ù.

\begin{align} x^2+y^2 &= 4 \\ 2x-y+1 &= 0 \end{align}

¿©·¯°³ÀÇ À½ÇÔ¼öµéÀ» ÇÔ²² ±×¸®·Á¸é, Or ±â´ÉÀ» »ç¿ëÇÑ´Ù.

In [13]:
plot_implicit( Or( Eq( x**2+y**2,4 ), Eq( 2*x-y+1,0 ) ), (x,-4,5), (y,-3,3) )
Out[13]:
<sympy.plotting.plot.Plot at 0x7fb1a90>

¿ø°ú Á÷¼±ÀÇ ±³Á¡À» ã±â À§Çؼ­, ¿¬¸³ ¹æÁ¤½ÄÀ» Ǭ´Ù

In [14]:
Ps = solve( [ Eq( x**2 + y**2, 4 ), Eq( 2*x - y + 1, 0 ) ], [x,y] )
Ps
Out[14]:
$$\left [ \left ( - \frac{2}{5} + \frac{\sqrt{19}}{5}, \quad \frac{1}{5} + \frac{2 \sqrt{19}}{5}\right ), \quad \left ( - \frac{\sqrt{19}}{5} - \frac{2}{5}, \quad - \frac{2 \sqrt{19}}{5} + \frac{1}{5}\right )\right ]$$

µÎ ±³Á¡ÀÇ ¼öÄ¡ÀûÀÎ °ªµéÀ» N( ) À¸·Î ±¸ÇØ º¸¸é

In [15]:
( N( Ps[0][0] ),  N( Ps[0][1] ) )            # 1»çºÐ¸é ±³Á¡ ÁÂÇ¥
Out[15]:
$$\left ( 0.471779788708135, \quad 1.94355957741627\right )$$
In [16]:
( N( Ps[1][0] ),  N( Ps[1][1] ) )            # 3»çºÐ¸é ±³Á¡ ÁÂÇ¥
Out[16]:
$$\left ( -1.27177978870813, \quad -1.54355957741627\right )$$

부등식

ºÎµî½Ä $y>x^2-1$ ÀÇ ¿µ¿ªÀ» ±×·¡ÇÁ·Î Ç¥½ÃÇϸé

In [17]:
plot_implicit( y > x**2 - 1 )
Out[17]:
<sympy.plotting.plot.Plot at 0x3e3af50>

¿ø $x^2+y^2=4$ ÀÇ ³»ºÎ ¿µ¿ªÀ» ±×·¡ÇÁ·Î ³ªÅ¸³½´Ù.

In [18]:
plot_implicit( x**2 + y**2 < 4, (x,-4.5,4.5), (y,-3,3) )
Out[18]:
<sympy.plotting.plot.Plot at 0x8110af0>

ºÎµî½Ä $(x-y)(x+y)<0$ ÀÇ ¿µ¿ªÀ» ±×·¡ÇÁ·Î ³ªÅ¸³»¸é

In [19]:
plot_implicit( (x-y)*(x+y) < 0, (x,-3,3), (y,-2,2) )
Out[19]:
<sympy.plotting.plot.Plot at 0x8360c10>

¿¬¸³ºÎµî½ÄÀÇ ¿µ¿ªÀ» ±×·¡ÇÁ·Î ³ªÅ¸³½´Ù

\begin{align} 2x \, + \, y & > 2 \\ x^2+y^2 & < 9 \end{align}

µÎ ºÎµî½ÄÀ» µ¿½Ã¿¡ ¸¸Á·ÇÏ´Â ¿µ¿ªÀ» ãÀ¸·Á¸é, And ±â´ÉÀ» »ç¿ëÇÑ´Ù.

In [20]:
plot_implicit( And( 2*x + y > 2, x**2 + y**2 < 9 ), (x,-6,6), (y,-4,4) )
Out[20]:
<sympy.plotting.plot.Plot at 0x8132950>