问题
I'm trying to plot a logarithmic scale, but I keep on getting this error:
Error in plot.window(...) : invalid "log=1/h" specification.
I'm not sure what I am doing wrong. Below is my code:
#function
function(stepsize, temp_val, counter) {
while(counter < 0) {
counter <- counter + stepsize
px_norm <- dnorm(counter, mean = 0, sd = .04)
temp_val <- temp_val + px_norm }
temp_val <- 2*temp_val
temp_val <- temp_val *(stepsize/2)
print(temp_val, digits = 12)
}
#Initial step size
h <- .01
while (h > .00001) {
x <- calc_error(h, 0, -5) #Gives me a result around .5
err <- x - (exp(-.02)*0.5)
plot(1/h, err, log = "1/h")
h <- h/10 }
Basically, in this short function I'm trying to show as the step size increases, the error from the real answer will decrease. However, I'm having trouble plotting this. Any help will be appreciated. Thanks
回答1:
This should be an easy way to create the plot:
h <- 10^-seq(2, 4)
err <- lapply(h, function(x) calc_error(x, 0, -5) - (exp(-.02) * .5))
plot(1/h, err, log = "x")

回答2:
plot(1/h, err, log = "1/h")
log should be the axis of your plot you want in log space, not the actual data.
i.e.
plot(1/h, err, log = "x")
will plot your x axis in log space
回答3:
I guess you want something like this:
i <- 1
h <- 0.01
err <- vector(0,mode="numeric")
while (h[i] > .00001) {
x <- rnorm(1,mean=0.5,sd=0.05) #use calc_error instead
err <- c(err,x - (exp(-.02)*0.5))
i <- i+1
h <- c(h,h[i-1]/10) }
plot(1/h[-length(h)],err,log="x")
来源:https://stackoverflow.com/questions/12554336/plotting-log-scale-in-r