home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / bit / listserv / sasl / 5464 < prev    next >
Encoding:
Text File  |  1992-12-23  |  6.5 KB  |  221 lines

  1. Comments: Gated by NETNEWS@AUVM.AMERICAN.EDU
  2. Path: sparky!uunet!paladin.american.edu!auvm!DMRHRZ11.BITNET!SCHICK
  3. Message-ID: <SAS-L%92122309551385@AWIIMC12.IMC.UNIVIE.AC.AT>
  4. Newsgroups: bit.listserv.sas-l
  5. Date:         Wed, 23 Dec 1992 08:30:03 CET
  6. Reply-To:     Arnold Schick <SCHICK@DMRHRZ11.BITNET>
  7. Sender:       "SAS(r) Discussion" <SAS-L@UGA.BITNET>
  8. From:         Arnold Schick <SCHICK@DMRHRZ11.BITNET>
  9. Subject:      differentation in case of 3D
  10. Lines: 209
  11.  
  12. Hallo SAS-Lers,
  13.  
  14. since I had sent a %macro to differentiate a function y=f(x), now I would
  15. like to present a %macro to solve partial differentation of functions with
  16. two variables as z=f(x,y). It's also a present to _ALL_ for christmas and
  17. a big Thanks for your helps in the past.
  18.  
  19. The theory of the differentation is like this:
  20.  
  21.          Y                              parameter-formula:
  22.           ^           / f(x)
  23.    y(i+2) |          +                  y'   = (y    - y )/(x   - x ) + ERR
  24.           |        /                     i+1     i+1    i    i+1   i
  25.    y(i+1) |      +
  26.           |     /
  27.      y(i) |    +                        with y'    = 2*y' - y'
  28.           |                                    i=1      3    2
  29.           +------------------------>
  30.               x(i)   x(i+2)        X    from i=1 to n points.
  31.                  x(i+1)                 With ERR function equal zero by
  32.                                         small delta X = x(i+1) - x(i).
  33.  
  34.  And in case of z=f(x,y) is to differentiate with this chema partial in steps,
  35.  with varying of the dependent variable. The total derivation is the sum of
  36.  differential to X and to Y.
  37.  
  38. The %macro DIFFER3D handles a differentation for z=f(x,z) with 3 SAS
  39. data set variables. The %macro call is:
  40.  
  41.        %differ3d (data_in, data_out, x, y, z, m, nosort);
  42.  
  43.  x,y,z names the SAS data set variables within DATA_IN, which contains
  44.  the parameter of function z=f(x,y). If data set DATA_IN is not present
  45.  or missing, data set _LAST_ is inuse.
  46.  
  47.  the data set DATA_OUT includes the orginal x,y,z values and the z_diff
  48.  variable with them values. If data set DATA_OUT is not present, halts
  49.  the %macro.
  50.  
  51.  the factor m is a level of the source function: if m=0 the source function
  52.  is not modified, if m=negative the source function becomes a fall
  53.  (inclination) to the positive dependent variable (i.e. X) direction, and
  54.  if m=positive rise it in this direction. Factor m is to use for a correction
  55.  of the function z=f(x,y) like z=m*dependent_variable by differentation.
  56.  If m is not present or missing, m=0 is inuse.
  57.  
  58.  the SORT option tells the %macro, if the input data set is not sorted
  59.  by  dependent variables, and it is to sort through the %macro, NOSORT
  60.  depresses it. If this option is not present, NOSORT is inuse.
  61.  
  62. In cases of needed inter-values between two points i and i+1 is to fit
  63. with the G3GRID procedure before the %macro was invoked.
  64.  
  65.  
  66. So long,
  67.  
  68. merry Christmas and a Happy N.Y. 1993
  69.  
  70. Arnold Schick  University of Marburg/Germany
  71. ----------------------------------------------------------------------
  72. Here is this %MACRO for your use with 3 SAS data set examples (3rd example
  73. is inuse and the others are enclosed in a comment):
  74.  
  75. %macro differ3d (data, out, x, y, z, m, sorts); *developed by Arnold Schick;
  76. options nosource nostimer nonotes nosymbolgen;  *ACC, University of Marburg;
  77.  
  78.   %if &data  =  or &data  = . %then %let data = _LAST_ ;
  79.   %if &out   =  or &out   = . %then %goto quit;
  80.   data _null_;
  81.     b = symget ('m') / 1;
  82.     if b = . then b = 0;
  83.     call symput('m',b);
  84.   run;
  85.  
  86.   %if &sorts = sort or &sorts = . or &sorts =  %then %do;
  87.       proc sort data = &data  out = &out;
  88.         by &x &y;
  89.       run;
  90.     %end;
  91.       %else %do;
  92.          data &out;
  93.            set &data;
  94.          run;
  95.       %end;
  96.  
  97.   data _null_ ;
  98.     set &out;
  99.     if _N_ = 1 then call symput('y_1', &y);
  100.     if _N_ = 2 then call symput('y_2', &y);
  101.     if _N_ = 3 then do; call symput('y_3', &y); stop; end;
  102.   run;
  103.  
  104.   data anfang(keep= &x dz_start);
  105.      set &out ;
  106.      x1 = lag( &x );
  107.      y1 = lag( &y );
  108.      z1 = lag( &z ) + &m * &y;
  109.      if y1 = &y then delete;
  110.      if &y_2 = &y or &y_3 = &y then do;
  111.          dz   = ( &z + &m * &y - z1 )/( &y - y1 );
  112.          dz_b = lag(dz);
  113.          if &y_3 = &y then do;
  114.             dz_start = 2*dz_b - dz;
  115.             output anfang;
  116.          end;
  117.      end;
  118.   run;
  119.  
  120.   data &out;
  121.     set &out;
  122.     merge &out anfang;
  123.        by &x;
  124.     length z_diff 8 ;
  125.     y_previo = lag( &y );
  126.     z_previo = lag( &z ) + &m * &y;
  127.     if &y = y_previo then delete;
  128.     if &y_1 = &y then z_diff = dz_start;
  129.                  else if &m ^= 0 then z_diff = -(z_previo - &z + &y * &m)
  130.                                                /( &y - y_previo);
  131.                                  else z_diff = -(z_previo - &z)
  132.                                                /( &y - y_previo);
  133.     drop y_previo z_previo dz_start;
  134.   run;
  135.  
  136.   %goto final ;
  137.   %quit : %put HALT: please define OUT data set (2nd parameter) ;
  138.   %final : ;
  139.  
  140. options source stimer notes;
  141. %mend differ3d;
  142.  
  143. /*
  144. * SAS data set example one: surface z=exp(-x*y)/(2-cos(x*y))*sin(y-x);
  145. data eins;
  146.   do x=0 to 1 by 0.05;
  147.      do y=0 to 2 by 0.1;
  148.         z=exp(-x*y)/(2-cos(x*y))*sin(y-x);
  149.         output;
  150.      end;
  151.   end;
  152. run;
  153.  
  154. * SAS data set example two: Cowboy Hat  z=sin(sqrt(x**2 + y**2));
  155. data eins;
  156.   do x=-5 to 5 by 0.25;
  157.      do y=-5 to 5 by 0.25;
  158.         z = sin(sqrt(x*x + y*y));
  159.         output;
  160.      end;
  161.   end;
  162. run;
  163. */
  164. * SAS data set example three: reconstruction of the Cowboy Hat;
  165. data eins;
  166.  do i=1 to 30;
  167.   x=10*ranuni(33)-5;
  168.   y=10*ranuni(35)-5;
  169.   z=sin(sqrt(x*x + y*y));
  170.   output;
  171.  end;
  172. run;
  173. proc g3grid data=eins out=eins;
  174.   grid x*y=z / spline
  175.                axis1=-5 to 5 by 0.25
  176.                axis2=-5 to 5 by 0.25;
  177. run;
  178. proc sort data=eins out=eins;
  179.   by x y;
  180. run;
  181.  
  182. %differ3d (eins, zwei,  x, y, z     ,-0.0025,nosort);
  183. %differ3d (eins, drei,  y, x, z     ,.,sort);
  184. %differ3d (drei, sechs, x, y, z_diff,0,sort);
  185. %differ3d (zwei, fuenf, y, x, z_diff,,sort);
  186.  
  187. * the following part shows the results;
  188. title 'orginal function';
  189. proc g3d data=eins;
  190.   plot y*x=z;
  191. run;
  192. title 'partial derivative to X';
  193. proc g3d data=zwei;
  194.   plot y*x=z_diff ;
  195. run;
  196. title 'partial derivative to Y';
  197. proc g3d data=drei;
  198.   plot y*x=z_diff;
  199. run;
  200. title 'partial derivative to XY';
  201. proc g3d data=fuenf;
  202.   plot y*x=z_diff;
  203. run;
  204. title 'partial derivative to YX';
  205. proc g3d data=sechs;
  206.   plot y*x=z_diff;
  207. run;
  208.  
  209. *---------this part counts the total derivative and shows the result;
  210. proc sort data=drei out=drei; by x y; run;
  211. data vier;
  212.   set drei (rename=(z_diff=zz_diff));
  213.   merge drei zwei;
  214.     by x y;
  215.   z_diff = zz_diff+z_diff;
  216. run;
  217. title 'total derivative to X and Y';
  218. proc g3d data=vier;
  219.   plot y*x=z_diff;
  220. run;
  221.