'How to resolve this Out of memory. The likely cause is an infinite recursion within the program error
I am tagging my question and code along with output please into it tell where I did wrong.
Write a function function p=r1p(a,n,x) that computes and return p=P*x where P is a 2x2 projection matrix of rank 1
- with column space consisting of the scalar multiples of a and
- with null space consisting of the scalar multiples of n. Notes: The function must be named r1p a is a nonzero column vector with two components. n is a nonzero column vector with two components. x is a nonzero column vector with two components.
My code:
function p = r1p(a,n,x)
% computes the action of P, the 2x2 projection matrix of rank 1 having
% a as an eigenvector with eigenvalue 1 and
% n as an eigenvector with eigenvalue
a=[1; 0.01]
n=[0.01; 1]
x=[1; 0]
p=r1p(a,n,x)
end
Error: Out of memory. The likely cause is an infinite recursion within the program. Error in r1p (line 8) p=r1p(a,n,x)
Solution 1:[1]
from the reason that are described in the comment, the function just calls itself each time. The stack, which is a memory that holds details about all the chain of function calls, just fills up till it catch the whole allocated memory, and can't make more call since there is no memory to write the additional call.
It seems that you are missing the point of the recursion. On each stage, the input for the recursion should be changed, getting closer to a "basic" input that will be calculated directly. So this is a stage that is missing at your code - the recursive call for the function must be under a certain condition, and this condition should be fulfilled in some stage.
in addition, it seems that your function does not perform any calculation with your inputs.
see here a simple example:
myFactorial = factorial(5)
function output=factorial(input)
if (input<=0)
output=1;
else
output=input*factorial(input-1);
end
end
you can see that:
- the internal call for factorial is under condition
- the input is decreased so finally the input is 1 and the function is not called again.
- there is an internal calculation, that implements what the programmer wanted.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | assafp |
