https://github.com/cran/pracma
Raw File
Tip revision: 162b33221cccd2760e0dd44598539c521ffcd73b authored by HwB on 18 March 2011, 00:00:00 UTC
version 0.2-2
Tip revision: 162b332
polyfit.R
###
### POLYFIT.R  Polynom
###

polyfit <- function(x, y, n=1) {
    if (!is.numeric(x) || !is.numeric(y))
        stop("Arguments x and y must be numeric.")
    if (length(x) != length(y))
        stop("Vectors/matrices x and y must be of same length.")
    if (is.null(n) || n < 0 || ceiling(n) != floor(n))
        stop("Degree n must be a non-negative integer.")

    x <- x[1:length(x)]; y <- y[1:length(y)]
    A <- outer(x, seq(n, 0), "^")
    p <- qr.solve(A, y)
    return(p)
}
back to top