Python3 Next 2

Similar documents
Windows (L): D:\jyugyou\ D:\jyugyou\ D:\jyugyou\ (N): en2 OK 2

1 Python LOGO Python Python Python Anaconda Python Anaconda Python Python 3.6 version Python2 Python3 Python 2.7 Py

cards.gif from Tkinter import * root = Tk() c0 = Canvas(root, width = 400, height = 300) c0.pack() image_data = PhotoImage(file = c1.gif ) c0.create_i

from Tkinter import * root = Tk() c0 = Canvas(root, width = 400, height = 300) c0.pack() image_data = PhotoImage(file = c1.gif ) c0.create_image(200,

i

I, II 1, A = A 4 : 6 = max{ A, } A A 10 10%

2009 IA 5 I 22, 23, 24, 25, 26, (1) Arcsin 1 ( 2 (4) Arccos 1 ) 2 3 (2) Arcsin( 1) (3) Arccos 2 (5) Arctan 1 (6) Arctan ( 3 ) 3 2. n (1) ta

Python Speed Learning

N88 BASIC 0.3 C: My Documents 0.6: 0.3: (R) (G) : enterreturn : (F) BA- SIC.bas 0.8: (V) 0.9: 0.5:

1/1 lim f(x, y) (x,y) (a,b) ( ) ( ) lim limf(x, y) lim lim f(x, y) x a y b y b x a ( ) ( ) xy x lim lim lim lim x y x y x + y y x x + y x x lim x x 1

1 matplotlib matplotlib Python matplotlib numpy matplotlib Installing A 2 pyplot matplotlib 1 matplotlib.pyplot matplotlib.pyplot plt import import nu

GraphicsWithPlotFull.nb Plot[{( 1), ( ),...}, {( ), ( ), ( )}] Plot Plot Cos x Sin x, x, 5 Π, 5 Π, AxesLabel x, y x 1 Plot AxesLabel

f(x,y) (x,y) x (x,y), y (x,y) f(x,y) x y f x (x,y),f y (x,y) B p.1/14

たのしいプログラミング Pythonではじめよう!

S I. dy fx x fx y fx + C 3 C dy fx 4 x, y dy v C xt y C v e kt k > xt yt gt [ v dt dt v e kt xt v e kt + C k x v + C C k xt v k 3 r r + dr e kt S dt d

Python (Anaconda ) Anaconda 2 3 Python Python IDLE Python NumPy 6

M3 x y f(x, y) (= x) (= y) x + y f(x, y) = x + y + *. f(x, y) π y f(x, y) x f(x + x, y) f(x, y) lim x x () f(x,y) x 3 -

(pack ) Toplevel

4 4 4 a b c d a b A c d A a da ad bce O E O n A n O ad bc a d n A n O 5 {a n } S n a k n a n + k S n a a n+ S n n S n n log x x {xy } x, y x + y 7 fx

(1) + b = b +, (2) b = b, (3) + 0 =, (4) 1 =, (5) ( + b) + c = + (b + c), (6) ( b) c = (b c), (7) (b + c) = b + c, (8) ( + b)c = c + bc (9

listings-ext

S I. dy fx x fx y fx + C 3 C vt dy fx 4 x, y dy yt gt + Ct + C dt v e kt xt v e kt + C k x v k + C C xt v k 3 r r + dr e kt S Sr πr dt d v } dt k e kt

1 No.1 5 C 1 I III F 1 F 2 F 1 F 2 2 Φ 2 (t) = Φ 1 (t) Φ 1 (t t). = Φ 1(t) t = ( 1.5e 0.5t 2.4e 4t 2e 10t ) τ < 0 t > τ Φ 2 (t) < 0 lim t Φ 2 (t) = 0


Excel ではじめる数値解析 サンプルページ この本の定価 判型などは, 以下の URL からご覧いただけます. このサンプルページの内容は, 初版 1 刷発行時のものです.

v er.1/ c /(21)

PYTHON 資料 電脳梁山泊烏賊塾 PYTHON 入門 ゲームプログラミング スプライトの衝突判定 スプライトの衝突判定 スプライトの衝突判定の例として インベーダーゲームのコードを 下記に示す PYTHON3 #coding: utf-8 import pygame from pygame.lo

( ) a, b c a 2 + b 2 = c : 2 2 = p q, p, q 2q 2 = p 2. p 2 p q 2 p, q (QED)


入試の軌跡

1.2 y + P (x)y + Q(x)y = 0 (1) y 1 (x), y 2 (x) y 1 (x), y 2 (x) (1) y(x) c 1, c 2 y(x) = c 1 y 1 (x) + c 2 y 2 (x) 3 y 1 (x) y 1 (x) e R P (x)dx y 2

1.1 ft t 2 ft = t 2 ft+ t = t+ t d t 2 t + t 2 t 2 = lim t 0 t = lim t 0 = lim t 0 t 2 + 2t t + t 2 t 2 t + t 2 t 2t t + t 2 t 2t + t = lim t 0

Python ( ) Anaconda 2 3 Python Python IDLE Python NumPy 6 5 matpl


y π π O π x 9 s94.5 y dy dx. y = x + 3 y = x logx + 9 s9.6 z z x, z y. z = xy + y 3 z = sinx y 9 s x dx π x cos xdx 9 s93.8 a, fx = e x ax,. a =

曲面のパラメタ表示と接線ベクトル

Python Speed Learning

, x R, f (x),, df dx : R R,, f : R R, f(x) ( ).,, f (a) d f dx (a), f (a) d3 f dx 3 (a),, f (n) (a) dn f dx n (a), f d f dx, f d3 f dx 3,, f (n) dn f

6. Euler x

num2.dvi


,. Black-Scholes u t t, x c u 0 t, x x u t t, x c u t, x x u t t, x + σ x u t, x + rx ut, x rux, t 0 x x,,.,. Step 3, 7,,, Step 6., Step 4,. Step 5,,.

5.. z = f(x, y) y y = b f x x g(x) f(x, b) g x ( ) A = lim h 0 g(a + h) g(a) h g(x) a A = g (a) = f x (a, b)

or a 3-1a (0 b ) : max: a b a > b result a result b ( ) result Python : def max(a, b): if a > b: result = a else: result = b ret

1 8, : 8.1 1, 2 z = ax + by + c ax by + z c = a b +1 x y z c = 0, (0, 0, c), n = ( a, b, 1). f = n i=1 a ii x 2 i + i<j 2a ij x i x j = ( x, A x), f =

C:/KENAR/0p1.dvi

x () g(x) = f(t) dt f(x), F (x) 3x () g(x) g (x) f(x), F (x) (3) h(x) = x 3x tf(t) dt.9 = {(x, y) ; x, y, x + y } f(x, y) = xy( x y). h (x) f(x), F (x

pdf

I A A441 : April 21, 2014 Version : Kawahira, Tomoki TA (Kondo, Hirotaka ) Google

DVIOUT

5.. z = f(x, y) y y = b f x x g(x) f(x, b) g x ( ) A = lim h g(a + h) g(a) h g(x) a A = g (a) = f x (a, b)

No2 4 y =sinx (5) y = p sin(2x +3) (6) y = 1 tan(3x 2) (7) y =cos 2 (4x +5) (8) y = cos x 1+sinx 5 (1) y =sinx cos x 6 f(x) = sin(sin x) f 0 (π) (2) y

webkaitou.dvi


<4D F736F F D B B83578B6594BB2D834A836F815B82D082C88C60202E646F63>

: Shift-Return evaluate 2.3 Sage? Shift-Return abs 2 abs? 2: abs 3: fac

1 1.1 ( ). z = a + bi, a, b R 0 a, b 0 a 2 + b 2 0 z = a + bi = ( ) a 2 + b 2 a a 2 + b + b 2 a 2 + b i 2 r = a 2 + b 2 θ cos θ = a a 2 + b 2, sin θ =

Appendix A BASIC BASIC Beginner s All-purpose Symbolic Instruction Code FORTRAN COBOL C JAVA PASCAL (NEC N88-BASIC Windows BASIC (1) (2) ( ) BASIC BAS

x = a 1 f (a r, a + r) f(a) r a f f(a) 2 2. (a, b) 2 f (a, b) r f(a, b) r (a, b) f f(a, b)

II No.01 [n/2] [1]H n (x) H n (x) = ( 1) r n! r!(n 2r)! (2x)n 2r. r=0 [2]H n (x) n,, H n ( x) = ( 1) n H n (x). [3] H n (x) = ( 1) n dn x2 e dx n e x2

( ) kadai4, kadai4.zip.,. 3 cos x [ π, π] Python. ( 100 ), x cos x ( ). (, ). def print cos(): print cos()

1 1 sin cos P (primary) S (secondly) 2 P S A sin(ω2πt + α) A ω 1 ω α V T m T m 1 100Hz m 2 36km 500Hz. 36km 1

2 1 κ c(t) = (x(t), y(t)) ( ) det(c (t), c x (t)) = det (t) x (t) y (t) y = x (t)y (t) x (t)y (t), (t) c (t) = (x (t)) 2 + (y (t)) 2. c (t) =

() x + y + y + x dy dx = 0 () dy + xy = x dx y + x y ( 5) ( s55906) 0.7. (). 5 (). ( 6) ( s6590) 0.8 m n. 0.9 n n A. ( 6) ( s6590) f A (λ) = det(a λi)

40 6 y mx x, y 0, 0 x 0. x,y 0,0 y x + y x 0 mx x + mx m + m m 7 sin y x, x x sin y x x. x sin y x,y 0,0 x 0. 8 x r cos θ y r sin θ x, y 0, 0, r 0. x,

.3. (x, x = (, u = = 4 (, x x = 4 x, x 0 x = 0 x = 4 x.4. ( z + z = 8 z, z 0 (z, z = (0, 8, (,, (8, 0 3 (0, 8, (,, (8, 0 z = z 4 z (g f(x = g(

(1) (2) (3) (4) HB B ( ) (5) (6) (7) 40 (8) (9) (10)

() n C + n C + n C + + n C n n (3) n C + n C + n C 4 + n C + n C 3 + n C 5 + (5) (6 ) n C + nc + 3 nc n nc n (7 ) n C + nc + 3 nc n nc n (

y = f(x) y = f( + h) f(), x = h dy dx f () f (derivtive) (differentition) (velocity) p(t) =(x(t),y(t),z(t)) ( dp dx dt = dt, dy dt, dz ) dt f () > f x

programmingII2019-v01

コンピュータ概論

(3) (2),,. ( 20) ( s200103) 0.7 x C,, x 2 + y 2 + ax = 0 a.. D,. D, y C, C (x, y) (y 0) C m. (2) D y = y(x) (x ± y 0), (x, y) D, m, m = 1., D. (x 2 y

Transcription:

Python Python Tkinter Tkinter Python Python Anaconda Python Anaconda Python https://www.continuum.io/downloads Python 3.6 version Python2 Python3 Python 2.7 Python 3.6 Python2 1

Python3 Next 2

I Agree Next 3

Destination Folder C:\Anaconda3 Next 4

Python3.6 Python3.6 5

Install Next 6

Finish Anaconda idle 7

8

Anaconda3 idle 9

Python3.6.1 Shell Anaconda python 10

Python Python Python Shell 11

File New File Editor from tkinter import * root = Tk() canvas = Canvas(root, width = 360, height=360) canvas.pack() root.mainloop() 12

Run Run Module OK en.py 360 360 from tkinter import * 13

root = Tk() canvas = Canvas(root, width = 360, height=360) canvas.pack() root.mainloop() from tkinter import * tkinter import tkinter Python root = Tk() top level main window root canvas = Canvas(root, width = 360, height=360) canvas.pack() window 360 360 Canvas canvas main window root root.mainloop() event loop user (event: ) tkinter window canvas.create_oval(50, 50, 310, 310) from tkinter import * root = Tk() canvas = Canvas(root, width = 360, height=360) canvas.create_oval(50, 50, 310, 310) canvas.pack() root.mainloop() 14

Run Run Module OK 15

canvas.create_oval(50, 50, 310, 310) (50,50) (310, 310) canvas (180, 180) 130 x y x y canvas (180 + 130 cos(t), 180 130 sin(t)) (180 + 130 cos(2 t), 180 130 sin(2 t)) t = 0 t = 2 pi pi/80 180 130 sin(t) cos(), sin(), pi from math import * t = 0 while t < 2*pi: canvas.create_line(180+130*cos(t), 180-130*sin(t), 180+130*cos(2*t), 180-130*sin(2*t)) t = t + pi/80 from tkinter import * from math import * root = Tk() canvas = Canvas(root, width = 360, height=360) canvas.create_oval(50, 50, 310, 310) t = 0 while t < 2*pi: canvas.create_line(180+130*cos(t), 180-130*sin(t), 16

t = t + pi/80 canvas.pack() root.mainloop() 180+130*cos(2*t), 180-130*sin(2*t)) 17

canvas.create_line(180+130*cos(t), 180-130*sin(t), 180+130*cos(2*t), 180-130*sin(2*t)) canvas.create_line(180+130*cos(t), 180-130*sin(t), 180+130*cos(3*t), 180-130*sin(3*t)) canvas.create_line(180+130*cos(2*t), 180-130*sin(2*t), 180+130*cos(5*t), 180-130*sin(5*t)) Python Pygame Pygame Python Anaconda Python 18

import pygame from math import * pygame.init() black = (0,0,0) white = (255, 255, 255) green = (0, 255, 0) red= (255, 0, 0) size = (700, 500) screen = pygame.display.set_mode(size) pi = atan(1.0)*4 done = False clock = pygame.time.clock() t = pi while done == False: for event in pygame.event.get(): if event.type == pygame.quit: done = True screen.fill(white) x = 0 while x <= 2*pi-2*t: pygame.draw.line(screen, green, [200+100*cos(x), 200-100*sin(x)],\ [200+100*cos(2*x), 200-100*sin(2*x)], 1) x += pi/40 t -= pi/40 pygame.display.flip() if t < 0: t = pi clock.tick(1) pygame.quit() 19

Pygame http://www.youtube.com/playlistlist=pl1d91f4e6e79e73e1? Python Pygame youtube 15 http://programarcadegame.com on-line Simpson College Dr. Paul Vincent Craven Linux Pygame Python youtube Timer Visual C++ C++ Builder Visual C++ C++ Builder Raspberry Pi Python Python Scratch y = exp(x) File New File Editor 20

from tkinter import * root = Tk() button = Button(root, text= NEXT ) canvas = Canvas(root, width=500, height=400) canvas.pack() button.pack() root.mainloop() 21

button = Button(root, text= NEXT ) button.pack() Canvas button = Button(root, text= NEXT ) button.pack() NEXT Button root Run Run Module OK exp.py from tkinter import * root = Tk() button = Button(root, text= NEXT ) canvas = Canvas(root, width=500, height=400) button.pack() canvas.pack() root.mainloop() 22

button = Button(root, text= NEXT ) button = Button(root, text= NEXT, command=paint) paint paint def paint(): canvas.create_line(0, 200, 500, 200) canvas.create_line(250, 0, 250, 400) from tkinter import * def paint(): canvas.create_line(0, 200, 500, 200) canvas.create_line(250, 0, 250, 400) root = Tk() button = Button(root, text= NEXT, command=paint) canvas = Canvas(root, width=500, height=400) canvas.pack() button.pack() root.mainloop() 23

Run Run Module OK NEXT 24

paint def paint(): canvas.create_line(0, 200, 500, 200) canvas.create_line(250, 0, 250, 400) for x in range(0, 500, 50): canvas.create_line(x, 185, x, 215) for y in range(0, 400, 50): canvas.create_line(235, y, 265, y) from tkinter import * def paint(): canvas.create_line(0, 200, 500, 200) canvas.create_line(250, 0, 250, 400) for x in range(0, 500, 50): canvas.create_line(x, 185, x, 215) for y in range(0, 400, 50): canvas.create_line(235, y, 265, y) root = Tk() button = Button(root, text= NEXT, command=paint) canvas = Canvas(root, width=500, height=400) canvas.pack() button.pack() root.mainloop() NEXT 25

y = exp(x) (5 <= x <= 5) paint def paint(): canvas.create_line(0, 200, 500, 200) canvas.create_line(250, 0, 250, 400) for x in range(0, 500, 50): canvas.create_line(x, 185, x, 215) for y in range(0, 400, 50): canvas.create_line(235, y, 265, y) x = -5 dx = 0.1 while x <= 5: canvas.create_line(250+50*x, 200-50*exp(x), 250+50*(x+dx), 200-50*exp(x+dx)) x = x + dx from tkinter import * from math import * def paint(): canvas.create_line(0, 200, 500, 200) canvas.create_line(250, 0, 250, 400) for x in range(0, 500, 50): canvas.create_line(x, 185, x, 215) for y in range(0, 400, 50): canvas.create_line(235, y, 265, y) x = -5 26

dx = 0.1 while x <= 5: canvas.create_line(250+50*x, 200-50*exp(x), 250+50*(x+dx), 200-50*exp(x+dx)) x = x + dx root = Tk() button = Button(root, text= NEXT, command=paint) canvas = Canvas(root, width=500, height=400) canvas.pack() button.pack() root.mainloop() NEXT y = exp(x) -5 <= x <= 5 canvas.create_line(250+50*x, 200-50*exp(x), 250+50*(x+dx), 200-50*exp(x+dx)) canvas.create_line(250+50*x, 200-50*exp(x), 250+50*(x+dx), 200-50*exp(x+dx), fill= blue, width=3.0) NEXT 27

y = exp(x) 5 <= x <= 5 blue Next exp(x) fact(n) n + 1 expn(n, x) def fact(n): r = 1.0 for i in range(1, n+1, 1): r = r * i return r def expn(n, x): s = 0.0 for i in range(n+1): s = s + pow(x, i) / fact(i) return s def paint(): gloval N = 1 N = -1 28

Next N 1 0, 1, 2, 3, 4, n = N expn(n, x) paint() def paint(): global N n = N canvas.create_line(0, 200, 500, 200) canvas.create_line(250, 0, 250, 400) for x in range(0, 500, 50): canvas.create_line(x, 185, x, 215) for y in range(0, 400, 50): canvas.create_line(235, y, 265, y) x = -5 dx = 0.1 while x <= 5: canvas.create_line(250+50*x, 200-50*exp(x), 250+50*(x+dx), 200-50*exp(x+dx), fill= blue, width=3.0) x = x + dx if n >= 0: x = -5 dx = 0.1 while x <= 5: canvas.create_line(250+50*x, 200-50*expN(n, x), 250+50*(x+dx), 200-50*expN(n, x+dx), fill= red, width=3.0) x = x + dx N = N+1 global N N from tkinter import * from math import * N = -1 def fact(n): r = 1.0 for i in range(1, n+1, 1): 29

r = r * i return r def expn(n, x): s = 0.0 for i in range(n+1): s = s + pow(x, i)/ fact(i) return s def paint(): global N n = N canvas.create_line(0, 200, 500, 200) canvas.create_line(250, 0, 250, 400) for x in range(0, 500, 50): canvas.create_line(x, 185, x, 215) for y in range(0, 400, 50): canvas.create_line(235, y, 265, y) x = -5 dx = 0.1 while x <= 5: canvas.create_line(250+50*x, 200-50*exp(x), 250+50*(x+dx), 200-50*exp(x+dx), fill= blue, width=3.0) x = x + dx if n >= 0: x = -5 dx = 0.1 while x <= 5: canvas.create_line(250+50*x, 200-50*expN(n, x), 250+50*(x+dx), 200-50*expN(n, x+dx), fill= red, width=3.0) x = x + dx N = N+1 root = Tk() button = Button(root, text= NEXT, command=paint) canvas = Canvas(root, width=500, height=400) canvas.pack() button.pack() root.mainloop() NEXT 30

31

y = expn(n; x) y = exp(x) paint 32

canvas.create_rectangle(0, 0, 500, 400, fill= white ) paint def paint(): global N n = N canvas.create_rectangle(0, 0, 500, 400, fill= white ) canvas.create_line(0, 200, 500, 200) canvas.create_line(250, 0, 250, 400) for x in range(0, 500, 50): canvas.create_line(x, 185, x, 215) for y in range(0, 400, 50): canvas.create_line(235, y, 265, y) x = -5 dx = 0.1 while x <= 5: canvas.create_line(250+50*x, 200-50*exp(x), 250+50*(x+dx), 200-50*exp(x+dx), fill= blue, width=3.0) x = x + dx if n >= 0: x = -5 dx = 0.1 while x <= 5: canvas.create_line(250+50*x, 200-50*expN(n, x), 250+50*(x+dx), 200-50*expN(n, x+dx), fill= red, width=3.0) x = x + dx N = N+1 33

34

35

fact(n) def fact(n): r = 1.0 for i in range(1, n+1, 1): r = r * i return r 4 def ( ) : fact n r = 1.0 for i in range(1, n+1, 1): r = r * i return r r (r 1.0 1 1.0 r ) for i 1 n i 1 n r for r n r return n! = 1 * 2 * 3 *... * n Python Prolog Scheme 36

def fact(n): if n == 0: return 1.0 else: return n * fact(n-1) def fact(n): r = 1 for i in range(1, n+1, 1): r = r * i return r def fact(n): if n == 0: return 1 else: return n * fact(n-1) if Scheme if : (true) if : else: (true) if A: else: if B: else: 37

if A: elif B: else: if A: elif B: elif C: elif D: else: elif n + 1 expn(n, x) def expn(n, x): s = 0.0 for i in range(n+1): s = s + pow(x, i) / fact(i) return s expn n, x n x, Python expn(n, x) n x Pyton s = 0.0 for i in range(n+1): s = s + pow(x, i) / fact(i) return s s s 0.0 s for i 0 n s x i i s return Prolog Scheme def expn(n, x): if n == -1: return 0.0 38

else: return pow(x, n) / fact(n) + expn(n-1, x) C++ pow(x, n) Python x n sqrt(x) x 0.5 range(n) fact(n) def fact(n): r = 1.0 for i in range(1, n+1, 1): r = r * i return r def fact(n): r = 1.0 for i in range(n): r = r * i return r expn(n, x) def expn(n, x): s = 0.0 for i in range(n): s = s + pow(x, i) / fact(i) return s NEXT 39

File "D:\python\ \exp.py", line 15, in expn s = s + pow(x, i)/ fact(i) ZeroDivisionError: float division by zero fact(i) 0 fact(n) 0 def fact(n): r = 1.0 for i in range(n): r = r * i return r range(n) 0 for i in range(n): r = r * i i 0, 1, 2, for r = 0 range(n) i = 0 i=1 def fact(n): r = 1.0 40

for i in range(1, n): r = r * i return r y = exe(x) y = expn(n; x) y = exe(x) r = 1.0 for i in range(1, n): r = r * i range(1, n) = [1, 2, 3,, n 1] fact(n) = n! fact(n) = (n 1)! expn(n, x) x = -1 print( "fact(", n, ")=", fact(n)) print( "expn(", n, ",", x, ")=", expn(n, x)) from tkinter import * from math import * N = -1 def fact(n): 41

r = 1.0 for i in range(1, n): r = r * i return r def expn(n, x): s = 0.0 for i in range(n): s = s + pow(x, i)/ fact(i) return s def paint(): global N n = N canvas.create_line(0, 200, 500, 200) canvas.create_line(250, 0, 250, 400) for x in range(0, 500, 50): canvas.create_line(x, 185, x, 215) for y in range(0, 400, 50): canvas.create_line(235, y, 265, y) x = -5 dx = 0.1 while x <= 5: canvas.create_line(250+50*x, 200-50*exp(x), 250+50*(x+dx), 200-50*exp(x+dx), fill= blue, width=3.0) x = x + dx x = -1 print( "fact(", n, ")=", fact(n)) print( "expn(", n, ",", x, ")=", expn(n, x)) if n >= 0: x = -5 dx = 0.1 while x <= 5: canvas.create_line(250+50*x, 200-50*expN(n, x), 250+50*(x+dx), 200-50*expN(n, x+dx), fill= red, width=3.0) x = x + dx N = N+1 root = Tk() button = Button(root, text= NEXT, command=paint) 42

canvas = Canvas(root, width=500, height=400) canvas.pack() button.pack() root.mainloop() NEXT >>> ================================ RESTART ================================ >>> fact( -1 )= 1.0 expn( -1, -1 )= 0.0 fact( 0 )= 1.0 expn( 0, -1 )= 0.0 fact( 1 )= 1.0 expn( 1, -1 )= 1.0 fact( 2 )= 1.0 expn( 2, -1 )= 0.0 fact( 3 )= 2.0 expn( 3, -1 )= 1.0 fact( 4 )= 6.0 expn( 4, -1 )= 0.5 fact( 5 )= 24.0 expn( 5, -1 )= 0.666666666667 43

fact( 6 )= 120.0 expn( 6, -1 )= 0.625 >>> fact(n) def fact(n): r = 1.0 for i in range(1, n): r = r * i return r range(n) range(n) def fact(n): r = 1.0 for i in range(1, n+1): r = r * i return r y = exp(x) x 44

>>> ================================ RESTART ================================ >>> fact( -1 )= 1.0 expn( -1, -1 )= 0.0 fact( 0 )= 1.0 expn( 0, -1 )= 0.0 fact( 1 )= 1.0 expn( 1, -1 )= 1.0 fact( 2 )= 2.0 expn( 2, -1 )= 0.0 fact( 3 )= 6.0 expn( 3, -1 )= 0.5 fact( 4 )= 24.0 expn( 4, -1 )= 0.333333333333 fact( 5 )= 120.0 expn( 5, -1 )= 0.375 fact( 6 )= 720.0 expn( 6, -1 )= 0.366666666667 fact( 7 )= 5040.0 expn( 7, -1 )= 0.368055555556 fact( 8 )= 40320.0 expn( 8, -1 )= 0.367857142857 45

>>> expn(n, x) def expn(n, x): s = 0.0 for i in range(n): s = s + pow(x, i) / fact(i) return s def expn(n, x): s = 0.0 for i in range(n+1): s = s + pow(x, i) / fact(i) return s debug debugging) bug debug debugging cos4πx from tkinter import * from math import * def combi(n, k): r = 1.0 for i in range(k): r = r * (n-i)/(i+1) return r def g(n, k, x): r = combi(n,k) * x**k * (1-x)**(n-k) * cos(4*pi*float(k)/n) return r def f (n, x): r = 0.0 46

for k in range(n+1): r += g(n,k,x) return r def paint(): global N canvas.create_line(0, 200, 500, 200) canvas.create_line(50, 0, 50, 400) t = 0 dt = 1.0/400 while t < 1: canvas.create_line(50+400*t, 200-150*cos(4*pi*t), 50+400*(t+dt), 200-150*cos(4*pi*(t+dt)), fill= blue, width=2.0) t = t + dt t = 0 dt = 1.0/400 while t < 1: canvas.create_line(50+400*t, 200-150*f(N, t), 50+400*(t+dt), 200-150*f(N, t+dt)) t = t + dt N = N+10 root = Tk() N=2 button = Button(root, text= NEXT, command=paint) canvas = Canvas(root, width=500, height=400) canvas.pack() button.pack() root.mainloop() 47

f C[0, 1] n f n (x) = nc k x k (1 x) n k f( k n ) k=0 f n (x) f(x) x [0, 1] x = 0.a 1 a 2 n a n = 1 F (x) = 1 m n,a m 1 x [0, 1] F (0) = 0, F (1) = 1 from tkinter import * def tento3(i, N): r =[0]*N for k in range(n): return r def f(i, N): r[k] = i % 3 i = i / 3 series = tento3(i, N) t = 0.0 for k in range(n): 1 2 m 48

if series[n-k-1] == 1: t += (1.0/2)**(k+1) break elif series[n-k-1] == 2: t += (1.0/2)**(k+1) return t def paint(): global N canvas.create_rectangle(0, 0, 500, 400, fill= white ) canvas.create_line(0, 300, 500, 300) canvas.create_line(50, 0, 50, 400) t = 0 dt = 1.0 / (3**N) for i in range(3**n): canvas.create_line(50+400*t, 300-200*f(i, N), 50+400*(t+dt), 300-200*f(i, N)) t = t + dt N = N+1 root = Tk() N=1 button = Button(root, text= NEXT, command=paint) canvas = Canvas(root, width=500, height=400) canvas.pack() button.pack() root.mainloop() 49

y = sin(x) + sin(2x)/2 + sin(3x)/3 sin(x) cos(x) r = 3sin(3 Python Shell File New File Editor from tkinter import * root = Tk() canvas = Canvas(root, width = 350, height=350) canvas.pack() root.mainloop() 50

r = 3sin(3 x( ) = 3sin(3 )cos( ), y( ) = 3sin(3 )sin( ) from math import * def X(t) : return 3*sin(3*t)*cos(t) def Y(t) : return 3*sin(3*t)*sin(t) canvas canvas.create_line(0, 175, 350, 175) canvas.create_line(175, 0, 175, 350) for x in range(25, 350, 50): canvas.create_line(x, 170, x, 180) for y in range(25, 350, 50): canvas.create_line(170, y, 180, y) t = 0.0 dt = pi/80 while t < 2*pi: canvas.create_line(175+50*x(t), 175-50*Y(t), 175+50*X(t+dt), 175-50*Y(t+dt)) t = t + dt, from tkinter import * from math import * def X(t) : return 3*sin(3*t)*cos(t) def Y(t) : return 3*sin(3*t)*sin(t) root = Tk() canvas = Canvas(root, width = 350, height=350) canvas.create_line(0, 175, 350, 175) canvas.create_line(175, 0, 175, 350) for x in range(25, 350, 50): canvas.create_line(x, 170, x, 180) for y in range(25, 350, 50): canvas.create_line(170, y, 180, y) t = 0.0 dt = pi/80 while t < 2*pi: canvas.create_line(175+50*x(t), 175-50*Y(t), 175+50*X(t+dt), 175-50*Y(t+dt)) t = t + dt canvas.pack() root.mainloop() 51

(1/2, 0) OP Python Shell File New File Editor 52

from tkinter import * from math import * root = Tk() canvas = Canvas(root, width = 360, height=360) canvas.create_line(0, 180, 360, 180) canvas.create_line(180, 0, 180, 360) K = 150 t = 0.0 while t <= 2*pi : x = 0.25+cos(t)/4 y = sin(t)/4 x1 = K*(x-sqrt(0.5+cos(t)/2)/2)+180 y1 = K*(-y-sqrt(0.5+cos(t)/2)/2)+180 x2 = x1 + K*sqrt(0.5+cos(t)/2) y2 = y1 + K*sqrt(0.5+cos(t)/2) canvas.create_oval(x1, y1, x2, y2) t = t + pi/20 canvas.pack() root.mainloop() 53

Python and Pygame import pygame from math import * pygame.init() black = (0,0,0) white = (255, 255, 255) green = (0, 255, 0) red= (255, 0, 0) pi = 3.141592653 size = (700, 500) screen = pygame.display.set_mode(size) pi = atan(1.0)*4 done = False clock = pygame.time.clock() theta = pi K = 200 while done == False: for event in pygame.event.get(): if event.type == pygame.quit: done = True screen.fill(white) t = 0 54

while t <= 2*pi - 2*theta: x = 0.25 + cos(t)/4.0 y = sin(t)/4.0 x1 = (int)(k * ( x - sqrt(0.5+cos(t)/2.0)/2.0) + 350) y1 = (int)(k * (-y - sqrt(0.5+cos(t)/2.0)/2.0) + 250) x2 = (int)(k * sqrt(0.5+cos(t)/2.0)) y2 = (int)(k * sqrt(0.5+cos(t)/2.0)) if x2 == 0 or y2 == 0: t += pi/20 continue pygame.draw.ellipse(screen, green, [x1, y1, x2, y2],2) t += pi/20 theta -= pi/20 pygame.display.flip() if theta < 0: theta = pi clock.tick(5) pygame.quit() a O b C O C P P O x A C OC θ Q P (x,y) P θ x = (a + b) cos θ b cos a + b θ, y = (a + b) sin θ b sin a + b θ b b Python Shell 55

File New File Editor from tkinter import * root = Tk() 56

f0 = Frame(root) f1 = Frame(root) canvas = Canvas(f0, width = 360, height=360) canvas.pack() button = Button(f1, text= Hyouji ).pack(side = LEFT) l1 = Label(f1, text= a ).pack(side = LEFT) t1 = Entry(f1).pack(side = LEFT) l2 = Label(f1, text= b ).pack(side = LEFT) t2 = Entry(f1).pack(side = LEFT) f0.pack() f1.pack() root.mainloop() canvas a b Entry 2 Button command button = Button(f1, text= Hyouji ).pack(side = LEFT) button = Button(f1, text= Hyouji, command=paint).pack(side = LEFT) Entry Entry from tkinter import * root = Tk() f0 = Frame(root) 57

f1 = Frame(root) canvas = Canvas(f0, width = 360, height=360) canvas.pack() button = Button(f1, text= Hyouji, command=paint).pack(side = LEFT) l1 = Label(f1, text= a ).pack(side = LEFT) content1 = StringVar() t1 = Entry(f1, textvariable=content1).pack(side = LEFT) l2 = Label(f1, text= b ).pack(side = LEFT) content2 = StringVar() t2 = Entry(f1, textvariable=content2).pack(side = LEFT) f0.pack() f1.pack() root.mainloop() paint def paint(): a = int(content1.get()) b = int(content2.get()) print( "a=", a, "b=", b, "a+b=", a+b ) from tkinter import * def paint(): a = int(content1.get()) b = int(content2.get()) print( "a=", a, "b=", b, "a+b=", a+b ) root = Tk() f0 = Frame(root) f1 = Frame(root) canvas = Canvas(f0, width = 360, height=360) canvas.pack() button = Button(f1, text= Hyouji, command=paint).pack(side = LEFT) l1 = Label(f1, text= a ).pack(side = LEFT) content1 = StringVar() t1 = Entry(f1, textvariable=content1).pack(side = LEFT) l2 = Label(f1, text= b ).pack(side = LEFT) content2 = StringVar() t2 = Entry(f1, textvariable=content2).pack(side = LEFT) f0.pack() f1.pack() root.mainloop() 58

Entry1 2 Entry2 3 Hyouji >>> ================================ RESTART ================================ >>> a= 2 b= 3 a+b= 5 >>> Entry1 2.5 Entry2 3.8 paint def paint(): a = float(content1.get()) b = float(content2.get()) print( "a=", a, "b=", b, "a+b=", a+b ) Entry1 2.5 Entry2 3.8 Hyouji 59

>>> ================================ RESTART ================================ >>> a= 2.5 b= 3.8 a+b= 6.3 >>> from math import * from tkinter import * from math import * def paint(): a = float(content1.get()) b = float(content2.get()) print "a=", a, "b=", b, "a+b=", a+b root = Tk() f0 = Frame(root) f1 = Frame(root) canvas = Canvas(f0, width = 360, height=360) canvas.pack() button = Button(f1, text= Hyouji, command=paint).pack(side = LEFT) l1 = Label(f1, text= a ).pack(side = LEFT) content1 = StringVar() t1 = Entry(f1, textvariable=content1).pack(side = LEFT) 60

l2 = Label(f1, text= b ).pack(side = LEFT) content2 = StringVar() t2 = Entry(f1, textvariable=content2).pack(side = LEFT) f0.pack() f1.pack() root.mainloop() a b paint def paint(): a = float(content1.get()) b = float(content2.get()) K = 170/(a+2*b) canvas.create_rectangle(0, 0, 360, 360, fill= white ) canvas.create_oval(180-k*a, 180-K*a, 180+K*a, 180+K*a, fill= red, width=3.0) t = 0.0 dt = pi/80 while t <= 2*pi*b : x1 = 180+K*((a+b)*cos(t)-b*cos((a+b)*t/b)) y1 = 180-K*((a+b)*sin(t)-b*sin((a+b)*t/b)) x2 = 180+K*((a+b)*cos(t+dt)-b*cos((a+b)*(t+dt)/b)) y2 = 180-K*((a+b)*sin(t+dt)-b*sin((a+b)*(t+dt)/b)) canvas.create_line(x1, y1, x2, y2, fill= blue, width=3.0) t = t + dt from tkinter import * from math import * def paint(): a = float(content1.get()) b = float(content2.get()) K = 170/(a+2*b) canvas.create_rectangle(0, 0, 360, 360, fill= white ) canvas.create_oval(180-k*a, 180-K*a, 180+K*a, 180+K*a, fill= red, width=3.0) t = 0.0 dt = pi/80 while t <= 2*pi*b : x1 = 180+K*((a+b)*cos(t)-b*cos((a+b)*t/b)) y1 = 180-K*((a+b)*sin(t)-b*sin((a+b)*t/b)) x2 = 180+K*((a+b)*cos(t+dt)-b*cos((a+b)*(t+dt)/b)) y2 = 180-K*((a+b)*sin(t+dt)-b*sin((a+b)*(t+dt)/b)) canvas.create_line(x1, y1, x2, y2, fill= blue, width=3.0) 61

t = t + dt root = Tk() f0 = Frame(root) f1 = Frame(root) canvas = Canvas(f0, width = 360, height=360) canvas.pack() button = Button(f1, text= Hyouji, command=paint).pack(side = LEFT) l1 = Label(f1, text= a ).pack(side = LEFT) content1 = StringVar() t1 = Entry(f1, textvariable=content1).pack(side = LEFT) l2 = Label(f1, text= b ).pack(side = LEFT) content2 = StringVar() t2 = Entry(f1, textvariable=content2).pack(side = LEFT) f0.pack() f1.pack() root.mainloop() a = 5, b = 3 a = 1, b = 1 62

a = 5.5, b = 2.5 a = 11. b = 5 63

import pygame from math import * pygame.init() black = (0,0,0) white = (255, 255, 255) green = (0, 255, 0) red= (255, 0, 0) blue = (0, 0, 255) pi = 3.141592653 size = (700, 500) screen = pygame.display.set_mode(size) pi = atan(1.0)*4 done = False clock = pygame.time.clock() theta = 0 K = 20 a = 5 b = 2 while done == False: 64

for event in pygame.event.get(): if event.type == pygame.quit: done = True screen.fill(white) pygame.draw.ellipse(screen, green, [350-K*a, 250-K*a, 2*K*a, 2*K*a]) x = (int)(k*((a+b)*cos(theta)-b)) y = (int)(k*((a+b)*sin(theta)+b)) pygame.draw.ellipse(screen, blue,[350+x,250-y, 2*K*b, 2*K*b]) x1 = (int)(350+k*((a+b)*cos(theta)-b*cos((a+b)*theta/b))) y1 = (int)(250-k*((a+b)*sin(theta)-b*sin((a+b)*theta/b))) x2 = (int)(350+k*((a+b)*cos(theta)+b*cos((a+b)*theta/b))) y2 = (int)(250-k*((a+b)*sin(theta)+b*sin((a+b)*theta/b))) pygame.draw.line(screen, black, [x1, y1], [x2, y2], 2) x1 = (int)(350+k*((a+b)*cos(theta)-b*cos((a+b)*theta/b+pi/2))) y1 = (int)(250-k*((a+b)*sin(theta)-b*sin((a+b)*theta/b+pi/2))) x2 = (int)(350+k*((a+b)*cos(theta)+b*cos((a+b)*theta/b+pi/2))) y2 = (int)(250-k*((a+b)*sin(theta)+b*sin((a+b)*theta/b+pi/2))) pygame.draw.line(screen, black, [x1, y1], [x2, y2], 2) t = 0 ox = (int)(350+k*((a+b)*cos(t)-b*cos((a+b)*t/b))) oy = (int)(250-k*((a+b)*sin(t)-b*sin((a+b)*t/b))) while t <= theta: nx = (int)(350+k*((a+b)*cos(t)-b*cos((a+b)*t/b))) ny = (int)(250-k*((a+b)*sin(t)-b*sin((a+b)*t/b))) pygame.draw.line(screen, red, [ox, oy], [nx, ny], 2) ox = nx oy = ny t += pi/100 theta += pi/25 if theta >= 2*pi*b: theta = 0 pygame.display.flip() clock.tick(5) pygame.quit() 65

a O b a > b > 0) C O C P P O x A C OC θ Q P (x,y) P θ x = (a b) cos θ + b cos a b θ, y = (a b) sin θ b sin a b θ b b r = a + b cos θ a = b r = a(1 + cos θ) 66

x = (a + b) cos θ c cos a + b b θ, y = (a + b) sin θ c sin a + b θ b x = (a b) cos θ + c cos a b b θ, y = (a b) sin θ c sin a b θ b 67

t x = A cos(at), y = B sin(bt + δ) t x = a(cos(t) + t sin(t)), y = a(sin(t) t cos(t)) : z 0 = 0, z n+1 = zn 2 + c z n n c z n K n Turtle Graphics from turtle import * import time def mb(c, K, LOOP): z = 0.0 + 0.0*1j n = 0 while (abs(z) < K and n < LOOP): z = z**2 + c n = n +1 return n 68

def plot(x, y, n, LOOP): s = hex(255-n) s = s[2:] if len(s) == 1: s = 0 + s cl = # + s + s + s pencolor(cl) pu() setpos(100*x, 100*y) pd() setpos(100*x+1, 100*y+1) dx, dy = 0.01, 0.01 xmin, xmax = -1.8, 0.6 ymin, ymax = -1.0, 1.0 K = 2.0 LOOP = 255 ht() start_time = time.time() x = xmin while x < xmax: y = ymin while y < ymax: c = x + y*1j n = mb(c, K, LOOP) plot(x, y, n, LOOP) y += dy x += dx end_time = time.time() print "time = %f" % (end_time-start_time) pylab Python pylab install pylab import time import pylab def mb(x,y): c = complex(x, y) 69

z = complex(0.0, 0.0) n = 0 LOOP = 255 while (abs(z) < 3 and n < LOOP): z = z**2 + c n = n +1 return n start_time = time.time() dx, dy = 0.01, 0.01 xmin, xmax = -1.8, 0.6 ymin, ymax = -1.0, 1.0 x = pylab.arange(xmin, xmax, dy) y = pylab.arange(ymin, ymax, dx) l = [] for i in range(0,len(y)): for j in range(0,len(x)): l.append(mb(x[j],y[i])) pylab.hist(l, bins = 30) pylab.show() 70

from tkinter import * import math import time root = Tk() canvas = Canvas(root, width = 500, height=400) def mb(c, K, LOOP): z = 0.0 + 0.0*1j n = 0 while (abs(z) < K and n < LOOP): z = z**2 + c n = n +1 return n def plot(x, y, n): gx = 200*x + 370 gy = -180*y + 200 if n <= 3: canvas.create_line(gx, gy, gx+1, gy+1, fill = yellow ) elif n <= 4: canvas.create_line(gx, gy, gx+1, gy+1, fill = orange ) elif n <= 5: canvas.create_line(gx, gy, gx+1, gy+1, fill = dark green ) elif n <= 6: canvas.create_line(gx, gy, gx+1, gy+1, fill = cyan ) elif n <= 7: canvas.create_line(gx, gy, gx+1, gy+1, fill = dark gray ) elif n <= 8: canvas.create_line(gx, gy, gx+1, gy+1, fill = green ) elif n < 9: canvas.create_line(gx, gy, gx+1, gy+1, fill = chocolate ) elif n < 10: canvas.create_line(gx, gy, gx+1, gy+1, fill = coral ) elif n < 12: canvas.create_line(gx, gy, gx+1, gy+1, fill = blue ) elif n < 14: canvas.create_line(gx, gy, gx+1, gy+1, fill = red ) elif n < 16: canvas.create_line(gx, gy, gx+1, gy+1, fill = pink ) elif n < 18: canvas.create_line(gx, gy, gx+1, gy+1, fill = sky blue ) elif n < 20: 71

canvas.create_line(gx, gy, gx+1, gy+1, fill = gray ) elif n < 25: canvas.create_line(gx, gy, gx+1, gy+1, fill = gold ) elif n < 30: canvas.create_line(gx, gy, gx+1, gy+1, fill = light yellow ) elif n < 250: canvas.create_line(gx, gy, gx+1, gy+1, fill = white ) else: canvas.create_line(gx, gy, gx+1, gy+1, fill = black ) dx, dy = 0.005, 0.005 xmin, xmax = -1.8, 0.6 ymin, ymax = -1.0, 1.0 K = 3.0 LOOP = 255 start_time = time.time() x = xmin while x < xmax: y = ymin while y < ymax: c = x + y*1j n = mb(c, K, LOOP) plot(x, y, n) y += dy x += dx end_time = time.time() print( "time = %f" % (end_time-start_time) ) canvas.pack() root.mainloop() 72

n #gx = 200*x + 370 #gy = -180*y + 200 # from tkinter import * import math import time root = Tk() canvas = Canvas(root, width = 480, height=400) sx, sy = 0.0, 0.0 def setsource(event): global sx, sy 73

sx = (xmax-xmin)/480.0*event.x + xmin sy = ymax - (ymax-ymin)/400.0*event.y canvas.bind("<button-1>", setsource) tx, ty = 0.0, 0.0 def drawline(event): global sx, sy, tx, ty tx = (xmax-xmin)/480.0*event.x + xmin ty = ymax - (ymax-ymin)/400.0*event.y gsx = 480/(xmax-xmin)*(sx-xmin) gsy = 400/(ymax-ymin)*(ymax-sy) gtx = 480/(xmax-xmin)*(tx-xmin) gty = 400/(ymax-ymin)*(ymax-ty) canvas.create_rectangle(gsx, gsy, gtx, gty) canvas.bind("<b1-motion>", drawline) def redraw(event): global sx, sy, tx, ty global xmax, xmin, ymax, ymin global dx, dy tx = (xmax-xmin)/480.0*event.x + xmin ty = ymax - (ymax-ymin)/400.0*event.y if tx < sx: sx, tx = tx, sx if ty < sy: sy, ty = ty, sy xmin, xmax = sx, tx ymin, ymax = sy, ty dx = (xmax-xmin)/480.0 dy = (ymax-ymin)/400.0 x = xmin while x < xmax: y = ymin while y < ymax: c = x + y*1j n = mb(c, K, LOOP) plot(x, y, n, LOOP) y += dy x += dx canvas.bind("<buttonrelease-1>", redraw) def mb(c, K, LOOP): z = 0.0 + 0.0*1j n = 0 74

while (abs(z) < K and n < LOOP): z = z**2 + c n = n +1 return n def plot(x, y, n, LOOP): #gx = 200*x + 370 #gy = -180*y + 200 gx = 480/(xmax-xmin)*(x-xmin) gy = 400/(ymax-ymin)*(ymax-y) if n == LOOP: canvas.create_line(gx, gy, gx+1, gy+1, fill = black ) elif n % 8 == 0: canvas.create_line(gx, gy, gx+1, gy+1, fill = yellow ) elif n % 8 == 1: canvas.create_line(gx, gy, gx+1, gy+1, fill = orange ) elif n % 8 == 2: canvas.create_line(gx, gy, gx+1, gy+1, fill = cyan ) elif n % 8 == 3: canvas.create_line(gx, gy, gx+1, gy+1, fill = white ) elif n % 8 == 4: canvas.create_line(gx, gy, gx+1, gy+1, fill = green ) elif n % 8 == 5: canvas.create_line(gx, gy, gx+1, gy+1, fill = coral ) elif n % 8 == 6: canvas.create_line(gx, gy, gx+1, gy+1, fill = blue ) elif n % 8 == 7: canvas.create_line(gx, gy, gx+1, gy+1, fill = red ) dx, dy = 0.005, 0.005 xmin, xmax = -1.8, 0.6 ymin, ymax = -1.0, 1.0 K = 3.0 LOOP = 255 #start_time = time.time() x = xmin while x < xmax: y = ymin while y < ymax: c = x + y*1j n = mb(c, K, LOOP) plot(x, y, n, LOOP) y += dy x += dx 75

#end_time = time.time() #print( "time = %f" % (end_time-start_time) ) canvas.pack() root.mainloop() 76

LOOP 77

Python3.6 Python2.7 from tkinter import * from Tkinter import * sx, sy = 0.0, 0.0 def setsource(event): global sx, sy sx = (xmax-xmin)/480.0*event.x + xmin sy = ymax - (ymax-ymin)/400.0*event.y canvas.bind("<button-1>", setsource) global sx sy tx, ty = 0.0, 0.0 def drawline(event): global sx, sy, tx, ty tx = (xmax-xmin)/480.0*event.x + xmin ty = ymax - (ymax-ymin)/400.0*event.y gsx = 480/(xmax-xmin)*(sx-xmin) gsy = 400/(ymax-ymin)*(ymax-sy) gtx = 480/(xmax-xmin)*(tx-xmin) gty = 400/(ymax-ymin)*(ymax-ty) canvas.create_rectangle(gsx, gsy, gtx, gty) canvas.bind("<b1-motion>", drawline) global tx ty sx, sy tx, ty def redraw(event): global sx, sy, tx, ty global xmax, xmin, ymax, ymin global dx, dy tx = (xmax-xmin)/480.0*event.x + xmin ty = ymax - (ymax-ymin)/400.0*event.y if tx < sx: sx, tx = tx, sx if ty < sy: sy, ty = ty, sy 78

xmin, xmax = sx, tx ymin, ymax = sy, ty dx = (xmax-xmin)/480.0 dy = (ymax-ymin)/400.0 x = xmin while x < xmax: y = ymin while y < ymax: c = x + y*1j n = mb(c, K, LOOP) plot(x, y, n, LOOP) y += dy x += dx canvas.bind("<buttonrelease-1>", redraw) dx dy C++ Python Python Python C++ f(z) = z 2 + C C = a + bi z n+1 = f(z n ) z n K n from tkinter import * import math root = Tk() f0 = Frame(root) f1 = Frame(root) canvas = Canvas(f0, width = 400, height=400) a = -0.3 b = -0.63 K = 3.0 LOOP = 255 C = complex(0.0, 0.0) xmin, xmax = -2.0, 2.0 79

ymin, ymax = -2.0, 2.0 def paint(): global C, xmin, xmax, ymin, ymax a = float(content1.get()) b = float(content2.get()) C = complex(a, b) dx, dy = 0.01, 0.01 xmin, xmax = -2.0, 2.0 ymin, ymax = -2.0, 2.0 x = xmin while x < xmax: y = ymin while y < ymax: z = complex(x, y) n = julia(z, K, LOOP) plot(x, y, n, LOOP) y += dy x += dx button = Button(f1, text= PAINT, command=paint).pack(side=left) l1 = Label(f1, text= a ).pack(side=left) content1 = StringVar() t1 = Entry(f1, textvariable=content1).pack(side=left) l2 = Label(f1, text= b ).pack(side=left) content2 = StringVar() t2 = Entry(f1, textvariable=content2).pack(side=left) f0.pack() f1.pack() sx, sy = 0.0, 0.0 def setsource(event): global sx, sy sx = (xmax-xmin)/400.0*event.x + xmin sy = ymax - (ymax-ymin)/400.0*event.y canvas.bind("<button-1>", setsource) tx, ty = 0.0, 0.0 def drawline(event): global sx, sy, tx, ty tx = (xmax-xmin)/400.0*event.x + xmin ty = ymax - (ymax-ymin)/400.0*event.y gsx = 400/(xmax-xmin)*(sx-xmin) gsy = 400/(ymax-ymin)*(ymax-sy) 80

gtx = 400/(xmax-xmin)*(tx-xmin) gty = 400/(ymax-ymin)*(ymax-ty) canvas.create_rectangle(gsx, gsy, gtx, gty) canvas.bind("<b1-motion>", drawline) def redraw(event): global sx, sy, tx, ty global xmax, xmin, ymax, ymin global dx, dy tx = (xmax-xmin)/400.0*event.x + xmin ty = ymax - (ymax-ymin)/400.0*event.y if tx < sx: sx, tx = tx, sx if ty < sy: sy, ty = ty, sy xmin, xmax = sx, tx ymin, ymax = sy, ty dx = (xmax-xmin)/400.0 dy = (ymax-ymin)/400.0 x = xmin while x < xmax: y = ymin while y < ymax: z = complex(x, y) n = julia(z, K, LOOP) plot(x, y, n, LOOP) y += dy x += dx canvas.bind("<buttonrelease-1>", redraw) def julia(z, K, LOOP): global C n = 0 while (abs(z) < K and n < LOOP): z = z**2 + C n = n +1 return n def plot(x, y, n, LOOP): gx = 400/(xmax-xmin)*(x-xmin) gy = 400/(ymax-ymin)*(ymax-y) if n == LOOP: canvas.create_line(gx, gy, gx+1, gy+1, fill = black ) elif n % 8 == 0: 81

canvas.create_line(gx, gy, gx+1, gy+1, fill = yellow ) elif n % 8 == 1: canvas.create_line(gx, gy, gx+1, gy+1, fill = orange ) elif n % 8 == 2: canvas.create_line(gx, gy, gx+1, gy+1, fill = cyan ) elif n % 8 == 3: canvas.create_line(gx, gy, gx+1, gy+1, fill = white ) elif n % 8 == 4: canvas.create_line(gx, gy, gx+1, gy+1, fill = green ) elif n % 8 == 5: canvas.create_line(gx, gy, gx+1, gy+1, fill = coral ) elif n % 8 == 6: canvas.create_line(gx, gy, gx+1, gy+1, fill = blue ) elif n % 8 == 7: canvas.create_line(gx, gy, gx+1, gy+1, fill = red ) content1.set( %.3f % (0.318)) content2.set( %.3f % (0.043)) canvas.pack() root.mainloop() content1.set( %.3f % (0.318)) content2.set( %.3f % (0.043)) 82

PAINT PAINT 83

Python3.6 Python2.7 84

f(z) = z 2 + C dy = f(x, y) dx { xi+1 = x i + x y i+1 = y i + f(x i, y i ) x (x 0, y 0 ) (x 1, y 1 ), (x 2, y 2 ), (x 3, y 3 ), from tkinter import * from math import * def f(x,y): return cos(x)-y*sin(x) root = Tk() canvas = Canvas(root, width = 360, height=360) canvas.pack() sy = -10 for sy in range(-10,10): x0 = 0 y0 = sy deltax = 0.1 while 10*x0 < 360: x = x0 + deltax y = y0 + f(x0, y0)*deltax canvas.create_line(180+10*x0, 180-10*y0,180+10*x, 180-10*y) x0 = x y0 = y x0 = 0 y0 = sy while 10*x0 > -360: x = x0 - deltax y = y0 - f(x0, y0)*deltax canvas.create_line(180+10*x0, 180-10*y0,180+10*x, 180-10*y) x0 = x y0 = y root.mainloop() 85

(0, 10), (0, 9), (0, 8),, (0, 9) dy = cos(x) y sin(x) dx f(x, y) Python 2.7 from Tkinter import * tkinter x i+1 = x i + x dy = f(x, y) dx y i+1 = y i + x 6 (k 1 + 2k 2 + 2k 3 + k 4 ) k 1 = f(x i, y i ) k 2 = f(x i + x 2, y i + x 2 k 1) k 3 = f(x i + x 2, y i + x 2 k 2) k 4 = f(x i + x, y i + xk 3 ) 86

(x 0, y 0 ) (x 1, y 1 ), (x 2, y 2 ), (x 3, y 3 ), from tkinter import * from math import * def f(x,y): return cos(x)-y*sin(x) root = Tk() canvas = Canvas(root, width = 360, height=360) canvas.pack() sy = -10 for sy in range(-10,10): x0 = 0 y0 = sy deltax = 0.1 while 10*x0 < 360: x = x0 + deltax k1 = f(x0, y0) k2 = f(x0+deltax/2, y0+deltax*k1/2) k3 = f(x0+deltax/2, y0+deltax*k2/2) k4 = f(x0+deltax, y0+deltax*k3) y = y0 + (k1+2*k2+2*k3+k4)*deltax/6 canvas.create_line(180+10*x0, 180-10*y0,180+10*x, 180-10*y) x0 = x y0 = y x0 = 0 y0 = sy while 10*x0 > -360: x = x0 - deltax k1 = f(x0, y0) k2 = f(x0-deltax/2, y0-deltax*k1/2) k3 = f(x0-deltax/2, y0-deltax*k2/2) k4 = f(x0-deltax, y0-deltax*k3) y = y0 - (k1+2*k2+2*k3+k4)*deltax/6 canvas.create_line(180+10*x0, 180-10*y0,180+10*x, 180-10*y) x0 = x y0 = y root.mainloop() 87

d 2 y dt 2 = y from tkinter import * from math import * root = Tk() canvas = Canvas(root, width = 360, height=360) canvas.pack() t0 = 0 u0 = 0 y0 = 10 delta_t = 0.1 while 10*t0 < 360: t = t0 + delta_t u = u0 - y0 * delta_t y = y0 + u0 * delta_t canvas.create_line(180+10*t0, 180-10*y0,180+10*t, 180-10*y) t0 = t u0 = u y0 = y t0 = 0 88

u0 = 0 y0 = 10 while 10*t0 > -360: t = t0 - delta_t u = u0 + y0 * delta_t y = y0 - u0 * delta_t canvas.create_line(180+10*t0, 180-10*y0,180+10*t, 180-10*y) t0 = t u0 = u y0 = y root.mainloop() (t 0 = 0, u 0 = 0, y 0 = 10) y = A cos(t) + B sin(t) from tkinter import * from math import * root = Tk() canvas = Canvas(root, width = 360, height=360) canvas.pack() t0 = 0 89

u0 = 0 y0 = 10 delta_t = 0.1 while 10*t0 < 360: k1 = u0*delta_t l1 = -y0*delta_t k2 = (u0+l1/2.0)*delta_t l2 = -(y0+k1/2.0)*delta_t k3 = (u0+l2/2.0)*delta_t l3 = -(y0+k2/2.0)*delta_t k4 = (u0+l3)*delta_t l4 = -(y0+k3)*delta_t k = (k1+2.0*(k2+k3)+k4)/6.0 l = (l1+2.0*(l2+l3)+l4)/6.0 t = t0 + delta_t u = u0 + l y = y0 + k canvas.create_line(180+10*t0, 180-10*y0,180+10*t, 180-10*y) t0 = t u0 = u y0 = y t0 = 0 u0 = 0 y0 = 10 delta_t = 0.1 while 10*t0 > -360: k1 = -u0*delta_t l1 = y0*delta_t k2 = -(u0+l1/2.0)*delta_t l2 = (y0+k1/2.0)*delta_t k3 = -(u0+l2/2.0)*delta_t l3 = (y0+k2/2.0)*delta_t k4 = -(u0+l3)*delta_t l4 = (y0+k3)*delta_t k = (k1+2.0*(k2+k3)+k4)/6.0 l = (l1+2.0*(l2+l3)+l4)/6.0 t = t0 - delta_t u = u0 + l y = y0 + k canvas.create_line(180+10*t0, 180-10*y0,180+10*t, 180-10*y) t0 = t u0 = u y0 = y 90

root.mainloop() tkinter pylab MIT John V. Guttag Introduction to Computation and Programming Using Python Python pylab Python Python 91