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

1 回表示 (過去 30 日間)
Osman Ballhysa
Osman Ballhysa 2021 年 6 月 4 日
回答済み: Benjamin Kraus 2021 年 6 月 4 日
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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by