필터 지우기
필터 지우기

I want to fix this Error : Out of memory. The likely cause is an infinite recursion within the program.

조회 수: 2 (최근 30일)
numintla.m
function [sol]=numintla(fun,n)
[xx,w]=gaussla(6); % Call the gaussla.m file
sx=size(xx,1);
sol=numintla('sin(x)',6);
for i=1:sx,
x=xx(i);
fx=eval(fun);
sol=sol+w(i)*fx;
end;
gaussla.m
function [xx,ww]=gaussla(n)
format long;
b(1)=1;
for i=1:n, % Calculation of the binomial coefficients
b(i+1)=(factorial(n))/(factorial(i)*(factorial(n+1-i)));
end;
for i=1:n+1, % The polynomial coefficients
poly(i)=((-1)^(n+1-i))*b(i)*(factorial(n)/(factorial(n+1-i)));
end;
xx=roots(poly);% The polynomial roots
for i=1:n, % Coefficients of the first derivative of the polynomial
polycd(i)=poly(i)*(n+1-i);
end;
for i=1:n, % Evaluation
x=xx(i);
solde=0;
for k=1:n,
solde=solde+polycd(k)*(x^(n-k));
end;
ww(i,1)=(factorial(n)^2)/(xx(i)*(solde^2));
end;

답변 (2개)

Walter Roberson
Walter Roberson 2022년 4월 10일
function [sol]=numintla(fun,n)
[xx,w]=gaussla(6); % Call the gaussla.m file
sx=size(xx,1);
sol=numintla('sin(x)',6);
Suppose numintla() gets invoked somehow.
Your numintla() code calls gaussla(6) and gets a result that it assigns to variables. It then calculations size() of something and assigns that to a variable.
The code then invokes numintla('sin(x)',6) to do some work. But numintla() is the exact same function you are already executing. So numintla() calls itself.
The once-nested call to numintla() calls gaussla(), and size(). Then the once-nested call to numintla() calls numintla('sin(x)',6) which is exactly the same function that is already executing.
Now the twice-nested call to numintla() calls gaussla() and size(), and calls numintla('sin(x)',6)
Now the three-times nested call to numintla().... and so on.
When does numintla() stop just calling itself and start returning a value to its caller?
  댓글 수: 4
Voss
Voss 2022년 4월 10일
"When does numintla() stop just calling itself and start returning a value to its caller?"
@Alireza: This question is for you to think about.
A related question is: Why does numintla() call itself in the first place?
function [sol]=numintla(fun,n)
% numintla is going to do something with (maybe solve) some
% function "fun", which has been given.
% numintla should use "n" as well (presumably), which is
% also given (but it does not use "n" in fact).
[xx,w]=gaussla(6); % Call the gaussla.m file
sx=size(xx,1);
% before doing whatever numintla is going to do with "fun" and "n",
% numintla must do whatever it does with 'sin(x)' and 6. That is,
% call numintla here, using fun = 'sin(x)' and n = 6:
sol=numintla('sin(x)',6);
Does it make sense that, no matter what fun and n are given, the function 'sin(x)' with parameter n = 6 must be dealt with first? (It doesn't make sense to me.)
Walter Roberson
Walter Roberson 2022년 4월 10일
sol=numintla('sin(x)',6)
sol =
3.026066427045930
function [sol]=numintla(fun,n)
[xx,w]=gaussla(6); % Call the gaussla.m file
sx=size(xx,1);
sol = 0;
for i=1:sx,
x=xx(i);
fx=eval(fun);
sol=sol+w(i)*fx;
end
end
function [xx,ww]=gaussla(n)
format long;
b(1)=1;
for i=1:n, % Calculation of the binomial coefficients
b(i+1)=(factorial(n))/(factorial(i)*(factorial(n+1-i)));
end
for i=1:n+1, % The polynomial coefficients
poly(i)=((-1)^(n+1-i))*b(i)*(factorial(n)/(factorial(n+1-i)));
end
xx=roots(poly);% The polynomial roots
for i=1:n, % Coefficients of the first derivative of the polynomial
polycd(i)=poly(i)*(n+1-i);
end
for i=1:n, % Evaluation
x=xx(i);
solde=0;
for k=1:n,
solde=solde+polycd(k)*(x^(n-k));
end
ww(i,1)=(factorial(n)^2)/(xx(i)*(solde^2));
end
end

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


Alireza
Alireza 2022년 4월 11일
actually from this code i want to extract x , w.
I copy this code from a paper but it seems that it does not match with that code.
  댓글 수: 3
Alireza
Alireza 2022년 4월 11일
what do recomend to fix it?
It has to give me the weight and zeros of Gauss Laguerre Quadrant.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by