Graphing an arbitrary function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | Resolution number: 23953 Created on: Mar 7 2002 Problem Subject: Graphing an arbitrary function Problem Description: Can SPSS produce a graph which plots an arbitrary, user-specified function? Resolution Subject: SPSS can only plot data. Resolution Description: SPSS can only plot data. However, the macro "fnplot" which appears below will create the necessary data, compute the function values, and plot them. Save any open data file before running the macro, as the working data file will be replaced. The macro allows the user to: * give any expression which is valid on a COMPUTE statement as the function to be plotted; * specify the minimum and maximum x values; * specify the number of cases; * specify names for the x variable and the variable which contains the function values. First, save the macro found at the end of this note to a file named Fnplot.sps. Load the macro: This can be done in two ways. Either open it in a Syntax window, use the Run->All menu to run the window, and close the window (to avoid accidental changes), or use the INCLUDE command (illustrated below). Run the macro: Once the macro is loaded, it may be run from an SPSS command syntax window as many times as desired. Open a syntax window, type a macro call, and use the Run menu to run the macro. An example of how to INCLUDE the macro and several examples of how to call it once it is loaded follow. * Load the FNPLOT macro with INCLUDE. * Modify the path to match the actual location if necessary. INCLUDE 'C:\\Program Files\\SPSS\\Fnplot.sps' . * Examples of fnplot macro in use. * Before attempting to run these, save the text indicated below to a file. * . fnplot . fnplot 'x ** 2 '. fnplot "sin(x)" . fnplot 'sin(theta)' /varname='theta' . fnplot 'sin(theta)' /varname='theta' /cases=1001 /min=0 /max=6.283 /fnname=sin . fnplot 'pdf.normal(x,0,1)' /cases=1001 /min=-3 /max=3 /fnname=z . *. *. *Save the following as Fnplot.sps' .................................. * . DEFINE fnplot (!POSITIONAL !DEFAULT('#x') !CHAREND('/') /min = !DEFAULT(0) !CHAREND('/') /max = !DEFAULT(10) !CHAREND('/') /cases = !DEFAULT(101) !CHAREND('/') /varname = !DEFAULT('x') !CHAREND('/') /fnname = !DEFAULT('fn_x') !CHAREND('/') ) . new file. input program. compute #inc = ( !max - !min )/ ( !cases - 1 ) . loop #i = 1 to !cases. + compute #x = !min + (#i - 1) * #inc . + compute !UNQUOTE(!varname) = #x . + compute !UNQUOTE(!fnname) = !UNQUOTE(!1) . + end case. end loop. end file. end input program. variable label !UNQUOTE(!fnname) !QUOTE(!UNQUOTE(!1)) . execute. IGRAPH /VIEWNAME='Function Plot' /X1 = VAR(!UNQUOTE(!varname)) /Y = VAR(!UNQUOTE(!fnname)) /SCATTER COINCIDENT = NONE . !ENDDEFINE . |
Related pages
...