Skip to content
Prev 166631 / 398502 Next

How to get solution of following polynomial?

In theory,
you could define the following 2 functions

powermat <- function(myvec) {
  powervec <- function(x,maxpower)
    sapply(0:maxpower,function(n)x^n)
  sapply(myvec,function(x)powervec(x,length(myvec)-1))
}

polycoeffs <- function(fn,order,support=0:order)
  solve(t(powermat(support)),sapply(support,Vectorize(fn)))

and then use polycoeffs(fn,4)
with your function fn to extract the coefficients of the polynomial.
polycoeffs takes a function and the order of a polynomial
as its input and computes the coefficients of the polynomial
or the given order with the same values as the function
at the points 0:order.
If the function is a polynomial of the given order, it gives you
exactly the coefficients you need.
You may use another set of support points (points where the function is
evaluated) as an optional argument.
Still, this method is not extremely reliable. If the support points are
not chosen well, you might get rather unreliable results.

A safer way would be to use a computer algebra system to extract the
coefficients of your polynomial. You could use Ryacas to do this from R.
But you still would have to grasp the syntax of yacas.
Paul Smith wrote: