Storing Outputs of a Recursive Function indexable by depth

5 views (last 30 days)
I am trying to run a monte carlo simulation i number of times. It involves a recursive function. I want to output an array of outputs with particular recursive depths (the number of time the recursive function is called). Here is some example of what I would like the code to look like
function [eion,exit_e] = E(t_in,x_in,i)
rd_e = 0; %This is the recursive depth tracker, it tracks the number of times a particular instance is called
rd_e = rd_e + 1;
for jj = 1 : 2550
RANDOM = rand(1,1)
if RANDOM < avgej(jj)
break
else
t_e = t+e + jj./v_se
end
end
x_out = jj;
t_out = t_in + t_e;
if 2300 < x_out < 2550
exit_e(i) = t_out(rd_e);
else
eion(i) = [x_out(rd_e),t_out(rd_e)];
E(x_out(rd_e),t_out(rd_e),i); %call the recursive function again
end
%After recursive instance of the algorithm is unwound, the data
%associated with that instance is cleared to avoid conflation between
%seperate Monte Carlo trials
clear x_out(rd_e) t_out(r_e)
%Recursively calling instances increases rd_e, so when each instance
%completes the depth counter decrements
rd_e = rd_e - 1;
end
for i = 1 : 1000
output = E(t_in,x_in,i); %E is the recursive function
end
Essentially, I am having issues with the eion(i) line and how to properly store this data and what data type to use. Any suggestions would be great help.
  5 Comments
Molly
Molly on 24 Aug 2021
  • Technically, both. Basically one electron is injected, lets call it e_1. e_1 hits a second electron e_2, then the whole process starts over again where t_in and x_in for e_2 is actually t_out and x_out for e_1. You still want to track where e_1 goes and what it does until it reaches the end of the active region (2300 < x_out < 2550). This goes for n number of electrons (e_n lets suppose) until t_in for e_n electron is at the end of the active region and therefore cannot go on hitting other electrons anymore.
  • I am not sure how well this would work with a while loop, but I guess it could. I suppose I did not want to use one because I wanted to be able to track all the electron events concurrently.
Stephen23
Stephen23 on 24 Aug 2021
Edited: Stephen23 on 25 Aug 2021
Recursion is most useful in situations where:
  • the total number of computation "branches" is not known in advance, and/or
  • the depth of each branch is independent (e.g. these might both be due to the branch depending on the data itself)
If you think that your case matches those basic conditions, then by all means use recursion. If the number of branches is known (even if just by some limit) then it might be easier to use a loop... something for you to consider.
Assuming that you want to use recursion, start by looking at some examples of simple recursive functions. Really track exactly how their data values change with each recursive function call (e.g. by printing variables to the command window), understand why those changes happen and how the stopping conditions work. Look at how they (most likely) select subsets of their data to pass to the recursive function calls. Pay attention to how they return and collect the output data from those recursive function calls.
Plan how your function should work: forget about MATLAB, do it on paper. Paper is how you can see your algorithm, scribble down your ideas, and write out examples with concrete data values. Use lots of paper. More paper!

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!