plotcurve <-
function(equation = "y = sqrt(1/(1+x^2))", ...){
leftright <- strsplit(equation, split = "=")[[1]]
left <- leftright[1] # The part to the left of the "="
right <- leftright[2] # The part to the right of the "="
expr <- parse(text=right)
xname <- all.vars(expr)
if(length(xname) > 1)stop(paste("There are multiple variables, i.e.",paste(xname,
collapse=" & "),
"on the right of the equation"))
if(length(list(...))==0)assign(xname, 1:10)
else {
nam <- names(list(...))
if(nam!=xname)stop("Clash of variable names")
assign("x", list(...)[[1]])
assign(xname, x)
}
y <- eval(expr)
yexpr <- parse(text=left)[[1]]
xexpr <- parse(text=xname)[[1]]
plot(x, y, ylab = yexpr, xlab = xexpr, type="n")
lines(spline(x,y))
mainexpr <- parse(text=paste(left, "==", right))
title(main = mainexpr)
}
plotcurve()
plotcurve("ang=asin(sqrt(p))", p=(1:49)/50)
No comments:
Post a Comment