Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
2701 views
1
D1tr <- function(y, x = 1)
2
{
3
## Purpose: discrete trivial estimate of 1st derivative.
4
## ----------------------------------------------------------------------
5
## Arguments: x is optional; let's say we don't like "'"
6
## ----------------------------------------------------------------------
7
## Author: Martin Maechler, ~ 1990
8
n <- length(y)
9
10
if(length(x) == 1)
11
c(y[2] - y[1], 0.5 * (y[-(1:2)] - y[-((n-1):n)]), y[n] - y[n-1])/x
12
else {
13
## Here, already using non-matching apostrophes, but developer mode
14
## still seems to work ..
15
if(n != length(x)) stop("lengths' of x & 'y' must equal")
16
if(is.unsorted(x)) stop("'x' must be sorted !")
17
c(y[2] - y[1], 0.5 * (y[-(1:2)] - y[-((n-1):n)]), y[n] - y[n-1]) /
18
c(x[2] - x[1], 0.5 * (x[-(1:2)] - x[-((n-1):n)]), x[n] - x[n-1])
19
}
20
}
21
22