# error: Out of memory. The likely cause is an infinite recursion within the program.

10 views (last 30 days)
Osman Ballhysa on 4 Jun 2021
Answered: Benjamin Kraus on 4 Jun 2021
function y = my_sin(x,n)
if n<=0 %if y=0/neg. Numer -->y=0
y = 0;
elseif n~= floor(n) %if y= pos.decimal-->y=NaN
y=NaN;
%Gauß
else
%alternative ?
%syms x i n
%y = symsum((-1)^i * x^(2*i+1)/factorial(2*i+1), i, 0, n)
for i=0:n
y=my_sin(x,n)+(-1)^i * x^(2*i+1)/factorial(2*i+1);
end
end
This was the exercise:
Benjamin Kraus on 4 Jun 2021
When you post code in a question, you can format your code so that it is much easier for others to read. For example, I copied your code and reformatted it:
function y = my_sin(x,n)
if n<=0 %if y=0/neg. Numer -->y=0
y = 0;
elseif n~= floor(n) %if y= pos.decimal-->y=NaN
y=NaN; %Gauß
else
%alternative ?
%syms x i n
%y = symsum((-1)^i * x^(2*i+1)/factorial(2*i+1), i, 0, n)
for i=0:n
y=my_sin(x,n)+(-1)^i * x^(2*i+1)/factorial(2*i+1);
end
end

Benjamin Kraus on 4 Jun 2021
As this looks like a homework assignment, I'm not going to provide the direct answer to how to do this in MATLAB. However, I can explain why you are getting the error you are getting.
I did not read the code in detail, but I focused on the following lines:
function y = my_sin(x,n)
...
y=my_sin(x,n)+(-1)^i * x^(2*i+1)/factorial(2*i+1);
end
You function is taking the inputs (x and n) and passing them unmodified back into the same function. If you are going to use recursion, then you need to make sure the inputs to the recursive function are modified before calling the function again, or you will get an endless loop. However, recursion is not a very efficient way to solve this problem in MATLAB.