I've found polynomial coefficients from my data:
R <- c(0.256,0.512,0.768,1.024,1.28,1.437,1.594,1.72,1.846,1.972,2.098,2.4029)
Ic <- c(1.78,1.71,1.57,1.44,1.25,1.02,0.87,0.68,0.54,0.38,0.26,0.17)
NN <- 3
ft <- lm(Ic ~ poly(R, NN, raw = TRUE))
pc <- coef(ft)
So I can create a polynomial function:
f1 <- function(x) pc[1] + pc[2] * x + pc[3] * x ^ 2 + pc[4] * x ^ 3
And for example, take a derivative:
g1 <- Deriv(f1)
How to create a universal function so that it doesn't have to be rewritten for every new polynomial degree NN
?
Now after quite much effort in demonstrating how we can work out this question ourselves, consider using R package
polynom
. As a small package, it aims at implementing construction, derivatives, integration, arithmetic and roots-finding of univariate polynomials. This package is written completely with R language, without any compiled code.We still consider the cubic polynomial example used before.
Note, by step-by-step construction, the resulting function has all parameters inside. It only requires a single argument for
x
value. In contrast, functions in the other two answers will take coefficients and derivative order as mandatory arguments, too. We can call this functionTo produce the same graph we see in other two answers, we get other derivatives as well:
Perhaps you have already noticed that I did not specify
nderiv
, but recursively take 1 derivative at a time. This may be a disadvantage of this package. It does not facilitate higher order derivatives.Now we can make a plot
My original answer may not be what you really want, as it was numerical rather symbolic. Here is the symbolic solution.
Of course,
Deriv
makes higher order derivatives easier to get. We can simply setnderiv
. ForD
however, we have to use recursion (see examples of?D
).If we use expression, we will be able to evaluate the result later. For example,
The above implies that we can wrap up an expression for a function. Using a function has several advantages, one being that we are able to plot it using
curve
orplot.function
.Note, the success of
fun
requiresexpr
to be an expression in terms of symbolx
. Ifexpr
was defined in terms ofy
for example, we need to definefun
withfunction (y, expr)
. Now let's usecurve
to plotcubic
anddcubic
, on a range0 < x < 5
:The most convenient way, is of course to define a single function
FUN
rather than doingf
+fun
combination. In this way, we also don't need to worry about the consistency on the variable name used byf
andfun
.Suppose we want to evaluate a cubic polynomial with coefficients
pc <- c(0.1, 0.2, 0.3, 0.4)
and its derivatives onx <- seq(0, 1, 0.2)
, we can simply do:Now plotting is also easy:
Since my final solution with symbolic derivatives eventually goes too long, I use a separate session for numerical calculations. We can do this as for polynomials, derivatives are explicitly known so we can code them. Note, there will be no use of R expression here; everything is done directly by using functions.
So we first generate polynomial basis from degree
0
to degreep - n
, then multiply coefficient and factorial multiplier. It is more convenient to useouter
thanpoly
here.We still use the example
x
andpc
defined in the symbolic solution:The result is consistent with what we have with
FUN
in the the symbolic solution.Similarly, we can plot
g
usingcurve
: