How to go around infinite recursions
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
추가 답변 (1개)
Richard Brown
2019년 11월 5일
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
Richard Brown
2019년 11월 7일
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)?
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!