You can shorten your code a little bit (and minimize the chance of typos) by using the built-in dnorm()
function:
curve(dnorm(x,mean=0,sd=0.5),from=-4,to=4)
curve(dnorm(x,mean=0,sd=0.6),add=TRUE,col=2)
If I try uniroot()
over a reasonable range, I get sensible answers:
uniroot(function(x) dnorm(x,0,0.5)-dnorm(x,0,0.6), c(0,5)) ## 0.546
uniroot(function(x) dnorm(x,0,0.5)-dnorm(x,0,0.6), c(-5,0)) ## -0.546
If I try to start from huge values, the calculation is going to underflow (whether you do it by hand or use dnorm()
; the Gaussian goes below R's minimum representable valuable (see ?.Machine
) between 19 and 20.
dnorm(19,0,0.5) ## 2e-314
dnorm(20,0,0.5) ## 0
In some cases you can use log=TRUE
in dnorm()
to compute the log-probability and use that in computations instead, but here (as in many Bayesian computations) you're a bit stuck because you have to subtract the results.