My function doesn't append to it's list

%calculates polynomials of form Pk = ((2k-1)t/k)*Pk-1 - ((k-1)/k)*Pk-2
function [poly, filllist] = calc_poly (n, t, filllist)
if n == 0
filllist = [filllist; ones(size(t))];
poly = ones(size(t));
return
end
if n == 1
filllist = [filllist; t];
poly = t;
return
end
poly = ((2*n-1)/n)*t.*calc_poly(n-1, t, filllist) - ((n-1)/n)*calc_poly(n-2, t, filllist);
filllist = [filllist; poly];
end
This function is supposed to recursively calculate the polys and append them into filllist, but the array remians unalatered after running it. I know that as it stands there would be duplicates in the list, but I first want to get the function to actually do anything.

답변 (1개)

Steven Lord
Steven Lord 2024년 5월 10일

0 개 추천

Your recursive calls only call your function with one output argument, so the modified second output argument is not returned from that function. Therefore any changes you make to your third input argument are discarded when each recursive call returns.

댓글 수: 6

Kacper
Kacper 2024년 5월 10일
편집: Kacper 2024년 5월 10일
I tried it with only one output and it still doesn't work. I don't actually care about the filllist output since it's supposed to operate on the given array, not an array of it's own context.
%calculates polynomials of form Pk = ((2k-1)t/k)*Pk-1 - ((k-1)/k)*Pk-2
function [poly, filllist] = calc_poly (n, t, filllist)
if n == 0
filllist = [filllist; ones(size(t))];
poly = ones(size(t));
return
end
if n == 1
filllist = [filllist; t];
poly = t;
return
end
[poly1, filllist] = calc_poly(n-1, t, filllist);
poly2 = calc_poly(n-2, t, []);
poly = ((2*n-1)/n)*t.*poly1 - ((n-1)/n)*poly2;
filllist = [filllist; poly];
end
This is a revised function where it returns the fillist with each call, but it still does nothing. I call it like this:
syms z;
list = [];
[polynomials, list] = calc_poly(1, z, list);
What do I do now? There's nothing on passing by reference or copy in matlab's variables and matricies docs.
It gives the result I would expect from that code ?
syms z;
list = [];
[polynomials, list] = calc_poly(1, z, list)
polynomials = 
z
list = 
z
%calculates polynomials of form Pk = ((2k-1)t/k)*Pk-1 - ((k-1)/k)*Pk-2
function [poly, filllist] = calc_poly (n, t, filllist)
if n == 0
filllist = [filllist; ones(size(t))];
poly = ones(size(t));
return
end
if n == 1
filllist = [filllist; t];
poly = t;
return
end
[poly1, filllist] = calc_poly(n-1, t, filllist);
poly2 = calc_poly(n-2, t, []);
poly = ((2*n-1)/n)*t.*poly1 - ((n-1)/n)*poly2;
filllist = [filllist; poly];
end
Kacper
Kacper 2024년 5월 12일
편집: Kacper 2024년 5월 12일
huh. The list is empty when I run it.
Experiment with creating a script that has both parts together, and running the script. You can use the "Copy" box on the code I posted to get the code to be pasted into the script.
Kacper
Kacper 2024년 5월 12일
I pasted your code to a new script and it worked, despite the previous script being the same. huh. Anyway, thx for your answers!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

제품

릴리스

R2024a

질문:

2024년 5월 10일

댓글:

2024년 5월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by