How to go around infinite recursions

Hi. I have a series of functions that go Weight_Approx_1 -> Geometric_Approx -> Weight_Approx_2 . I would like for the Weight_Approx_2 to feed back in to Weight_Approx_1 so it can act as the new input. I can do this so well that I get an infinite loop without trying. How do I get a loop like this to return a final value from the Weight_Approx_2 program after a number of iterations I choose?
In other words, how do I rewrite a series of functions like below such that I control how many times f_c gets run? Once int == 2, it'll just be an infinite loop
function a = fa
for int = (1:10)
if int = 1
a = 1;
else
a = fc;
end
end
end
function b = fb
b = fa*2
end
function c = fc
c = fb + 1;
end

 Accepted Answer

I just redesigned the whole program for this program to work.

More Answers (1)

I'm not sure the code you've written describes what you intend. It's going to create an unterminated recursion that will cause an error very quickly.
Can you describe in words a little more clearly what you mean? Why are you using recursion at all? Why not something like:
x = 1; % whatever your initial estimate of the "thing" is
for k = 1:max_iterations
a = weight_approx_1(x);
b = geometric_approx(a);
x = weight_approx_2(b);
end

6 Comments

So what I start off with is a value being approximated in one method, and that method is no longer useful after acquiring that very first point. However, my geometry functions are functions of the code that relates to that first approximation, so if I want to get some iterations, I need to swap the value from the no-longer useful method to a method that gives me a value based on geometry. But that doesn't want to work out because I go into an infinite loop.
I think we might need some more specific details for this to make sense!
Alrighty, I attached a bunch of functions. The initial function is the WEIGHT_SIZING file and the output function is the WEIGHT_ALLOCATION file. The value output from WEIGHT_SIZING is seen in all the other files, and WEIGHT_ALLOCATION is composed of the outputs of all the other files including the WEIGHT_SIZING file. I would like the output [~,W_total] to be the new output of WEIGHT_SIZING so that value can go through all the other files and travel back to being an input of WEIGHT_ALLOCATION with this cycle repeating a few times. I know I could have done this just by copying and pasting the output of WEIGHT_ALLOCATION to be the output of WEIGHT_SIZING, but I would like to learn how to get MATLAB to do that for me.
The code I posted isn't very polished and I haven't thrown in a lot of comments. In the STD_ATM program, there's a line that changes the directory. If you are gonna use the code, you should probably get rid of that.
Sorry for taking a while to get back to you. Just looking at it now - what do you mean by "value output from WEIGHT_SIZING is seen in all the other files". The function WEIGHT_SIZING has no inputs or global variables, so I don't see how running this again will help ...
perhaps you could step me through the actual calculation you want performed (what variables need to be iterated)?
So the function WEIGHT_SIZING has an output of weight. This value is an input to every other function, and once this value has propagated through all the other functions, one of the outputs of WEIGHT_APPROXIMATION is also a weight. I would like this weight to cycle back to that WEIGHT_SIZING program and become the new output of WEIGHT_SIZING or the new input to all the other functions.
I inserted a picture of what kind of loop I would like. The writing in purple is what I have already, a straight flow from WEIGHT_SIZING to other programs that feed into WEIGHT_APPROXIMATION. What I would like is one of the loops in gray or blue, with the gray being a loop that gives me an infinite recursion and the blue seeming to be one that can't be done. The gray is one where I have a conditional in the WEIGHT_SIZING program that swaps the value from the method in which this program calculates weight to the value from the WEIGHT_ALLOCATION program, but because the second program is a function of the first, I get an infinite recursion. The blue one is where the value of WEIGHT_ALLOCATION is directly plugged in to the functions; whereever I have a function call for WEIGHT_SIZING, it calls for WEIGHT_ALLOCATION instead, bypassing the whole infinite recursion nonsense, but I don't know how to do that.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!