시어핀스키 삼각형

import random
import matplotlib.pyplot as plt

def transformation_1(p):
    x=p[0]
    y=p[1]
    x1=0.5*x
    y1=0.5*y
    return x1,y1

def transformation_2(p):
    x=p[0]
    y=p[1]
    x1 = 0.5 * x +0.5
    y1 = 0.5 * y + 0.5
    return x1,y1

def transformation_3(p):
    x=p[0]
    y=p[1]
    x1 = 0.5 * x + 1
    y1 = 0.5 * y
    return x1,y1

def get_index(probability):
    r=random.random()
    c_probability=0
    sum_probability=[]
    for p in probability:
        c_probability += p
        sum_probability.append(c_probability)
    for item,sp in enumerate(sum_probability):
        if r<=sp:
            return item
    return len(probability)-1

def transform(p):
    transformations=[transformation_1,transformation_2,transformation_3]
    probability=[1/3,1/3,1/3]
    tindex=get_index(probability)
    t=transformations[tindex]
    x,y=t(p)
    return x,y

def draw_fern(n):
    x=[0]
    y=[0]

    x1,y1=0,0
    for i in range(n):
        x1,y1=transform((x1,y1))
        x.append(x1)
        y.append(y1)
    return x,y

n=int(input("Enter the number of points:"))

x,y=draw_fern(n)
plt.plot(x,y,'o')
plt.title('{0} points'.format(n))
plt.show()

10000개의 점에 대하여 실행하면 다음과 같다.

sier

Barnsley fern

Barnsley's fern uses four affine transformations. The formula for one transformation is the following:

f(x,y)={\begin{bmatrix}a&b\\c&d\end{bmatrix}}{\begin{bmatrix}x\\y\end{bmatrix}}+{\begin{bmatrix}e\\f\end{bmatrix}}

Barnsley shows the IFS code for his Black Spleenwort fern fractal as a matrix of values shown in a table. In the table, the columns "a" through "f" are the coefficients of the equation, and "p" represents the probability factor.

w a b c d e f p Portion generated
ƒ1 0 0 0 0.16 0 0 0.01 Stem
ƒ2 0.85 0.04 −0.04 0.85 0 1.60 0.85 Successively smaller leaflets
ƒ3 0.20 −0.26 0.23 0.22 0 1.60 0.07 Largest left-hand leaflet
ƒ4 −0.15 0.28 0.26 0.24 0 0.44 0.07 Largest right-hand leaflet

 

 

 

 

 

<출처: https://en.wikipedia.org/wiki/Barnsley_fern>

import random
import matplotlib.pyplot as plt

def transformation_1(p):
    x=p[0]
    y=p[1]
    x1=0.85*x +0.04*y
    y1=-0.04*x +0.85*y+1.6
    return x1,y1

def transformation_2(p):
    x=p[0]
    y=p[1]
    x1 = 0.2 * x - 0.26 * y
    y1 = 0.23 * x + 0.22 * y + 1.6
    return x1,y1

def transformation_3(p):
    x=p[0]
    y=p[1]
    x1 = -0.15 * x + 0.28 * y
    y1 = 0.26 * x + 0.24 * y + 0.44
    return x1,y1

def transformation_4(p):
    x=p[0]
    y=p[1]
    x1 = 0
    y1 = 0.16*y
    return x1,y1

def get_index(probability):
    r=random.random()
    c_probability=0
    sum_probability=[]
    for p in probability:
        c_probability += p
        sum_probability.append(c_probability)
    for item,sp in enumerate(sum_probability):
        if r<=sp:
            return item
    return len(probability)-1

def transform(p):
    transformations=[transformation_1,transformation_2,transformation_3,transformation_4]
    probability=[0.85,0.07,0.07,0.01]
    tindex=get_index(probability)
    t=transformations[tindex]
    x,y=t(p)
    return x,y

def draw_fern(n):
    x=[0]
    y=[0]

    x1,y1=0,0
    for i in range(n):
        x1,y1=transform((x1,y1))
        x.append(x1)
        y.append(y1)
    return x,y

n=int(input("Enter the number of points in the Fern:"))

x,y=draw_fern(n)
plt.plot(x,y,'o')
plt.title('Fern with {0} points'.format(n))
plt.show()

결과를 실행시켜보면 아래와 같다.

barnsley

정해진 확률로 대상 선택하기

import random

def get_index(probability):
    ex_probability=0
    sum_probability=[]
    for p in probability:
        ex_probability+=p
        sum_probability.append(ex_probability)
    r=random.random()
    for index, sp in enumerate(sum_probability):
        if r<=sp:
            return index
    return len(probability)-1

dollar=['a','b','c']
pro=[0.5,0.3,0.2]
pro_index=get_index(pro)
print(dollar[pro_index])

0.5의 확률로 a 선택, 0.3의 확률로 b 선택, 0.2의 확률로 c 선택하기

원 그래프

설문조사의 결과를 원그래프로 나타낸 파일

 

import matplotlib.pyplot as plt
import matplotlib as mpl
plt.rc('font',family='HysanB') #한글 폰트 깨짐방지
mpl.rcParams['font.size'] =20.0
labels='월 1회 퇴사','월 2회 퇴사','현행 유지','기타'
sizes=[24.2,14.5,54,7.3]
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral','lightcyan','lightpink']
explode = (0,0,0.1,0)  # only "explode" the 2nd slice (i.e. 'Hogs')
plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=90)
plt.axis('equal')
import numpy as np
plt.show()

fi

벡터장의 선적분

Find the work done by the force field

in moving an object along an arch of the cycloid

(출처, Calculus ( James Stewart ) ch16.3 problem 39)

import numpy as np
import matplotlib.pyplot as plt
import math
X, Y = np.mgrid[-0.5:2*math.pi:20j, -0.5:2.5:20j]   # 10j 는 10개 생성한다!
U = X
V = Y+2
th=np.linspace(0,2*math.pi,100)
x=th - np.sin(th)
y=1-np.cos(th)
plt.quiver(X, Y, U, V, color='k')
plt.plot(x,y,color='red')
plt.show()

Output