지수함수 y = exp(1/x) 의 그래프 그리기

참고로 이 함수는 두 함수 f(x) = 1/x 과 g(x) = exp(x)  의 합성함수이다.
 


* 윈도우 XP 에서 Mathematica 8 을 이용하여 그린 함수의 그래프:




* 윈도우 7 에서 Mathematica 8 을 이용하여 그린 함수의 그래프:




* Mac OS X Lion 에서 Maxima 5.25.0 을 이용하여 지수함수의 그래프를 그리기 위한 명령



* 위의 명령으로 별도의 창에 그려진 지수 함수 y = exp(1/x)  의 그래프





* Mac OS X Lion 에서 Gnuplot 을 이용하여 지수함수 y = exp(1/x) 의 그래프 그리기:

gnuplot> set term aqua

gnuplot> set xzeroaxis linetype 0 linewidth 1.000

gnuplot> set yzeroaxis linetype 0 linewidth 1.000

gnuplot> set xtics axis in scale 1,0.5 offset character 0, 0, 0 autofreq 

gnuplot> set ytics axis in scale 1,0.5 offset character 0, 0, 0 autofreq 

gnuplot> set yrange [ -10.0 : 10.0 ] noreverse nowriteback      

gnuplot> set xrange [ -10.0 : -0.01 ] noreverse nowriteback                

gnuplot> plot exp(1/x), 1




* 위의 명령으로 그려진 지수함수 y = exp(1/x) 의 그래프 중 좌측 반쪽:




* 우측 반쪽을 그리기 위한 Gnuplot 명령:

gnuplot> 
set xrange [ 0.01 : 10.0 ] noreverse nowriteback

gnuplot> plot exp(1/x), 1



* 위의 명령으로 그려진 지수함수 y = exp(1/x) 의 그래프 중 우측 반쪽:




* 윈도우 XP 의 Python 2.7 에 matplotlib 1.0.1 을 (모두 32비트 용으로) 설치하여 그린 지수함수 y = exp(1/x) 의 그래프:

#!/usr/bin/env python

import math
from pylab import *
import matplotlib.pyplot as plt
import matplotlib.lines as lines


def make_xaxis(ax, yloc, offset=0.05, **props):
    xmin, xmax = ax.get_xlim()
    locs = [loc for loc in ax.xaxis.get_majorticklocs()
            if loc>=xmin and loc<=xmax]
    tickline, = ax.plot(locs, [yloc]*len(locs),linestyle='',
            marker=lines.TICKDOWN, **props)
    axline, = ax.plot([xmin, xmax], [yloc, yloc], **props)
    tickline.set_clip_on(False)
    axline.set_clip_on(False)
    for loc in locs:
        ax.text(loc, yloc-offset, '%1.1f'%loc,
                horizontalalignment='center',
                verticalalignment='top')

def make_yaxis(ax, xloc=0, offset=0.05, **props):
    ymin, ymax = ax.get_ylim()
    locs = [loc for loc in ax.yaxis.get_majorticklocs()
            if loc>=ymin and loc<=ymax]
    tickline, = ax.plot([xloc]*len(locs), locs, linestyle='',
            marker=lines.TICKLEFT, **props)
    axline, = ax.plot([xloc, xloc], [ymin, ymax], **props)
    tickline.set_clip_on(False)
    axline.set_clip_on(False)

    for loc in locs:
        ax.text(xloc-offset, loc, '%1.1f'%loc,
                verticalalignment='center',
                horizontalalignment='right')


fig = plt.figure(facecolor='white')
ax = fig.add_subplot(111, frame_on=False)

props = dict(color='black', linewidth=2, markeredgewidth=2)
ax.axison = False
ax.set_xlim(-10, 10)
ax.set_ylim(-1, 10)
make_xaxis(ax, 0, offset=0.2, **props)
make_yaxis(ax, 0, offset=0.5, **props)

x = arange(-10.0, -0.01, 0.01)
a = exp(1/x)
a1 = 0*x + 1

t = arange(0.01, 10.0, 0.01)
b = exp(1/t)
b1 = 0*x + 1
ax.plot(x, a,'b-',  x, a1,'k--',  t, b, 'b-',  t, b1, 'k--')

grid(True)
xlabel('----> x')
ylabel('----> y')

title('The graph of y = exp(1/x)')

# plot the colored markers on the graph
# ax.plot(t, b, 'd', markersize=3, markerfacecolor='blue')
# ax.plot(x, a, 'd', markersize=3, markerfacecolor='red')

