rm(list=ls()) # löscht alle Objekte im Workspace
#
# Definition der Fehlerfunktion "zu Fuss"
erfu <- function(z) {
  fun <- function(z){exp(-z^2)}
  intgr <- integrate(fun,0,z)
  erf.def <- (2/sqrt(pi))*intgr$value # integrate liefert eine Liste
}
#
# Definition der komplementären Fehlerfunktion
erfc <- function(x) {erfc <- 1-erfu(x)}
#
# Datenvektor x für Berechnungen:
xmat <- as.matrix(seq(-5,5,0.1))  # gleichzeitig Umwandeln in Matrix für apply
#
# Berechnung der Werte mit apply (keine Schleife erforderlich!): 
y.erfu <- apply(xmat,1,erfu)
y.erfc <- apply(xmat,1,erfc)
#
# plot der Ergebnisse
par(mfrow=c(2,1))
cat("\n Bitte Position für Formel in der Graphik anklicken! \n (in beiden Graphiken)")
plot(xmat,y.erfu,ylab="erf",xlab="x",type="l",lwd=2,col="blue",main="error function")
p <- locator(1)
text(p$x,p$y,expression(erf(x)==frac(2,sqrt(pi))*integral(e^-t^2*dt, 0, x)))
plot(xmat,y.erfc,ylab="erfc",xlab="x",type="l",lwd=2,col="blue"
     ,main="complementary \n error function")
p <- locator(1)
text(p$x,p$y,expression(erfc(x) == 1-erf(x)))

#stop()
