MATLAB Answers

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

1 view (last 30 days)
Sean St Cyr
Sean St Cyr on 1 Jul 2020
Edited: Cris LaPierre on 2 Jul 2020
%% 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?

Answers (2)

Gaurav Aggarwal
Gaurav Aggarwal on 2 Jul 2020
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 on 2 Jul 2020
Edited: Cris LaPierre on 2 Jul 2020
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.

Community Treasure Hunt

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

Start Hunting!

Translated by