MATLAB: Polyval error with recurrence relation

polyval

function c = relation( n )
if n==0
c = 1;
elseif n==1
syms x
c = [x 0];
else
syms x
c = ((2*n-1)*x*[relation(n-1),0] - (n-1)*[0,0,relation(n-2)])/n;
sum(c)
end
n=8
m=n+1;
xval=linspace(-1,1,5000)
I=trapsum(polyval(relation(n),xval).*polyval(relation(m),xval),-1,1);
Gives the following output:
??? Error using ==> polyval
Inputs to polyval must be floats, namely single or double.
Error in ==> polyval at 63
y = zeros(siz_x, superiorfloat(x,p));
What needs to be done to ensure that the output for the recurrence relation is a double?
I mean, in the function, we have syms x but this is to acquire the polynomial in analytic form. It's not necessary in this case but I need to plot it later on. The main issue I have is evaluating the integral using trapsum but I would need to do both; plot for various n, evaluate the integral.

Best Answer

In your code for relation(), why do you have
sum(c)
which is just displaying the sum rather than returning the sum ?
After you build the relation symbolically, use
RelationM = relation(m);
RelationN = relation(n);
funM = matlabFunction(RelationM);
funN = matlabFunction(RelationN);
Y = funM(xval) .* funN(xval);
trapsum(Y, -1, 1)
However, I do not know what your trapsum routine does: it is not a routine in any Mathworks toolbox, and the obvious trapsum in the File Exchange uses a different calling sequence.