Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

polyval error with recurrence relation

Asked by T on 6 Oct 2013
Latest activity Commented on by T on 11 Oct 2013
function c = relation( n )
if n==0
   c = 1;
elseif n==1   
   syms x
   c = [x 0];
   syms x
   c = ((2*n-1)*x*[relation(n-1),0] - (n-1)*[0,0,relation(n-2)])/n;

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.





No products are associated with this question.

2 Answers

Answer by Walter Roberson on 6 Oct 2013
Accepted answer

In your code for relation(), why do you have


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.


Walter Roberson on 8 Oct 2013

What I was referring to as c1 was


and for c2 it was


breaking this into two assignments allows a breakpoint to be put in to confirm that the two are the same size before doing the subtraction

c = sum( (c1 - c2) / n )

which in turn is algebraically

c = (sum(c1) - sum(c2)) / n

you might need to simplify() the result for it to look sensible.

Is there a reason you are not using the explicit formulation involving the "choose" operator and x^k ?

T on 8 Oct 2013

That's right. And for some reason using c1 and c2 will not generate the same polynomials for n where if you used

c = ((2*n-1)*x*[relation(n-1),0] - (n-1)*[0,0,relation(n-2)])/n;

The point was to do this using recursive programming.

T on 11 Oct 2013

Actually I don't think I need to do any of the above:

for n=0:inf
int( ((2*n-1)*x*[relation(n-1),0] - (n-1)*[0,0,relation(n-2)])/n )* ((2*m-1)*x*[relation(m-1),0] - (m-1)*[0,0,relation(m-2)])/m),x,-1,1)

This will just give me zero. But is it possible to determine the round-off error? Or at least get the maximum value of n before the loop crashes?

I can set the recursion limit using:

set(0, 'RecursionLimit', 100000) but it keeps crashing still.

Walter Roberson
Answer by Matt J on 6 Oct 2013
Edited by Matt J on 6 Oct 2013

Get rid of the "syms x". You are doing only numerical manipulations with the polynomial so there is no apparent need to have it in symbolic form, and that includes for plotting. Example

 p=[1 2 1]; %polynomial coefficients

Also, why use trapsum instead of the builtin trapz?


T on 6 Oct 2013

I have removed syms x and ran with n = 8

However I still receive the error:

??? Undefined function or method 'relation' for input arguments of type 'double'.

How would I be able to plot the polynomial if the output for c returns an array with x as undefined?!

Matt J on 6 Oct 2013

It's the function relation() that can't be found. Check your path.

T on 6 Oct 2013

Okay now I receive the error:

??? Undefined function or variable 'x'.
Error in ==> rlegendre at 9
   c = ((2*n-1)*x*[rlegendre(n-1),0] - (n-1)*[0,0,rlegendre(n-2)])/n;

This happens when I remove syms x.

Matt J

Contact us