Last active
February 18, 2023 20:50
-
-
Save okurka12/2359d7e4ce3e3189aff796707e546579 to your computer and use it in GitHub Desktop.
skripticek co jsem udelal kdyz jsem si nebyl jisty jestli jsem slozil spravne funkce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# skripticek na overeni spravnosti slozeni funkci | |
# autor: vitek kvitek 🌻 | |
# datum: 18.02.2023 | |
# funguje v poho s: | |
# windows 10 | |
# python 3.10.2 | |
# numpy 1.22.3 | |
# matplotlib 3.7.0 | |
import matplotlib.pyplot as plt | |
import numpy as np | |
import math | |
if False: # TADY DAT TRUE JESTLI CHCES VIDET PRVNI PRIKLAD A FALSE JESTLI CHCES VIDET DRUHY | |
# definice funkce | |
def f(x): | |
if -2 <= x <= -1: # x + 2 pro x v <-2, -1> | |
return x + 2 | |
if -1 < x < 0: # x pro x v (-1, 0) | |
return x | |
if 0 <= x < 1: # 1 - x pro x v <0, 1) | |
return 1 - x | |
if 1 <= x <= 2: # -1 pro x v <1, 2> | |
return -1 | |
return 0 # 0 jinak | |
def f_po_f_moje(x): | |
if -2 < x < -1: | |
return - x - 1 | |
if x == 0: | |
return - 1 | |
if -1 <= x < 1: | |
return x | |
return 1 | |
def f_po_f(x): | |
return f(f(x)) | |
else: | |
def f(x): | |
if -1 <= x <= 1: | |
return -math.sqrt(abs(x)) | |
return abs(x) - 2 | |
def f_po_f_moje(x): | |
if -3 <= x <= -1 or 1 <= x <= 3: | |
return -math.sqrt(abs(abs(x) - 2)) | |
if -1 < x < 1: | |
return -math.sqrt(abs(-math.sqrt(abs(x)))) | |
return abs(abs(x) - 2) - 2 | |
def f_po_f(x): | |
return f(f(x)) | |
# tady funkce na vicenasobne zavolani pro zobrazeni vicero grafu | |
def plot(funkce, od=-3, do=3, n=10000, color="b"): | |
# n linearne rozprostrenych cisel (np.float64) v mezich od, do | |
xs = np.linspace(od, do, n) | |
# at tam jsou i integery a nebyl problem s porovnavanim v meznich hodnotach x | |
xs = np.append(xs, list(range(od, do + 1))) | |
# obrazy | |
ys = [funkce(x) for x in xs] | |
# bloat aby byly hezky osy uprostred | |
fig = plt.figure() | |
ax = fig.add_subplot(1, 1, 1) | |
ax.spines['left'].set_position('center') | |
ax.spines['bottom'].set_position('zero') | |
ax.spines['right'].set_color('none') | |
ax.spines['top'].set_color('none') | |
ax.xaxis.set_ticks_position('bottom') | |
ax.yaxis.set_ticks_position('left') | |
# jedna instance grafu | |
plt.plot(xs, ys, f"{color}o") | |
def main(): | |
plot(f, color="b") | |
plot(f_po_f_moje, color="g") | |
plot(f_po_f, color="r") | |
# ukaze graf (otevre okynko) | |
plt.show() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
dobry vito povedlo se ti to