Store recursive intermediate values

I'm trying to save intermediate values when calling recursive function without using global variables. I don't find anything suiting my issue on mathworks topics and I'd need help.
I'm trying to replicate Legendre polynomials. For example I have to compute the 6-th order polynomial, but to compute so I need the first 5-th orders. But I'd like to keep the intermediates polynomails/results instead of only the 6-th polynom.
Here's the funcion I'm using and that I need to adjust:
function Y = Laguerre(X, k)
if (k==0)
Y=1;
elseif (k==1)
Y=1-X;
else
Y=(1/k)*((2*k-1-X).*Laguerre(X,k-1)-(k-1)*Laguerre(X,k-2));
end
end
Any clue could be appreciated !

4 Comments

@Cedric Which intermediate values?, What are X and K?
"But I'd like to keep the intermediates polynomails/results instead of only the 6-th polynom."
Do you want to keep all of them, or just some of them?
cedric W
cedric W on 7 Sep 2018
Edited: cedric W on 7 Sep 2018
X is the point at which we evaluate the k-th order Laguerre polynom
Keep all of the intermediate values
i.e. at the point x=3, the 3-rd order is polynom has a value of -0.5, the 2nd value of -2, the first order result is 1
"Keep all of the intermediate values"
It is not clear what you mean by this. Which of these do you mean?:
  • keep intermediate Y values that occur within the recursive function, that are otherwise discarded when the function returns.
  • keep all output values (i.e. the final Y value) from the function, over a range of X.
  • some other definition of "intermediate value".... ?
You might know what you want, but we don't. Please explain exactly which values you are talking about, with examples.

Sign in to comment.

 Accepted Answer

Method one: nested function: which lets you keep any intermediate values that you want:
function [Yout,C] = Laguerre(Xin, kin)
C = []; to collect any intermediate values
Yout = mynest(Xin,kin)
%
function Y = mynest(X,k)
if (k==0)
Y=1;
elseif (k==1)
Y=1-X;
else
Y=(1/k)*((2*k-1-X).*mynest(X,k-1)-(k-1)*mynest(X,k-2));
end
C(end+1) = .... anything you want
end
end
Method two: a second output argument:
function [Y,C] = Laguerre(Xin, kin)
[Y,C] = mysub(Xin,kin)
end
function [Y,C] = mysub(X,k)
if (k==0)
Y=1;
elseif (k==1)
Y=1-X;
else
[V1,C1] = mysub(X,k-1);
[V2,C2] = mysub(X,k-2);
Y=(1/k)*((2*k-1-X).*V1-(k-1)*V2);
end
C(end+1) = ... something with C1,C2
end

4 Comments

Do you know what's the fastest solution ?
I don't understand the "C(end+1)=..." part ? Could you please elaborate what should be there ?
"Do you know what's the fastest solution ?"
I doubt that either of them will be particularly fast, because fundamentally collecting arbitrary amounts of data within a recursive function is going to be slow.
"I don't understand the "C(end+1)=..." part ? Could you please elaborate what should be there ?"
It is just indexing into the array C, adding a new element onto the end. You can put whatever value you want there, I guess you might want:
C(end+1) = Y;
You were not very clear about exactly which values you wanted to store, under which conditions, or from which recursion depths, so I left the ... for you to fill in. The point of the ... is to also give a hint that you will probably have to spend some time experimenting and trying different things to get the results you want.
cedric W
cedric W on 10 Sep 2018
Edited: cedric W on 10 Sep 2018
I'm answering here for both your comments.
Indeed let's take an example:
Say X is the point where to evaluate the function, k is the order of the polynom, then 4 first-order Laguerre Polynomials are the followings:
1/ Y1(X)=1 ; (k=0)
2/ Y2(X)=1-X ; (k=1)
3/ Y3(X)=(1/3)*((2*3-1-X)*Y2(X) - (3-1)*Y1(X))) ; (k=2)
4/ Y4(X)=(1/4)*((2*4-1-X)*Y3(X) - (4-1)*Y2(X))) ; (k=3)
Therefore for k>=2, we need previous polynomials.
The issue is that the function I coded so far, if am evaluating Laguerre(1,3), the output will be Y4(1)= -0.667. But I would instead like to have an output Y=[Y1(1),Y2(1),Y3(1),Y4(1)]=[1,0,-0.5,0.667]
Intermediate values are then Y1, Y2 and Y3.
Using a nested function:
function C = Laguerre(Xin, kin)
C = nan(1,1+kin);
mynest(Xin,kin);
%
function Y = mynest(X,k)
switch k
case 0
Y = 1;
case 1
Y = 1-X;
otherwise
Y = (1/k)*((2*k-1-X).*mynest(X,k-1)-(k-1)*mynest(X,k-2));
end
C(k+1) = Y;
end
end
And tested:
>> Laguerre(1,3)
ans =
1 0 -0.5 -0.66667
Note that some k values repeat (e.g. k=1 for your example), in which case only the last Y value will be stored for that k value.

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

on 7 Sep 2018

Edited:

on 10 Sep 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!