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

조회 수: 1 (최근 30일)
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:
  댓글 수: 1
Benjamin Kraus
Benjamin Kraus 2021년 6월 4일
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

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

답변 (1개)

Benjamin Kraus
Benjamin Kraus 2021년 6월 4일
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.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by