/*************************************************************************** Author : Yvon Lafranche (March 2007) E-mail : Yvon.Lafranche@univ-rennes1.fr Web : http://perso.univ-rennes1.fr/yvon.lafranche/ Fig4TeX page : http://perso.univ-rennes1.fr/yvon.lafranche/fig4tex/ Creation of a TeX file to draw a function f(x) over an interval [a,b] using the fig4TeX macro package. Assuming this script is saved in the file named "bcscript", entering the following command at the Unix prompt in a terminal window : bc -l bcscript > Fsin2x.tex will create a standalone TeX file containing the commands to draw the graph of the requested function f. This program computes n pairs of values (xi, f(xi)), where xi are equally spaced abscissae in the interval [a,b] and the function f is defined below in "ftest" via the "define" instruction. Here, f(x) = sin(2*x). According to the user's needs, this script can be easily modified by assigning other values to a, b or n, and by altering the definition of the function "ftest". The classical operators can be used : +, -, *, /, % (rem) and ^ (power). Moreover, the following mathematical functions are available : sqrt(x), s(x), c(x), a(x), l(x), e(x) and j(m,x) ; they compute respectively the square root, sine, cosine, arctangent, natural logarithm, exponential and bessel function of order m of x (see "man bc"). The following parameters can also be modified : . xdim and ydim are the desired dimensions of the figure given in mm, . ndec is the number of significant digits of the values printed on the graph, . trueorig is equal to 1 if the axes are wanted to cross at the true origin (with (0,0) coordinates). It equals 0 otherwise, in which case the axes cross at the lower left corner of the bounding box of the graph ; this choice always lead to a coherent graph. At last, in section 2 of the program, the legend of the graph can be modified or even suppressed. Search for "GraphLegend". *****************************************************************************/ /* ------------------------------------------------------------------ Section 1 : User defined settings ------------------------------------------------------------------ */ define ftest(x){ return(s(2*x)); } /* User function */ a = -1 /* Lower bound of the interval */ b = 1 /* Upper bound of the interval */ n = 30 /* Number of points to be computed */ xdim = 80 /* Horizontal dimension of the figure in mm */ ydim = 60 /* Vertical dimension of the figure in mm */ ndec=1 /* Number of significant digits printed (>= 0)*/ trueorig=0 /* Equals 1 if the true origin must be used, 0 otherwise. */ /* ------------------------------------------------------------------ Section 2 : Computation (in principle, do not need to be modified except the legend : search for "GraphLegend") ------------------------------------------------------------------ */ /* Some useful functions */ define format(n,x){ auto y scale=n; y = x/1; scale = 20; return (y);} define round(n,x){ auto y, z z=10^(n+1) d = 5; if (x<0) d = -d; y=z*x+d; scale=n; y=y/z; scale=20; return(y)} prec=5 /* First computation to set the scale factors */ h = (b-a)/(n-1) ymin = ftest(a); ymax = ymin; for (i=0; iymax) ymax = y } xsc = xdim/(b-a) ysc = ydim/(ymax-ymin) /* Creation of the TeX file */ print "\\input fig4tex\n" /* vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv */ /* Legend of the figure to be set to the desired text */ print "\\def\\GraphLegend{$y = \\sin(2 x)$}\n" /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */ print "% 1. Definition of characteristic points\n" print "\\figinit{mm}\n" if ( trueorig==1 ) { print "\\def\\Xmin{", format(prec,a*xsc), "}\n" print "\\def\\Ymin{", format(prec,ymin*ysc), "}\n" print "\\def\\Xmax{", format(prec,b*xsc), "}\n" print "\\def\\Ymax{", format(prec,ymax*ysc), "}\n" print "\\def\\Xori{", format(prec,-a*xsc), "}\n" print "\\def\\Yori{", format(prec,-ymin*ysc), "}\n" } if ( trueorig!=1 ) { print "\\def\\Xmin{0}\n" print "\\def\\Ymin{0}\n" print "\\def\\Xmax{", xdim, "}\n" print "\\def\\Ymax{", ydim, "}\n" print "\\def\\Xori{0}\n" print "\\def\\Yori{0}\n" } print "\\figpt0:(\\Xori,\\Yori)\n" print "% 2. Creation of the graphical file\n" print "\\psbeginfig{}\n" print "\\def\\Xmaxx{\\Xmax} % To customize the position\n" print "\\def\\Ymaxx{\\Ymax} % of the arrow-heads of the axes.\n" print "\\psaxes 0(\\Xmin, \\Xmaxx, \\Ymin, \\Ymaxx)\n" print "\\pslineC(\n" for (i=0; i<(n-1); i++){ x = a+i*h y = ftest(x) print format(prec,(x-a)*xsc), " ", format(prec,(y-ymin)*ysc), ",\n" } print format(prec,xdim), " ", format(prec,(ftest(b)-ymin)*ysc), "\n" print ")\n" print "\\psendfig\n" print "% 3. Writing text on the figure\n" print "\\figvisu{\\figBoxA}{\\GraphLegend}{%\n" print "\\figptsaxes 1:0(\\Xmin, \\Xmaxx, \\Ymin, \\Ymaxx)\n" print "% Points 1 and 2 are the end points of the arrows\n" print "\\figwritee 1:(5pt) \\figwriten 2:(5pt)\n" print "\\figptsaxes 1:0(\\Xmin, \\Xmax, \\Ymin, \\Ymax)\n" print "% Points 1 and 2 are the first two end points of the axes\n" print "\\figwrites 1:$",round(ndec,b),"$(5pt) \\figwritew 2:$",round(ndec,ymax),"$(8pt)\n" print "% Points 3 and 4 are the two other end points of the axes\n" print "\\figpttraC 3:=0/\\Xmin,0/ \\figwrites 3:$",round(ndec,a),"$(5pt) \n" print "\\figpttraC 4:=0/0,\\Ymin/ \\figwritew 4:$",round(ndec,ymin),"$(8pt)\n" print "}\n" print "\\centerline{\\box\\figBoxA}\n" print "\\bye\n" quit