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

1 view (last 30 days)
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;

Answers (2)

Walter Roberson
Walter Roberson on 10 Apr 2022
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 Comments
Voss
Voss on 10 Apr 2022
"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 on 10 Apr 2022
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

Sign in to comment.


Alireza
Alireza on 11 Apr 2022
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 Comments

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!