Out of memory- infinite recursion error
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi, my code below keeps on coming up with an infinite recursion error, could someone tell me how to fix this please?
4 comentarios
Jan
el 29 de Abr. de 2021
@Cicely Blocksidge: Please post code as text and not as screenshot. Then the readers can suggest an improvement without having to retype your complete code.
Respuestas (2)
Jan
el 29 de Abr. de 2021
Posting a failing code only does not allow to fix it, because the readers do not have a chance to guess the intention of the code reliably. Therefore you have to explain the purpose either in the text or better in exhaustive comments inside the code.
You have created a loop over the counter i, but i is not used anywhere in the body of the loop.
Calling a function recursively from inside the function without any limitation is an "infinite recursion". This cannot work.
Maybe all you need is
function y = cicelysode(x)
D = 1e-7; % More efficient than the expensive 10^-7
v = 1e-5;
t = 1;
y = exp(-0.25 * D * (x - v * t) .^ 2) / sqrt(4 * pi * D * t);
end
2 comentarios
Jan
el 29 de Abr. de 2021
The code still contains a line, which falls itself recursively. But why?
How do you call it? If I remove the recursive call, I get a diagram:
function cicelyscode
D = 10^-7;
v = 10^-5;
t = 1;
x = 0:0.1:2;
figure
axes('NextPlot', 'add'); % Equivalent to: hold on
for i = 1:length(x)
y = (1 / sqrt(4*pi*D*t)) * exp(-((x(i) - v * t) .^ 2) / 4*D);
plot(x(i), y, '.');
end
end
This would work without a loop also:
function cicelyscode
D = 10^-7;
v = 10^-5;
t = 1;
x = 0:0.1:2;
y = (1 ./ sqrt(4*pi*D*t)) * exp(-((x - v * t) .^ 2) / 4 * D);
plot(x, y);
end
Steven Lord
el 29 de Abr. de 2021
Let's say I call cicelyscode with an input argument.
cicelyscode([0 1])
This will create a figure then start the for loop to iterate over the elements of x. You use that value of i (but do not use x at all) to compute y, then overwrite the x variable [0 1] that I passed into cicelyscode and then calls
cicelyscode(0:0.01:2)
This will create a figure then start the for loop to iterate over the elements of x. You use that value of i (but do not use x at all) to compute y, then overwrite the x variable 0:0.01:2 that I passed into cicelyscode and then calls
cicelyscode(0:0.01:2)
This will create a figure then start the for loop to iterate over the elements of x. You use that value of i (but do not use x at all) to compute y, then overwrite the x variable 0:0.01:2 that I passed into cicelyscode and then calls
cicelyscode(0:0.01:2)
This will create a figure then start the for loop to iterate over the elements of x. You use that value of i (but do not use x at all) to compute y, then overwrite the x variable 0:0.01:2 that I passed into cicelyscode and then calls
cicelyscode(0:0.01:2)
etc.
I could repeat this copy and paste a few more times, but I think you get the picture that MATLAB will never reach to the plot call in your function.
So some of the issues are:
1) You never use the x input argument with which the user calls the function
2) Then you overwrite that unused input argument with a different vector.
3) Every call to your function results in a new figure being opened and a new call to cicelyscode.
So you've showed us how you've tried to solve the problem with which you've been presented, but you haven't told us anything about the problem itself. I suspect that you don't need recursion at all, but you could confirm or reject my suspicion by showing us the problem statement. It's entirely possible this could be a four line script:
% Step 1: Define x
% Step 2: Use x to compute y
% Step 3: Open the figure window. This is optional since step 4 will do it if necessary
% Step 4: Call plot
If you need a function instead of a script, step 1 could be replaced with defining a function that accepts x as input.
0 comentarios
Ver también
Categorías
Más información sobre Annotations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
