Skip to content

Instantly share code, notes, and snippets.

@cra
Created September 3, 2021 21:27
Show Gist options
  • Select an option

  • Save cra/4706c6e07abcecbc84a5ee12ef9edd04 to your computer and use it in GitHub Desktop.

Select an option

Save cra/4706c6e07abcecbc84a5ee12ef9edd04 to your computer and use it in GitHub Desktop.
import random
from math import exp, factorial
import py5
no_noise = True
noise_range = 0.25
show_fit = False
show_diff = False
num_terms = 1
SQUARE_SIZE = 10
xmin = -SQUARE_SIZE
xmax = SQUARE_SIZE
ymin = -SQUARE_SIZE
ymax = SQUARE_SIZE
rangex = xmax - xmin
rangey = ymax - ymin
#####
def noise():
if no_noise:
return 0
return random.uniform(-noise_range, noise_range)
# return random.gauss(0, 1)
def f(x):
return noise() + exp(x)
def g(x):
s = 1
for i in range(1, num_terms + 1):
s += x**i / factorial(i)
return noise() + s
def h(x):
return f(x) - g(x)
#####
def settings():
py5.size(900, 900)
def setup():
global xscl, yscl
xscl = py5.width/rangex
yscl = -py5.height/rangey
py5.no_fill()
def draw():
global xscl, yscl
py5.text_size(32)
py5.background(255)
py5.translate(py5.width/2, py5.height/2)
grid(xscl, yscl)
py5.stroke(255, 0, 0)
graph_function(f, xscl, yscl)
if show_fit:
py5.stroke(0, 0, 255)
graph_function(g, xscl, yscl)
py5.text(
f"разложение до степени {num_terms:d}",
xscl * 1,
yscl * -5.5,
)
py5.fill(0, 102, 153)
if show_diff:
py5.stroke(0, 255, 0)
graph_function(h, xscl, yscl)
if not no_noise:
amp = f"{abs(noise_range):.2f}"
msg = f"шум -{amp} +{amp}"
py5.text(msg, xscl * -6, yscl * 6)
py5.fill(0, 102, 153)
def grid(xscl, yscl):
py5.stroke_weight(1)
py5.stroke(200, 200, 200)
for i in range(xmin, xmax+1):
x = i * xscl
py5.line(x, ymin*yscl, x, ymax*yscl)
for i in range(ymin, ymax+1):
y = i * yscl
py5.line(xmin*xscl, y, xmax*xscl, y)
py5.stroke(0)
py5.stroke_weight(2)
py5.line(0, ymin*yscl, 0, ymax*yscl)
py5.line(xmin*xscl, 0, xmax*xscl, 0)
def graph_function(func, xscl, yscl, dx=0.1):
x = xmin
py5.stroke_weight(4)
while x <= xmax:
x0 = x * xscl
y0 = func(x) * yscl
x1 = (x + dx) * xscl
y1 = func(x + dx) * yscl
py5.line(x0, y0, x1, y1)
x += dx
def key_pressed():
global num_terms, no_noise, noise_range, show_fit, show_diff
dx = 0.05
if py5.key == py5.CODED:
if py5.key_code == py5.UP:
num_terms += 1
if py5.key_code == py5.DOWN:
if num_terms == 1:
return
num_terms -= 1
if py5.key in ['n', 'N']:
no_noise = not no_noise
if py5.key in ['h', 'H']:
noise_range += 0.05
if py5.key in ['l', 'L']:
noise_range -= 0.05
if py5.key in ['d', 'D']:
show_diff = not show_diff
if py5.key in ['f', 'F']:
show_fit = not show_fit
if __name__ == '__main__':
py5.run_sketch()
import random
from math import sin, pi, factorial
import py5
no_noise = True
noise_range = 0.25
show_fit = False
show_diff = False
show_pi = False
num_terms = 1
SQUARE_SIZE = 10
xmin = -SQUARE_SIZE
xmax = SQUARE_SIZE
ymin = -SQUARE_SIZE
ymax = SQUARE_SIZE
rangex = xmax - xmin
rangey = ymax - ymin
#####
def noise():
if no_noise:
return 0
return random.uniform(-noise_range, noise_range)
# return random.gauss(0, 1)
def f(x):
return noise() + sin(x)
def g(x):
s = 0
for i in range(1, num_terms + 1):
s += (-1)**(i+1) * x**(2*i - 1) / factorial(2*i - 1)
return noise() + s
def h(x):
return f(x) - g(x)
#####
def settings():
py5.size(800, 800)
def setup():
global xscl, yscl
xscl = py5.width/rangex
yscl = -py5.height/rangey
py5.no_fill()
def draw():
global xscl, yscl
py5.background(255)
py5.translate(py5.width/2, py5.height/2)
grid(xscl, yscl)
py5.stroke(255, 0, 0)
graph_function(f, xscl, yscl)
py5.text_size(24)
if show_fit:
py5.stroke(0, 0, 255)
graph_function(g, xscl, yscl)
level = 2*num_terms - 1
py5.text(f"разложение до степени {level:d}", xscl * 1, yscl * -5)
py5.fill(0, 102, 153)
if show_diff:
py5.stroke(0, 255, 0)
graph_function(h, xscl, yscl)
if show_pi:
py5.stroke(255, 0, 255)
py5.stroke_weight(3)
x = pi * xscl
py5.line(-x, ymin*yscl, -x, ymax*yscl)
py5.line(+x, ymin*yscl, +x, ymax*yscl)
py5.text(f"+3.1415926", x + 0.05, yscl * 4)
py5.fill(0, 102, 153)
py5.text(f"-3.1415926", -x - 0.05, yscl * 4)
if not no_noise:
amp = f"{abs(noise_range):.2f}"
py5.text(f"шум -{amp} +{amp}", xscl * -6, yscl * 5.5)
py5.fill(0, 102, 153)
def grid(xscl, yscl):
py5.stroke_weight(1)
py5.stroke(200, 200, 200)
for i in range(xmin, xmax+1):
x = i * xscl
py5.line(x, ymin*yscl, x, ymax*yscl)
for i in range(ymin, ymax+1):
y = i * yscl
py5.line(xmin*xscl, y, xmax*xscl, y)
py5.stroke(0)
py5.stroke_weight(2)
py5.line(0, ymin*yscl, 0, ymax*yscl)
py5.line(xmin*xscl, 0, xmax*xscl, 0)
def graph_function(func, xscl, yscl, dx=0.1):
x = xmin
py5.stroke_weight(4)
while x <= xmax:
x0 = x * xscl
y0 = func(x) * yscl
x1 = (x + dx) * xscl
y1 = func(x + dx) * yscl
py5.line(x0, y0, x1, y1)
x += dx
def key_pressed():
global num_terms, no_noise, noise_range, show_diff, show_fit, show_pi
dx = 0.05
if py5.key == py5.CODED:
if py5.key_code == py5.UP:
num_terms += 1
if py5.key_code == py5.DOWN:
if num_terms == 1:
return
num_terms -= 1
if py5.key in ['n', 'N']:
no_noise = not no_noise
if py5.key in ['h', 'H']:
noise_range += 0.05
if py5.key in ['l', 'L']:
noise_range -= 0.05
if py5.key in ['d', 'D']:
show_diff = not show_diff
if py5.key in ['f', 'F']:
show_fit = not show_fit
if py5.key in ['p', 'P']:
show_pi = not show_pi
if __name__ == '__main__':
py5.run_sketch()
import random
from math import cos, sin, pi
import py5
add_noise = False
noise_range = 0.25
TWO_PI = 2 * pi
shift_x = 0 #pi / 6
xmin = -8
xmax = 8
ymin = -8
ymax = 8
rangex = xmax - xmin
rangey = ymax - ymin
k = 1
b = 0
draw_sum = False
draw_wave = False
do_move = False
#####
def noise():
if not add_noise:
return 0
return random.uniform(-noise_range, noise_range)
# return random.gauss(0, 1)
def f(x):
return noise() + k * x + b
def g(x):
#return noise() + sin(x + shift_x) #** 2
return noise() + (x + shift_x) ** 2
def h(x):
return f(x) + g(x)
#####
def settings():
py5.size(900, 900)
def setup():
global xscl, yscl
xscl = py5.width/rangex
yscl = -py5.height/rangey
py5.no_fill()
def draw():
global xscl, yscl, shift_x
py5.background(255)
py5.translate(py5.width/2, py5.height/2)
grid(xscl, yscl)
py5.stroke(255, 0, 0)
graph_function(f, xscl, yscl)
if draw_wave:
py5.stroke(0, 255, 0)
graph_function(g, xscl, yscl)
if draw_sum:
py5.stroke(0, 0, 255)
graph_function(h, xscl, yscl)
py5.text_size(32)
#py5.text(f"shift_x = {shift_x:.2f}", xscl * -5, yscl * 5)
py5.text(f"прямая {k:.2f}x + {b:.2f}", xscl * -5.5, yscl * 5.5)
py5.fill(0, 102, 153)
if add_noise:
danet = f"шум -{abs(noise_range):.2f} +{abs(noise_range):.2f}"
py5.text(danet, xscl * -5.5, yscl * 4.5)
py5.fill(0, 102, 153)
if do_move:
shift_x += 0.05
def grid(xscl, yscl):
py5.stroke_weight(1)
py5.stroke(200, 200, 200)
for i in range(xmin, xmax+1):
x = i * xscl
py5.line(x, ymin*yscl, x, ymax*yscl)
for i in range(ymin, ymax+1):
y = i * yscl
py5.line(xmin*xscl, y, xmax*xscl, y)
py5.stroke(0)
py5.stroke_weight(2)
py5.line(0, ymin*yscl, 0, ymax*yscl)
py5.line(xmin*xscl, 0, xmax*xscl, 0)
def graph_function(func, xscl, yscl, dx=0.1):
x = xmin
py5.stroke_weight(4)
while x <= xmax:
x0 = x * xscl
y0 = func(x) * yscl
x1 = (x + dx) * xscl
y1 = func(x + dx) * yscl
py5.line(x0, y0, x1, y1)
x += dx
def key_pressed():
global shift_x, add_noise, noise_range, k, b, draw_wave, draw_sum, do_move
dx = 0.05
if py5.key == py5.CODED:
if py5.key_code == py5.UP:
shift_x += 10 * dx
if py5.key_code == py5.DOWN:
shift_x -= 10 * dx
if py5.key_code == py5.RIGHT:
shift_x += dx
if py5.key_code == py5.LEFT:
shift_x -= dx
if py5.key in ['n', 'N']:
add_noise = not add_noise
if py5.key in ['h', 'H']:
noise_range += 0.05
if py5.key in ['l', 'L']:
noise_range -= 0.05
if py5.key in ['j', 'J']:
k -= 0.05
if py5.key in ['k', 'K']:
k += 0.05
if py5.key == 'b':
b -= 0.05
if py5.key == 'B':
b += 0.05
if py5.key in ['w', 'W']:
draw_wave = not draw_wave
if py5.key in ['s', 'S']:
draw_sum = not draw_sum
if py5.key in ['m', 'M', 't', 'T']:
do_move = not do_move
if __name__ == '__main__':
py5.run_sketch()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment