Using the output of one m-file in Matlab as the input to another? Establishing the connection

I have created the functions but dont know how to proceed? like how to establish the connection?
Let's say that:
1) the first function produces a concentration profile with respect to time for a given period of let's say 1 hour of exposure: C(t)
2) The second function computes dosage as D(c,t)= C^a * delta t, where C is found in 1 and a is a constant
Dosage is computed for 4 different time intervals so that the final outcome is D1 (for the first time interval, say the first 10 minutes), D2(for the second time interval, and is accumulative such that D2 = D1+ C2*deltat2),...etc
Also, how can I write the code for the dosage such that it's computed not only once but 4 times, each time for a given time interval
Hope my question is clear, and sorry for the so many questions. I haven't used Matlab since 2008-2009.
Thanks in advance.

 Réponse acceptée

A for loop would be appropriate.
Save the concentration profile that you output from the first function as either a vector of concentrations, or if it computes its own times, a matrix of times and concentrations. Index it using the index variable (loop counter) of your loop.
Then use whatever information you need from the vector or matrix computed by the first function as an input argument to the second function. Save those outputs as well, indexed by the index variable (loop counter) of your loop. If the length of the output changes between iterations, use a cell array to store the results.
The previous data you computed will be available in the next iteration if you need them, since you have saved them and they are still in your workspace.
When the loop finishes, I would save all the results in a .mat file so I can use them later, without having to recompute everything.

4 commentaires

I will try with the loops once I finalize the concentration profile. You have mentioned about saving the output of the concentration function as a vector and then using that as input to the dosage function, is it possible to link the first function somehow with the second one such that it automatically uses the output the first function? I'm asking because I might do some changes in the original function and as such the output and hence the vector needed for the next stage would change in terms of values.
Thanks for responding.
My pleasure!
If you want the concentration ‘trough’ just prior to the next dosing interval, you can most accurately specify it by using end. To illustrate:
v = 1:10;
ve = v(end);
I would not use min because the first concentration might be the minimum, depending on how you are calculating the concentration profile. (I usually use ode45 and the appropriate differential equations with initial conditions and kinetic parameters varying as necessary.)
A necessarily non-specific illustration to outline the idea:
for k1 = 1:4
[T{k1},C{k1}] = conc_profile(parameters);
D{k1} = dose_calc(C{k1});
end
Here, ‘T’ is a time vector, and ‘C’ is the concentration vector (or matrix if you are doing compartmental analysis, with one column for each compartment). The ‘D’ variable is the calculated dosage, and the funciton that calculates it chooses the required value of ‘C{k1}’ internally. I use cell arrays for flexibility.
The saved cell arrays are available in the next iteration as well if you need to refer to them. (I am not certain how you are doing your calculations.)
Also, if you are not using a differential equation solver and want to do cumulative integration of a vector, use the cumtrapz function.

Connectez-vous pour commenter.

Plus de réponses (1)

Given the function (let's call it f1) and the vector t over which to compute...
C=f1(t); % the concentration
D=trapz(t,C); % the dose integral

3 commentaires

Hi dpb,
I have tried using trapz to compute the dosage for the profile I'm having but it gives one value at the end for the whole profile (whole duration). I want it to be computed for individual time intervals in the profile, say for the first 10 mins, then for then next 30 mins following the first 10 mins and so on...
Thx
doc cumsum
From the example for trapz
>> y=reshape(0:5,2,3);
>> dt=0.1;
>> t=0:dt:0.2;
>> trapz(t,y,2)
ans =
0.4000
0.6000
>> cumsum(y,2)/(3/2)*dt
ans =
0 0.1333 0.4000
0.0667 0.2667 0.6000
>>

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by