error: Out of memory. The likely cause is an infinite recursion within the program.
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
Benjamin Kraus
le 4 Juin 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
Réponses (1)
Benjamin Kraus
le 4 Juin 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.
0 commentaires
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!