Recursive line dysfunction and indexing question
1 view (last 30 days)
I think this would fall under recursive function dysfunction but I'm in the weeds right now so I might be off with my title...
Anyway, I'm trying to get the Serum equation to start at later and later time intervals until the final predicted Serum(98) no longer exceeds the measured Serum_blood. The columns are individual people and the rows represent time (6mo intervals between 1970 and 2018). So in the first iteration of the for-loop, the simulation starts at t=2, then if the final predicted Serum(98) is greater than the measured Serum_blood, then start the loop again at t=3 (startpt=startpt+1), and repeat until the final predicted value is no longer greater than the measured value. The code runs...but it's taking a full day for it to run through one complete loop... this will ultimately be fit into a Monte Carlo simulation with 10k runs, so I'm hoping there is a way to speed up the code! I've done a tic/toc of the rest of the code (not shared) and it takes only fractions of a second. Below is the problem segment of the larger code. Matlab seems to get bogged down in the while-loop and the startpt=startpt+1....but I don't know how else to tell matlab to iterate through a for-loop at later and later starting points based on whether or not a condition is met. Is there
Also, for the output of the model, I'm trying to save WHEN in time the final iteration of the for-loop began for each person (column). For example, if the model had to iterate 15 times in the while-loop before Serum(98) no longer exceeded the measured Serum_blood, then I'd want to save that time, being 1986 (16 times steps after the model initially began. Essentially, I'm trying to back out the start of human exposure to a contaminant. That is what the last row (StartExposure(column)=startpt;) is hopefully doing? Is this indexed properly?
Thank you thank you for any and all help with this!!
for column = (1:ncolumn)
for t = (startpt:timesteps)
Serum(t,column) = Serum(t-1,column) + (I(t,column) * Ea / Vd) - (Ke(t,column) * Serum(t-1,column));
for j = startpt:timesteps
Serum(j,column) = Serum(j-1,column) + (I(j,column) * Ea / Vd) - (Ke(j,column) * Serum(j-1,column));