Hi all,
I am not used to using the computer to do calculus and have up to
now done my differentiation "by hand" , calling on skills I learned
many years ago and some standard cheat sheets.
My interest at present is in getting the second derivative of a
gaussian, which I did by hand and results in a somewhat messy
result involving terms in sigma^5 .. I have done some spot checks
against R on the first derivative and my algebra seems OK, for the
second derivative I get inflection points where expected via my
algebra and R but I have not yet done a detailed numerical
comparison (so I cannot be 100% sure of my algebra).
What I would like is for the output of D to look at least somewhat
similar to my hand algebra. It occurred to me that if expressions
could be constructed from subexpressions, I could get a more
readable result (from D), as opposed to
fx2
-(invroot2pi/sigma) * (exp(-0.5 * (((x - mu)/sigma)^2)) * (0.5 *
(2 * (1/sigma * (1/sigma)))) - exp(-0.5 * (((x - mu)/sigma)^2)) *
(0.5 * (2 * (1/sigma * ((x - mu)/sigma)))) * (0.5 * (2 *
(1/sigma * ((x - mu)/sigma)))))
So I tried, in the interests of simplification..
expr1<-expression( ((x-mu)/sigma)^2)
expr1
expression(((x - mu)/sigma)^2)
x
[1] 5 15 20
expr2<-expression(exp(-0.5*expr1))
expr2
expression(exp(-0.5 * expr1))
eval(expr2)
Error in -0.5 * expr1 : non-numeric argument to binary operator
I thought this might mean that I needed to define expr2 as calling
expr1(x) , as follows
expr2<-expression(exp(-0.5*expr1(x)))
expr2
expression(exp(-0.5 * expr1(x)))
eval(expr2)
Error in eval(expr, envir, enclos) : couldn't find function "expr1"
which seems to indicate that I am lacking in understanding of
some issues of scope and calling enviroments.
Is what I am trying to do ( get a "readable" machine generated
expression/formula for a higher derivative) possible? and if so, could
someone give me some clues as to how to go about it?
Thank you
----------------------------------------- the original code
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._