MATLAB: How to code two function that is separated by time

functionprogramming

Hi,
I try to code the attached equation but unable to get it work correctly. My goal is to plot those two function in one figure.
Here's my original code:
%calculate the plasma pharmacokinetics for continous injection
% I used term... to simplify the long formula
Dd = 50;
Td = 3600;
A1 = 74.6;
alpha1 = 2.43*(10^(-3));
A2 = 2.49;
alpha2 = 2.83*(10^(-4));
A3 = 0.552;
alpha3 = 1.18*(10^(-5));
t = 0:32400;
term1 = (A1/alpha1)*(1-(exp(-alpha1*t)));
term2 = (A2/alpha2)*(1-(exp(-alpha2*t)));
term3 = (A3/alpha3)*(1-(exp(-alpha3*t)));
term4 = (A1/alpha1).*((exp(-alpha1*t))-1).*(exp(-alpha1*t));
term5 = (A2/alpha2).*((exp(-alpha2*t))-1).*(exp(-alpha2*t));
term6 = (A3/alpha3).*((exp(-alpha3*t))-1).*(exp(-alpha3*t));
if (t<Td)
Cv=(Dd/Td).*(term1+term2+term3);
elseif (t>Td)
Cv=(Dd/Td).*(term4+term5+term6);
end
%calculate free doxorubicin concentration in plasma (Cfp)
s = 0.25;
Cfp = s*Cv;
figure(1);plot (t/3600,Cfp); hold on
%calculate bound doxorubicin concentration in plasma (Cfp)
Cbp = (1-s)*Cv;
plot (t/3600,Cbp);
ylabel ('Concentration Doxorubicin in Plasma for Continuous Injection ');
xlabel ('Time (hours)');
legend('Cfp=free doxorubicin','Cbp=bound doxorubicin');

Best Answer

  • If you wanted to use an if ... else construct you would have to do that in a for loop. Furthermore, you would not be using an elseif, just an else as at the moment you're missing the case where t == Td.
    In any case, you don't need an if ... else and you don't want to use a for loop as that would slow your code for no benefit. The way to do it is by using a logical array for indexing:
    Cv = zeros(size(t)); %Initialise Cv
    Cv(t<Td) = (Dd/Td).*(term1(t<Td) + term2(t<Td) + term3(t<Td));
    Cv(t>=Td) = (Dd/Td).*(term4(t>=Td) + term5(t>=Td) + term6(t>=Td));
    %the logical indices are t<Td and t>=Td respectively