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

2 次查看(过去 30 天)
%% Variables
% Input
% a
% b the corresponding coefficients in the quadratic equation
% c
% Output
% x1 positive root of the equation
% x2 negative root of the equation
% Write your entire function code below here (Including header)
function [x1, x2] = RQ16_19(a,b,c) % The order of input variables should be a, b, and then c
x1 = (-b+sqrt(b^2-4*a*c))/(2*a);
x2 = (-b-sqrt(b^2-4*a*c))/(2*a);
RQ16_19(10,5,3)
end
I am trying to run this code in Matlab Grader and it keeps gigiving me that 'Out of memory' error? Can someone please help?

回答(2 个)

Gaurav Aggarwal
Gaurav Aggarwal 2020-7-2
Hi Sean,
RQ16_19(10,5,3)
The above function call is causing the problem. Consider one call to RQ16_19 with some parameters say a,b,c. The following chain of instructions will happen,
  1. call RQ16_19(a,b,c)
  2. calculate x1 and x2 for a,b,c
  3. call RQ16_19(10,5,3)
  4. calculate x1 and x2 for 10,5,3
  5. call RQ16_19(10,5,3)
  6. Repeats.
You see the infinite recursion here?
As per my understanding, you are trying to find roots of a quadratic equation, you wouldn't need the function call within the function. Just remove the above line of code, and then RQ16_19 should be able to work perfectly fine.
Hope this helps. Thanks.

Cris LaPierre
Cris LaPierre 2020-7-2
编辑:Cris LaPierre 2020-7-2
Do you want to be calling your function recursively? If so, try adding a semicolon at the end of your "RQ16_19(10,5,3)" line of code.
If your code prints too many lines to the screen, it can fill up the available memory in MATLAB Grader.
However, as Gaurav mentions, if you are just trying to solve the quadratic equation, you do not need to have your function call itself recursively.

类别

Help CenterFile Exchange 中查找有关 Scope Variables and Generate Names 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by