최근 편집
최근 토론
게시판 메인
도구
투표
무작위 문서
스킨 설정
파일 올리기
기타 도구
216.73.216.153
IP
사용자 도구
사용자 설정
로그인
회원 가입
최근 편집
최근 토론
돌아가기
삭제
이동
파일 올리기
연구실(bwlee42)/251111
(편집) (1)
(편집 필터 규칙)
20,5986
==합성함수 그래프 구현 코드 보완== [[연구실(bwlee42)/251109]]에서 제작한 합성함수 그래프 구현 코드를 보완한 것이다. {{{#!folding '''fc_9.py(누르면 펼쳐짐)''' {{{#!syntax python import tkinter as tk from tkinter import messagebox import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation def start_animation(): f_expr = f_entry.get() g_expr = g_entry.get() if not f_expr or not g_expr: messagebox.showerror("Error", "f(x)와 g(x)를 모두 입력하세요!") return try: f = lambda x: eval(f_expr, {"x": x, "np": np, "__builtins__": {}}) g = lambda x: eval(g_expr, {"x": x, "np": np, "__builtins__": {}}) except Exception as e: messagebox.showerror("Error", f"함수 입력 오류: {e}") return x_vals = np.linspace(-3, 3, 200) # f, g(f(x)) 전체 계산 f_all, gf_all = [], [] for x in x_vals: y_f = f(x) if not isinstance(y_f, (list, tuple, np.ndarray)): y_f = [y_f] f_all.extend(y_f) gf_all.extend([g(y) for y in y_f]) f_all = np.array(f_all) gf_all = np.array(gf_all) # --------------------------- # 애니메이션 패널 # --------------------------- fig, axes = plt.subplots(2,2,figsize=(12,8)) ax_f, ax_g, ax_h, ax_combo = axes.flatten() # f 패널 ax_f.plot(x_vals, [np.mean(f(x)) if isinstance(f(x),(list,tuple,np.ndarray)) else f(x) for x in x_vals], color='lightblue') ax_f.set_title('f(x)') ax_f.set_xlabel('x') ax_f.set_ylabel('f(x)=t') dot_f, = ax_f.plot([],[], 'ro') trail_f, = ax_f.plot([],[], 'b', lw=2) ax_f.set_xlim(min(x_vals), max(x_vals)) ax_f.set_ylim(min(f_all)-0.5, max(f_all)+0.5) # g 패널 g_all_sorted = np.sort(f_all) g_vals = [g(y) for y in g_all_sorted] ax_g.plot(g_all_sorted, g_vals, color='lightgreen') ax_g.set_title('g(t)') ax_g.set_xlabel('t') ax_g.set_ylabel('g(t)') dot_g, = ax_g.plot([],[], 'ro') trail_g_scatter = ax_g.scatter([],[], c=[], cmap='Greens', s=50) ax_g.set_xlim(min(f_all)-0.5, max(f_all)+0.5) ax_g.set_ylim(min(gf_all)-0.5, max(gf_all)+0.5) # h 패널 ax_h.set_title('h(x) = g(f(x))') ax_h.set_xlabel('x') ax_h.set_ylabel('h(x)') dot_h, = ax_h.plot([],[], 'ro') trail_h, = ax_h.plot([],[], 'r', lw=2) ax_h.set_xlim(min(x_vals), max(x_vals)) ax_h.set_ylim(min(gf_all)-0.5, max(gf_all)+0.5) # 추가 패널 (combo) ax_combo.set_title('Combined View') ax_combo.set_xlabel('x') ax_combo.set_ylabel('y') combo_dot_f, = ax_combo.plot([],[], 'ro', label='f(x)') combo_trail_f, = ax_combo.plot([],[], 'b', lw=2) combo_dot_g, = ax_combo.plot([],[], 'go', label='g(f(x))') combo_trail_g, = ax_combo.plot([],[], 'g', lw=2) combo_dot_h, = ax_combo.plot([],[], 'mo', label='h(x)') combo_trail_h, = ax_combo.plot([],[], 'm', lw=2) ax_combo.legend() ax_combo.set_xlim(min(x_vals), max(x_vals)) ax_combo.set_ylim(min(min(f_all), min(gf_all))-0.5, max(max(f_all), max(gf_all))+0.5) # --------------------------- # 히스토리 # --------------------------- x_hist_f, y_hist_f = [], [] x_hist_h, y_hist_h = [], [] trail_g_count = {} combo_xf, combo_yf = [], [] combo_xh, combo_yh = [], [] combo_xg, combo_yg = [], [] # --------------------------- # 애니메이션 업데이트 # --------------------------- def update(frame): x = x_vals[frame] y_f_raw = f(x) if not isinstance(y_f_raw,(list,tuple,np.ndarray)): y_f_vals = [y_f_raw] else: y_f_vals = y_f_raw y_h_vals = [g(y) for y in y_f_vals] # f dot_f.set_data([x]*len(y_f_vals), y_f_vals) x_hist_f.extend([x]*len(y_f_vals)) y_hist_f.extend(y_f_vals) trail_f.set_data(x_hist_f, y_hist_f) # g dot_g.set_data(y_f_vals, y_h_vals) for xf,yf in zip(y_f_vals,y_h_vals): key = (xf,yf) trail_g_count[key] = trail_g_count.get(key,0)+1 xs,ys,cs=[],[],[] for (xf,yf),count in trail_g_count.items(): xs.append(xf) ys.append(yf) cs.append(min(count,10)) if xs: trail_g_scatter.set_offsets(np.c_[xs,ys]) trail_g_scatter.set_array(np.array(cs)) # h dot_h.set_data([x]*len(y_h_vals), y_h_vals) x_hist_h.extend([x]*len(y_h_vals)) y_hist_h.extend(y_h_vals) trail_h.set_data(x_hist_h, y_hist_h) # combo panel combo_xf.extend([x]*len(y_f_vals)) combo_yf.extend(y_f_vals) combo_trail_f.set_data(combo_xf, combo_yf) combo_dot_f.set_data([x]*len(y_f_vals), y_f_vals) combo_xh.extend([x]*len(y_h_vals)) combo_yh.extend(y_h_vals) combo_trail_h.set_data(combo_xh, combo_yh) combo_dot_h.set_data([x]*len(y_h_vals), y_h_vals) combo_xg.extend(y_f_vals) combo_yg.extend(y_h_vals) combo_trail_g.set_data(combo_xg, combo_yg) combo_dot_g.set_data(y_f_vals, y_h_vals) return (dot_f, trail_f, dot_g, trail_g_scatter, dot_h, trail_h, combo_dot_f, combo_trail_f, combo_dot_g, combo_trail_g, combo_dot_h, combo_trail_h) ani = FuncAnimation(fig, update, frames=len(x_vals), interval=50, blit=False, repeat=False) plt.show() # --------------------------- # Tkinter GUI # --------------------------- root = tk.Tk() root.title("g(f(x))") tk.Label(root, text="f(x) =").grid(row=0,column=0) f_entry = tk.Entry(root, width=30) f_entry.grid(row=0,column=1) f_entry.insert(0,"np.sin(x)") tk.Label(root, text="g(x) =").grid(row=1,column=0) g_entry = tk.Entry(root, width=30) g_entry.grid(row=1,column=1) g_entry.insert(0,"np.exp(x)") start_btn = tk.Button(root, text="Start Animation", command=start_animation) start_btn.grid(row=2,column=0,columnspan=2,pady=10) root.mainloop() }}} }}} 최종적으로 모든 함수들이 구현되는 창을 하나 더 만들었다. {{{#!folding '''f(x)=sin(x), g(x)=exp(x)인 경우 실행(누르면 펼쳐짐)''' [[파일:fc-2.gif]] }}}
(임시 저장)
(임시 저장 불러오기)
기본값
모나코 에디터
normal
namumark
namumark_beta
macromark
markdown
custom
raw
(추가)
==합성함수 그래프 구현 코드 보완== [[연구실(bwlee42)/251109]]에서 제작한 합성함수 그래프 구현 코드를 보완한 것이다. {{{#!folding '''fc_9.py(누르면 펼쳐짐)''' {{{#!syntax python import tkinter as tk from tkinter import messagebox import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation def start_animation(): f_expr = f_entry.get() g_expr = g_entry.get() if not f_expr or not g_expr: messagebox.showerror("Error", "f(x)와 g(x)를 모두 입력하세요!") return try: f = lambda x: eval(f_expr, {"x": x, "np": np, "__builtins__": {}}) g = lambda x: eval(g_expr, {"x": x, "np": np, "__builtins__": {}}) except Exception as e: messagebox.showerror("Error", f"함수 입력 오류: {e}") return x_vals = np.linspace(-3, 3, 200) # f, g(f(x)) 전체 계산 f_all, gf_all = [], [] for x in x_vals: y_f = f(x) if not isinstance(y_f, (list, tuple, np.ndarray)): y_f = [y_f] f_all.extend(y_f) gf_all.extend([g(y) for y in y_f]) f_all = np.array(f_all) gf_all = np.array(gf_all) # --------------------------- # 애니메이션 패널 # --------------------------- fig, axes = plt.subplots(2,2,figsize=(12,8)) ax_f, ax_g, ax_h, ax_combo = axes.flatten() # f 패널 ax_f.plot(x_vals, [np.mean(f(x)) if isinstance(f(x),(list,tuple,np.ndarray)) else f(x) for x in x_vals], color='lightblue') ax_f.set_title('f(x)') ax_f.set_xlabel('x') ax_f.set_ylabel('f(x)=t') dot_f, = ax_f.plot([],[], 'ro') trail_f, = ax_f.plot([],[], 'b', lw=2) ax_f.set_xlim(min(x_vals), max(x_vals)) ax_f.set_ylim(min(f_all)-0.5, max(f_all)+0.5) # g 패널 g_all_sorted = np.sort(f_all) g_vals = [g(y) for y in g_all_sorted] ax_g.plot(g_all_sorted, g_vals, color='lightgreen') ax_g.set_title('g(t)') ax_g.set_xlabel('t') ax_g.set_ylabel('g(t)') dot_g, = ax_g.plot([],[], 'ro') trail_g_scatter = ax_g.scatter([],[], c=[], cmap='Greens', s=50) ax_g.set_xlim(min(f_all)-0.5, max(f_all)+0.5) ax_g.set_ylim(min(gf_all)-0.5, max(gf_all)+0.5) # h 패널 ax_h.set_title('h(x) = g(f(x))') ax_h.set_xlabel('x') ax_h.set_ylabel('h(x)') dot_h, = ax_h.plot([],[], 'ro') trail_h, = ax_h.plot([],[], 'r', lw=2) ax_h.set_xlim(min(x_vals), max(x_vals)) ax_h.set_ylim(min(gf_all)-0.5, max(gf_all)+0.5) # 추가 패널 (combo) ax_combo.set_title('Combined View') ax_combo.set_xlabel('x') ax_combo.set_ylabel('y') combo_dot_f, = ax_combo.plot([],[], 'ro', label='f(x)') combo_trail_f, = ax_combo.plot([],[], 'b', lw=2) combo_dot_g, = ax_combo.plot([],[], 'go', label='g(f(x))') combo_trail_g, = ax_combo.plot([],[], 'g', lw=2) combo_dot_h, = ax_combo.plot([],[], 'mo', label='h(x)') combo_trail_h, = ax_combo.plot([],[], 'm', lw=2) ax_combo.legend() ax_combo.set_xlim(min(x_vals), max(x_vals)) ax_combo.set_ylim(min(min(f_all), min(gf_all))-0.5, max(max(f_all), max(gf_all))+0.5) # --------------------------- # 히스토리 # --------------------------- x_hist_f, y_hist_f = [], [] x_hist_h, y_hist_h = [], [] trail_g_count = {} combo_xf, combo_yf = [], [] combo_xh, combo_yh = [], [] combo_xg, combo_yg = [], [] # --------------------------- # 애니메이션 업데이트 # --------------------------- def update(frame): x = x_vals[frame] y_f_raw = f(x) if not isinstance(y_f_raw,(list,tuple,np.ndarray)): y_f_vals = [y_f_raw] else: y_f_vals = y_f_raw y_h_vals = [g(y) for y in y_f_vals] # f dot_f.set_data([x]*len(y_f_vals), y_f_vals) x_hist_f.extend([x]*len(y_f_vals)) y_hist_f.extend(y_f_vals) trail_f.set_data(x_hist_f, y_hist_f) # g dot_g.set_data(y_f_vals, y_h_vals) for xf,yf in zip(y_f_vals,y_h_vals): key = (xf,yf) trail_g_count[key] = trail_g_count.get(key,0)+1 xs,ys,cs=[],[],[] for (xf,yf),count in trail_g_count.items(): xs.append(xf) ys.append(yf) cs.append(min(count,10)) if xs: trail_g_scatter.set_offsets(np.c_[xs,ys]) trail_g_scatter.set_array(np.array(cs)) # h dot_h.set_data([x]*len(y_h_vals), y_h_vals) x_hist_h.extend([x]*len(y_h_vals)) y_hist_h.extend(y_h_vals) trail_h.set_data(x_hist_h, y_hist_h) # combo panel combo_xf.extend([x]*len(y_f_vals)) combo_yf.extend(y_f_vals) combo_trail_f.set_data(combo_xf, combo_yf) combo_dot_f.set_data([x]*len(y_f_vals), y_f_vals) combo_xh.extend([x]*len(y_h_vals)) combo_yh.extend(y_h_vals) combo_trail_h.set_data(combo_xh, combo_yh) combo_dot_h.set_data([x]*len(y_h_vals), y_h_vals) combo_xg.extend(y_f_vals) combo_yg.extend(y_h_vals) combo_trail_g.set_data(combo_xg, combo_yg) combo_dot_g.set_data(y_f_vals, y_h_vals) return (dot_f, trail_f, dot_g, trail_g_scatter, dot_h, trail_h, combo_dot_f, combo_trail_f, combo_dot_g, combo_trail_g, combo_dot_h, combo_trail_h) ani = FuncAnimation(fig, update, frames=len(x_vals), interval=50, blit=False, repeat=False) plt.show() # --------------------------- # Tkinter GUI # --------------------------- root = tk.Tk() root.title("g(f(x))") tk.Label(root, text="f(x) =").grid(row=0,column=0) f_entry = tk.Entry(root, width=30) f_entry.grid(row=0,column=1) f_entry.insert(0,"np.sin(x)") tk.Label(root, text="g(x) =").grid(row=1,column=0) g_entry = tk.Entry(root, width=30) g_entry.grid(row=1,column=1) g_entry.insert(0,"np.exp(x)") start_btn = tk.Button(root, text="Start Animation", command=start_animation) start_btn.grid(row=2,column=0,columnspan=2,pady=10) root.mainloop() }}} }}} 최종적으로 모든 함수들이 구현되는 창을 하나 더 만들었다. {{{#!folding '''f(x)=sin(x), g(x)=exp(x)인 경우 실행(누르면 펼쳐짐)''' [[파일:fc-2.gif]] }}}
비로그인 상태이므로 문서 편집 및 게시판 글 작성 시 IP가 그대로 노출 됩니다.
전송
미리보기