Storing output of function file in workspace

3 views (last 30 days)
I am attemtping to fit a system of differntial equations to empirical data in the file below. In doing this however, the workspace does not store any variables used throughout the function. I am seeking, in the code below, to have the variable Cfit stored in the workspace for later usage. Thank you in advance.
function ParameterEstimates
function C=kinetics(theta,t)
c0=[0;0;1;18632080.08];
[T,Cv]=ode15s(@DifEq,t,c0);
function dC=DifEq(t,c)
dcdt=zeros(4,1);
dcdt(1)= 20.*c(3)-0.33.*c(1);
dcdt(2)= 20.*c(3)-theta(1).*c(2)-theta(2).*c(4).*c(2);
dcdt(3)= 0.02.*c(1)+0.02.*c(2)-40.*c(3);
dcdt(4)= theta(3).*c(4)-0.01.*c(4);
dC=dcdt;
end
C=Cv;
end
t = [
0.3,
0.4,
0.5,
0.6,
0.7,
0.8,
0.9,
1,
1.1,
1.2,
1.3,
1.4,
1.5,
1.6,
1.7,
1.8,
1.9,
2,
2.1,
2.2,
2.3,
2.4,
2.5,
2.6,
2.7,
2.8,
2.9,
3,
3.1,
3.2,
3.3,
3.4,
3.5,
3.6,
3.7,
3.8,
3.9,
4,
4.1,
4.2,
4.3,
4.4,
4.5,
4.6,
4.7,
4.8,
4.9,
5,
5.1,
5.2,
5.3,
5.4,
5.5,
5.6,
5.7,
5.8,
5.9,
6];
Liver = [1348.29506832772;2005.14924465480;3254.48028488375;5124.72602075486;7829.13399421537;12328.2869399683;18834.1405923453;31035.1405834789;37215.9680323785;43957.3958719222;51919.9890262101;63209.6174575039;75798.1777562693;89528.5191319988;107358.640668647;130703.005426235;154378.995580650;185124.463958724;235847.504315023;319220.389375715;406684.943474129;526015.488352822;680360.307874421;907037.705438451;1173183.04804904;1517421.43993357;1962667.15198536;2500426.68428754;2738116.52311521;2953362.53283321;3137679.85208163;3333500.28045675;3595549.98787599;3878199.67831042;4120235.43661992;4377376.48920232;4721486.32323604;5092646.95512564;5410475.54007316;5748139.58785559;6200006.43137868;6687394.98234579;7104750.69204894;7548153.26643603;8141520.95463497;8781533.85537407;9329583.30669371;9911836.16785702;10530427.0286059;11358234.1390003;12251115.9996602;13015699.7017134;13828000.5135698;14915033.1171122;16087518.4135442;17091529.3055869;18158199.8644633;19512934.2423026]
Spleen = [5296.69267490515;10227.3087507503;19289.1179170183;37685.2809190182;73625.9897448358;138861.593923331;261898.581983822;493951.316193815;632183.081414251;762935.125052513;953763.591782546;1151026.92014362;1389089.47590313;1676389.61586326;2023110.96075868;2499597.23736815;3016579.51143867;3640487.28042792;3555935.91071807;3275173.79878704;3052232.23024447;2844466.32094523;2682173.17053339;2499597.23736815;2302239.29364786;2170883.31625475;2023110.96075868;1885397.49183905;1599424.62057788;1325314.76389575;1124293.98711175;953763.591782546;771952.194224899;654864.213353809;555535.875329819;460327.975433029;381436.833112652;323581.348079842;268125.882529280;227457.148844088;188475.476517165;158020.308745250;132486.296784471;112391.071847443;95343.8456474215;77168.9037907287;65464.0971997401;55534.6494722643;46017.1049869811;38130.6802376093;32347.1040114341;26803.4479174991;22737.9609345979;18841.1225760795;15612.1255088756;13244.1132580966;11235.2758049230;9201.02479406369]
Blood = [0;0;0;0;0;0;0;0;0;0;52.2014133300000;101.212603400000;100.332610600000;158.349065400000;168.029595700000;211.000025000000;236.931893500000;262.698760300000;305.810229500000;315.198283900000;373.497283200000;372.632137200000;421.480608400000;451.089518400000;473.650283900000;519.348867100000;522.308799600000;583.994896000000;583.601187900000;730.737827100000;1243.55573200000;1759.05491500000;2273.82061800000;2788.50042700000;3302.89718600000;3816.87852900000;4330.43718800000;4855.66067000000;5361.42520000000;5872.68671900000;6390.09304900000;6906.84082300000;7417.25482000000;7946.45088800000;8445.36977700000;8986.19744300000;9478.10745500000;10020.6616800000;10516.9369500000;11049.6286300000;11557.1411400000;12076.6875300000;12593.0145500000;13108.4890300000;13622.8341400000;14136.5140900000;14649.9922200000;15163.7253500000]
Tcell = [18632080.0800000;18632080.0800000;18456466.6800000;18456466.6800000;18456466.6800000;18456466.6800000;18456466.6800000;18456466.6800000;18368660;18280853.2800000;18280853.2800000;18280853.2800000;18280853.2800000;18280853.2800000;18105239.8800000;18105239.8800000;18105239.8800000;18193046.5800000;19246726.9900000;20124794;21178474.4100000;22319961.5100000;23110221.8200000;24251708.9300000;25393196.0400000;26183456.3500000;27324943.4500000;28115203.7600000;29256690.8700000;30310371.2800000;31188438.2900000;32242118.6900000;33120185.7000000;34173866.1100000;35315353.2200000;36105613.5300000;37247100.6400000;38388587.7400000;39178848.0500000;40320335.1600000;41110595.4700000;42252082.5800000;43305762.9800000;44183829.9900000;45237510.4000000;46115577.4100000;47169257.8200000;48398551.6300000;49188811.9300000;50330299.0400000;51383979.4500000;52262046.4600000;53315726.8700000;54193793.8700000;55247474.2800000;56388961.3900000;57179221.7000000;58320708.8100000]
c = [Liver, Spleen, Blood, Tcell];
theta0=[1;1;1];
[theta,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat]=lsqcurvefit(@kinetics,theta0,t,c);
fprintf(1,'\tRate Constants:\n')
for k1 = 1:length(theta)
fprintf(1, '\t\tTheta(%d) = %8.5f\n', k1, theta(k1))
end
tv = linspace(min(t), max(t));
Cfit = kinetics(theta, tv);
figure(1)
plot(t, c, '+')
hold on
hlp = plot(tv, Cfit);
hold off
grid
xlabel('Time')
ylabel('Concentration')
legend(hlp, 'Liver(t)', 'Spleen(t)', 'Blood(t)', 'Tcell(t)', 'Location','N')
end

