Search code examples
matlabsumseriespolynomials

How to sum an arbitrary polynomial series in MATLAB?


Given:

y=[y(1),y(2),...,y(n)]

Where n is input by the user, and:

x=[x(1),x(2),...,x(n)]

a=[a0,a1,...,am]=[a(1),a(2),...,a(m+1)] 

Where m is also input by the user, then I need to compute:

y(p) = a0*x(p)^0 + a1*x(p)^1 + a2*x(p)^2 + ... + am*x(p)^m

y(p) = a(1)*x(p)^0 + a(2)*x(p)^1 + a(3)*x(p)^2 + ... + a(m+1)*x(p)^m.

i.e. Each element of y is a polynomial in m, y=a0+a1x+a2x^2+...+amx^m, using the pth x value for the pth y value.

In summation notation:

y(p) = **sum** (from q=0 to m) **[a(q+1)*x(p)^q]**

I'm not sure how to sum this series in MATLAB. Any help would be greatly appreciated!

EDIT:

I've attempted to evaluate each value of y(p) by the following, for example, y(2):

syms q a x  
f=a(q+1)*x(2)^q
y(2) = symsum(f, q, 0, m)  

However, this returns the error Invalid indexing or function definition.


Solution

  • If you have x defined as an N-element row vector of values, and a defined as an M-element row vector of polynomial coefficients, then you can use the function polyval to calculate your y values for a subset of m+1 polynomial terms and n values of x:

    y = polyval(flip(a(1:(m+1)), 2), x(1:n));
    

    Note that polyval expects the polynomial coefficients ordered from highest power to lowest power, so the order of the vector a(1:(m+1)) has to be flipped using flip.