# TITLE: Numerical Approximation for Partial Differentiation and Hessian # AUTHOR: Roy F.A. Maclean # EMAIL: rfamgm at gmail # WEB: http://www.spiderpixel.co.uk/caspro # DATE: 3Jul2007 # MAKE: CASIO # MODEL: 9850 or later # NOTES: # # PARDIFF - evaluates partial derivatives of a function # PARDIFF2 - evaluates second order partial derivatives # HESSIAN - evaluates the Hessian Matrix of a function # PDENTER - subroutine used by PARDIFF, PARDIFF2 and HESSIAN # PDIFLOOP - subroutine used by PARDIFF, PARDIFF2 and HESSIAN # PDIFFUNC - subroutine used by PDIFLOOP # # ----------------------------------------------------------- # # Each program when started will ask for how many variables. "VARS"? # # Then it will ask for the coordinates of the point to evaluate the derivatives at. "X1,X2..."? # # Then for the PARDIFF and PARDIFF2 it will ask which variables to differentiate. "DIFF VAR"? # # # Before running these programs, store the function in program PDIFFUNC as follows: # # example: function f(x1,x2,x3)=x1^2+x2^2+2x3^2-x1x2-x2x3-x1x3 # # First change this to variables that the calculator can understand: # e.g.: X^2+Y^2+2Z^2-XY-YZ-XZ # # You can use any of the variables A,..,Z, but you must tell the calulator what variables to use. # # For the above function you would store the following in program PDIFFUNC # # # List 1[1]->X # List 1[2]->Y # List 1[3]->Z # X^2+Y^2+2Z^2-XY-YZ-XZ # Return # # # If you want to change the function and the number of variables you could use for example # # List 1[1]->X # List 1[2]->Y # X^2+Y^2 # Return # List 1[3]->Z # # When changing the number of variables you don't need to delete anything after the 'Return' command. # Use the buttons [SHIFT][DEL] to go toggle Insert mode. # # Or you could use: # # List 1[1]->A # List 1[2]->B # A^2+B^2 # # ----------------------------------------------------------- # If you enter very large coordinates then the program will produce wrong answers. # These programs use 1E-4 as the small element. # Making this element larger will allow larger coordinates to be used # but the results will be less accurate in general. # You could change this to 1E-5 or 1E-6 to get more accuracy, # but this means the largest allowable coordinate will be smaller. # If you change it to 1E-7 or smaller then the programs won't work. # ----------------------------------------------------------- # # Notation: # # -> assignment arrow # => if..then, implication, double arrow # <= less than or equal to # \r polar r # \th theta # \exp the EXP button (*10^ button on the french models) # _ the triangle Disp symbol on the Pgrm menu # # ----------------------------------------------------------- @@ Program "PARDIFF" Prog "PDENTER" "DIFF VAR"?->\th 1\exp(-)4->\r List 1[\th]+\r->List 1[\th] Prog "PDIFFUNC" Ans->List 2[1] List 1[\th]-2\r->List 1[\th] Prog "PDIFFUNC" Ans->List 2[2] (List 2[1]-List 2[2])/2\r # ----------------------------------------------------------- @@ Program "PARDIFF2" Prog "PDENTER" 2->List 4[2] Seq(0,\r,1,2List 4[2],1->List 2 Seq(0,\r,1,List 4[2],1->List 3 "DIFF VAR" 1->\th:Lbl 2 ?->List 3[\th]:\th+1->\th \th<=List 4[2]=>Goto 2 Prog "PDIFLOOP" # ----------------------------------------------------------- @@ Program "HESSIAN" Prog "PDENTER" 2->List 4[2] Seq(0,\r,1,2List 4[2],1->List 2 Identity List 4[1]->Mat A 1->\r:Lbl 3 1->\th:Lbl 4 {\r,\th}->List 3 Prog "PDIFLOOP" Ans->Mat A[\r,\th] \th+1->\th \th<=List 4[1]=>Goto 4 \r+1->\r \r<=List 4[1]=>Goto 3 Mat A_ # ----------------------------------------------------------- @@ Program "PDENTER" {0,0,0,0,0}->List 4 "VARS"?->List 4[1] Seq(0,\r,1,List 4[1],1->List 1 "X1,X2..." 1->\th:Lbl 1 ?->List 1[\th] \th+1->\th \th<=List 4[1]=>Goto 1 # ----------------------------------------------------------- @@ Program "PDIFLOOP" 1->List 4[3]:Lbl 0 List 4[3]=1=>1\exp(-)4 List 4[3]=2=>(-)2\exp(-)4 Ans->List 4[4] List 3[2] List 1[Ans]+List 4[4]->List 1[Ans] List 3[1] List 1[Ans]+1\exp(-)4->List 1[Ans] Prog "PDIFFUNC" Ans->List 4[5] 2List 4[3]-1 List 4[5]->List 2[Ans] List 3[1] List 1[Ans]-2\exp(-)4->List 1[Ans] Prog "PDIFFUNC" Ans->List 4[5] 2List 4[3] List 4[5]->List 2[Ans] List 3[1] List 1[Ans]+1\exp(-)4->List 1[Ans] List 4[3]+1->List 4[3]:List 4[3]<=2=>Goto 0 List 3[2] List 1[Ans]+1\exp(-)4->List 1[Ans] (List 2[1]-List 2[2]-List 2[3]+List 2[4])/((2\exp(-)4)^List 4[2]) # ----------------------------------------------------------- @@ Program "PDIFFUNC" List 1[1]->A List 1[2]->B List 1[3]->C A^2+B+C Return List 1[4]->D List 1[5]->E List 1[6]->F List 1[7]->X List 1[8]->Y List 1[9]->Z # -----------------------------------------------------------