# set the rectangular range to be viewed.
plt.axis([-10, 10, -1.0, 10.0])

plt.show()


(* 위의 소스는 수정 없이 윈도우 7 의 Python 3.2 64bit 에 matplotlib 1.1.0 64bit 를 설치하여 실행해도 된다 *)







* 윈도우 XP의 Python 2.7 & Tkinter 환경에서 지수함수 y = exp(1/x) 를 그려주는 파이썬 소스:

#! /usr/local/bin/python    <- The UNIX she-bang
# coding: MS949

#  Filename: seventhGraph.py
#
#  2011/09/10 Sat.    Modified by PHKim
#
# See: http://www.schockwellenreiter.de/pythonmania/pybutt.html

import Tkinter
import Canvas
import math    # for the exp function
import sys           # for sys.exit(0)   
from Tkconstants import *


class SeventhExponentialGraph:
   def __init__(self, master = None):
   self.canvas = Tkinter.Canvas(master, relief = RIDGE, bd = 2, bg = "white",
         width = 400, height = 400)
   self.canvas.pack()
  
   self.button = Tkinter.Button(master, text = " Draw ", command = self.draw)
   self.button.pack(side = BOTTOM, pady = 4)

   def make_xaxis(self, startx, endx, yloc, initx, deltax, offset=1, gap=20):
           i = initx
           px =startx
           while px < endx:
          self.canvas.create_line(px, yloc-3, px, yloc + 3, fill='black', width=1)
          if i != 0:
                   self.canvas.create_text(px, yloc-3 + offset + 12, text="%d" % i, fill='magenta')
          else:
                   self.canvas.create_text(px - 6, yloc-3 + offset + 12, text="%d" % i, fill='magenta')
               i = i + deltax
               px = px + gap

   def make_yaxis(self, starty, endy, xloc, inity, deltay, offset=-1, gap=20):
           i = inity
           py =starty
           while py < endy:
          self.canvas.create_line(xloc-3, py, xloc+3, py, fill='black', width=1)
          if i != 0:
              self.canvas.create_text(xloc-3 + offset + 2, py, text="%d" % i, fill='darkgreen')
               i = i + deltay
               py = py + gap

   def draw(self):
   self.canvas.create_line(0, 200, 440, 200, fill='black', width=2)
   self.canvas.create_line(200, 0, 200, 440, fill='black', width=2)
   self.canvas.create_line(0, 200-20, 440, 200-20, fill='gray', width=1)

   self.make_xaxis(0, 400, 200, -10, 1, 0.05, 20)
   self.make_yaxis(0, 400, 200, -10, 1, -10, 20)

   delta = 0.01
   t  = -10.0
   while t <= 0.01:
       # Draw the left part in the second quadrant
       x = t
       y = math.exp(1/x)
       xx = (x*20) + 200  # 20 pixel is the unit 1
       yy =  200 -  (y*20)              # 400 x 400 is the size of the drawing region
       if (t== -10.0):
       Canvas.Line(self.canvas, xx, yy, xx, yy, fill="blue", width=2)
       else:
       Canvas.Line(self.canvas, xOld, yOld, xx, yy, fill="blue", width=2)
       self.canvas.update_idletasks()
       xOld = xx
       yOld = yy
       t = t + delta

   delta = 0.01
   t  = 0.01
   while t <= 10+0.1:
       # Draw the right part in the first quadrant
       x = t
       y = math.exp(1/x)
       xx = (x*20) + 200  # 20 pixel is the unit 1
       yy =  200 -  (y*20)              # 400 x 400 is the size of the drawing region
       if (t== -10.0):
       Canvas.Line(self.canvas, xx, yy, xx, yy, fill="blue", width=2)
       else:
       Canvas.Line(self.canvas, xOld, yOld, xx, yy, fill="blue", width=2)
       self.canvas.update_idletasks()
       xOld = xx
       yOld = yy
       t = t + delta

   self.button.config(text = " Quit ", command = self.exit)
   self.canvas.update_idletasks()

   def exit(self):
   sys.exit(0)

if __name__ == "__main__":
   root = Tkinter.Tk()
   root.title("The curve of y = exp(1/x)")
 
   SeventhExponentialGraph(root)
   root.mainloop()





* 위의 소스를 실행하여 그려진 함수 y = exp(1/x) 의 그래프



Posted by Scripter
,