In Matlab, polynomials are represented by a vector of coefficients. For example, the polynomial p=a*x^2 + b*x + c is represented by the vector p=[a, b, c].
In this problem, you will be given a polynomial p and a power N. We would like you to return the vector q that represents the polynominal p^N, the Nth power of p. If p = (x + 1), for instance, you will be returning the coefficients of (x+1)^N. (N will be a positive integer greater than 0.)
Thought of another way to do this...
q=poly(kron(roots(p),ones(N,1)));
One-liner with size 21, but fails because of trivial roundoff error :(
This isnt a particularly difficult problem
you can call this function powerpoly
function ppower = powerpoly(p,n)
ppower = p;
i = 1
while i < n
ppower = conv(ppower,p);
i = i + 1;
end
This is a numerical algorithm and is not exact, but very accurate. I'm failing the test because the output for p=1:5; N=3 is off by this amount...
-3.4972e-14 8.8818e-15 3.5527e-14 0.0000e+00 0.0000e+00 -2.8422e-14
Columns 7 through 12:
0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 -5.6843e-14
Column 13:
0.0000e+00
COMPLETELY RIDICULOUS!!!!. Using isequal() is a poor choice for evaluating numerical algorithms.
Here is my algorithm that "failed" the isequal test by -5e-14 on a few values. I thought I would actually try to write a somewhat fast algorithm instead of just a for loop calling conv() repeatedly and reallocating memory each time.
function q = polypow(p,N)
q=ifft((fft([p zeros(1,(N-1)*(length(p)-1))])).^N);
end
Given you comments above, you might be interested in solving my convolution series at http://www.mathworks.com/matlabcentral/cody/?term=Fast+1-D+Convolution
237 Solvers
Project Euler: Problem 7, Nth prime
471 Solvers
Back to basics 25 - Valid variable names
291 Solvers
339 Solvers
281 Solvers