Accepted Answer

Voss
Voss on 27 Jun 2022
Have the ParameterEstimates function return whatever outputs you need. In this case, I made it return Cfit.
Then assign the output(s) to variable(s) when you call ParameterEstimates.
% Assign the return value from ParameterEstimates to the variable iCfit:
% (If this function call happens in the base workspace (e.g., by calling
% from the command line), then iCfit is a variable in the base workspace
% after the function returns. In general, iCfit is a variable in whatever
% workspace ParameterEstimates is called from.)
iCfit = ParameterEstimates()
Local minimum possible. lsqcurvefit stopped because the size of the current step is less than the value of the step size tolerance. Rate Constants: Theta(1) = -2009.02825 Theta(2) = 1.69324 Theta(3) = 0.20508
iCfit = 100×4
1.0e+07 * 0 0 0.0000 1.8632 0.0000 0.0000 0.0000 1.8843 0.0000 0.0000 0.0000 1.9055 0.0000 0.0000 0.0000 1.9271 0.0000 0.0000 0.0000 1.9488 0.0000 0.0000 0.0000 1.9708 0.0000 0.0000 0.0000 1.9931 0.0000 0.0000 0.0000 2.0156 0.0000 0.0000 0.0000 2.0384 0.0000 0.0000 0.0000 2.0614
function Cfit = ParameterEstimates % define ParameterEstimates to return Cfit
function C=kinetics(theta,t)
c0=[0;0;1;18632080.08];
[T,Cv]=ode15s(@DifEq,t,c0);
function dC=DifEq(t,c)
dcdt=zeros(4,1);
dcdt(1)= 20.*c(3)-0.33.*c(1);
dcdt(2)= 20.*c(3)-theta(1).*c(2)-theta(2).*c(4).*c(2);
dcdt(3)= 0.02.*c(1)+0.02.*c(2)-40.*c(3);
dcdt(4)= theta(3).*c(4)-0.01.*c(4);
dC=dcdt;
end
C=Cv;
end
t = [
0.3,
0.4,
0.5,
0.6,
0.7,
0.8,
0.9,
1,
1.1,
1.2,
1.3,
1.4,
1.5,
1.6,
1.7,
1.8,
1.9,
2,
2.1,
2.2,
2.3,
2.4,
2.5,
2.6,
2.7,
2.8,
2.9,
3,
3.1,
3.2,
3.3,
3.4,
3.5,
3.6,
3.7,
3.8,
3.9,
4,
4.1,
4.2,
4.3,
4.4,
4.5,
4.6,
4.7,
4.8,
4.9,
5,
5.1,
5.2,
5.3,
5.4,
5.5,
5.6,
5.7,
5.8,
5.9,
6];
Liver = [1348.29506832772;2005.14924465480;3254.48028488375;5124.72602075486;7829.13399421537;12328.2869399683;18834.1405923453;31035.1405834789;37215.9680323785;43957.3958719222;51919.9890262101;63209.6174575039;75798.1777562693;89528.5191319988;107358.640668647;130703.005426235;154378.995580650;185124.463958724;235847.504315023;319220.389375715;406684.943474129;526015.488352822;680360.307874421;907037.705438451;1173183.04804904;1517421.43993357;1962667.15198536;2500426.68428754;2738116.52311521;2953362.53283321;3137679.85208163;3333500.28045675;3595549.98787599;3878199.67831042;4120235.43661992;4377376.48920232;4721486.32323604;5092646.95512564;5410475.54007316;5748139.58785559;6200006.43137868;6687394.98234579;7104750.69204894;7548153.26643603;8141520.95463497;8781533.85537407;9329583.30669371;9911836.16785702;10530427.0286059;11358234.1390003;12251115.9996602;13015699.7017134;13828000.5135698;14915033.1171122;16087518.4135442;17091529.3055869;18158199.8644633;19512934.2423026];
Spleen = [5296.69267490515;10227.3087507503;19289.1179170183;37685.2809190182;73625.9897448358;138861.593923331;261898.581983822;493951.316193815;632183.081414251;762935.125052513;953763.591782546;1151026.92014362;1389089.47590313;1676389.61586326;2023110.96075868;2499597.23736815;3016579.51143867;3640487.28042792;3555935.91071807;3275173.79878704;3052232.23024447;2844466.32094523;2682173.17053339;2499597.23736815;2302239.29364786;2170883.31625475;2023110.96075868;1885397.49183905;1599424.62057788;1325314.76389575;1124293.98711175;953763.591782546;771952.194224899;654864.213353809;555535.875329819;460327.975433029;381436.833112652;323581.348079842;268125.882529280;227457.148844088;188475.476517165;158020.308745250;132486.296784471;112391.071847443;95343.8456474215;77168.9037907287;65464.0971997401;55534.6494722643;46017.1049869811;38130.6802376093;32347.1040114341;26803.4479174991;22737.9609345979;18841.1225760795;15612.1255088756;13244.1132580966;11235.2758049230;9201.02479406369];
Blood = [0;0;0;0;0;0;0;0;0;0;52.2014133300000;101.212603400000;100.332610600000;158.349065400000;168.029595700000;211.000025000000;236.931893500000;262.698760300000;305.810229500000;315.198283900000;373.497283200000;372.632137200000;421.480608400000;451.089518400000;473.650283900000;519.348867100000;522.308799600000;583.994896000000;583.601187900000;730.737827100000;1243.55573200000;1759.05491500000;2273.82061800000;2788.50042700000;3302.89718600000;3816.87852900000;4330.43718800000;4855.66067000000;5361.42520000000;5872.68671900000;6390.09304900000;6906.84082300000;7417.25482000000;7946.45088800000;8445.36977700000;8986.19744300000;9478.10745500000;10020.6616800000;10516.9369500000;11049.6286300000;11557.1411400000;12076.6875300000;12593.0145500000;13108.4890300000;13622.8341400000;14136.5140900000;14649.9922200000;15163.7253500000];
Tcell = [18632080.0800000;18632080.0800000;18456466.6800000;18456466.6800000;18456466.6800000;18456466.6800000;18456466.6800000;18456466.6800000;18368660;18280853.2800000;18280853.2800000;18280853.2800000;18280853.2800000;18280853.2800000;18105239.8800000;18105239.8800000;18105239.8800000;18193046.5800000;19246726.9900000;20124794;21178474.4100000;22319961.5100000;23110221.8200000;24251708.9300000;25393196.0400000;26183456.3500000;27324943.4500000;28115203.7600000;29256690.8700000;30310371.2800000;31188438.2900000;32242118.6900000;33120185.7000000;34173866.1100000;35315353.2200000;36105613.5300000;37247100.6400000;38388587.7400000;39178848.0500000;40320335.1600000;41110595.4700000;42252082.5800000;43305762.9800000;44183829.9900000;45237510.4000000;46115577.4100000;47169257.8200000;48398551.6300000;49188811.9300000;50330299.0400000;51383979.4500000;52262046.4600000;53315726.8700000;54193793.8700000;55247474.2800000;56388961.3900000;57179221.7000000;58320708.8100000];
c = [Liver, Spleen, Blood, Tcell];
theta0=[1;1;1];
[theta,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat]=lsqcurvefit(@kinetics,theta0,t,c);
fprintf(1,'\tRate Constants:\n')
for k1 = 1:length(theta)
fprintf(1, '\t\tTheta(%d) = %8.5f\n', k1, theta(k1))
end
tv = linspace(min(t), max(t));
Cfit = kinetics(theta, tv);
figure(1)
plot(t, c, '+')
hold on
hlp = plot(tv, Cfit);
hold off
grid
xlabel('Time')
ylabel('Concentration')
legend(hlp, 'Liver(t)', 'Spleen(t)', 'Blood(t)', 'Tcell(t)', 'Location','N')
end

More Answers (2)

Jan
Jan on 27 Jun 2022
Nested nested functions - brrr.
What about: t = 0.3:0.1:6; ?
You store the variable Cfit in the workspace of the function ParameterEstimates . There it is available. In which workspace do you search this variable? In the "base" workspace, which is the workspace of the conmmand window? Then:
% In the command window:
Cfit = ParameterEstimates
% In the file ParameterEstimates.m :
function Cfit = ParameterEstimates
...
end

yatharth
yatharth on 27 Jun 2022
Hey since your cfit variable is wrapped inside the ParameterEstimates function it cannot be saved to the workspace unless you specify it as the return variable ... you can simply define your ParameterEstimates function like mentioned below and your cfit variable will show up in workspace
function Cfit = ParameterEstimates
function C = kinetics(theta,t)
c0=[0;0;1;18632080.08];
[T,Cv]=ode15s(@DifEq,t,c0);
.
.
.
end

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by