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
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
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!