1 The functions in this file (well, the single function) will allow the
2 user to plot different functions using the Gnuplot program. In fact,
3 all it really does is output a number of points on the list and allow
4 the user to activate Gnuplot and use the plotting program to get the
7 The first line just gives the module name. For the moment, I don't
8 anticipate using any modules (although this may change).
13 Now we give the type of the function. This consists of a file name, a
14 list of values, and a function that goes from the appropriate types.
16 > plot2d:: (Show a, Show b) => String -> [a] -> (a -> b) -> IO()
17 > plot2d fl inp f = openFile fl WriteMode >>= \flh ->
18 > plot2d' flh inp f >>
21 > plot2d':: (Show a, Show b) => Handle -> [a] -> (a -> b) -> IO()
22 > plot2d' fl [] f = return ()
23 > plot2d' fl (x:xs) f = hPutStr fl (show x) >>
25 > hPutStr fl (show (f x)) >>
29 > plot3d:: (Show a, Show b, Show c) => String -> [a] -> [b] ->
30 > (a -> b -> c) -> IO()
31 > plot3d fl inp1 inp2 f = openFile fl WriteMode >>= \flh ->
32 > plot3d' flh inp1 inp2 f >>
35 > plot3d':: (Show a, Show b, Show c) => Handle -> [a] -> [b] ->
36 > (a -> b -> c) -> IO()
37 > plot3d' fl [] inp f = return ()
38 > plot3d' fl (x:xs) inp f = plot3d'' fl x inp f >>
42 > plot3d'':: (Show a, Show b, Show c) => Handle -> a -> [b] ->
43 > (a -> b -> c) -> IO()
44 > plot3d'' fl inp [] f = return ()
45 > plot3d'' fl x (y:ys) f = hPutStr fl (show x) >>
47 > hPutStr fl (show y) >>
49 > hPutStr fl (show (f x y)) >>
54 And now, let's create a function to make a range out of a triple of a
55 start point, an end point, and an increment.
57 > createRange:: (Num a, Ord a) => a -> a -> a -> [a]
58 > createRange s e i = if s > e then []
59 > else s : createRange (s+i) e i
61 We now settle down to a couple of more specific functions that do
62 things that are more unique to gnuplot. First, we have something that
63 creates the appropriate gnuplot command file.
65 > createGnuPlot:: Show a => String -> a -> a -> IO()
66 > createGnuPlot fl s e = openFile (fl ++ ".gnp") WriteMode >>= \flh ->
67 > hPutStr flh "set terminal latex\n" >>
68 > hPutStr flh "set output \"" >>
69 > hPutStr flh (fl ++ ".tex\"\n") >>
70 > hPutStr flh "set nokey\n" >>
71 > hPutStr flh "plot [" >>
72 > hPutStr flh (show s) >>
74 > hPutStr flh (show e) >>
75 > hPutStr flh "] \"" >>
76 > hPutStr flh (fl ++ ".plt\"") >>
77 > hPutStr flh " with lines\n" >>
80 And now we create a fairly specific plotExam function that takes a
81 string, a function, and two floats and produces the correct files
83 > plotExam:: String -> Float -> Float -> (Float -> Float) -> IO()
84 > plotExam fl s e f = plot2d (fl++".plt") r f >>
85 > createGnuPlot fl s e
86 > where r = createRange s e ((e - s) / 2500)