Skip to content

Commit

Permalink
Updates transient function
Browse files Browse the repository at this point in the history
  • Loading branch information
ckreutz committed Dec 12, 2023
1 parent 65434b3 commit 0ec58a9
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
% WriteTextSummary(fit, folder)
%
% fit a fit or cell array of fits as generated by
% arConditon2NewDataStruct*.m
%
% folder [WriteTextSummary]
%
% This function write matlab code for simulating the dynamics based on
% fitted transient functions in the specified folder.
%
% Example:
% arLoad
% fits = arApproximateTimeCoursesByTransientFunction2;
% WriteTextSummary(fits)
% t = linspace(0,100,101);
% plot(t,feval(fits{1}.label,t));

function WriteTextSummary(fit,folder)
if ~exist('folder','var') || isempty(folder)
folder = 'WriteTextSummary';
end
if ~isdir(folder)
mkdir(folder);
end

if iscell(fit)
for i=1:length(fit)
if isfield(fit{i},'pLabel') % otherwise fitting failed
WriteTextSummary(fit{i});
else
fprintf('WriteTextSummary.m: It seems that fit{%i} fitting was failed.\n',i)
end
end

else
if isfield(fit,'label')
name = sprintf('%s',fit.label);
else
name = sprintf('Model%i_%s_condition%i',fit.m, fit.x, fit.c);
end
name = strrep(name,' ','_');
filename = [folder,filesep,name,'.m'];
fid = fopen(filename,'w');

fprintf(fid,'%s This function can be used to approximate model%i, condition%i, dynamic variable %s\n\n','%',fit.m, fit.c, fit.x);
fprintf(fid,'function [out, outSD] = %s(t)\n',name);
for i=1:length(fit.data.p)
if strcmp('init____dummy___',fit.data.p{i})~=1 % omit this D2D dummy parameter
if strcmp(fit.data.p{i},fit.data.fp{i})~=1
fprintf(fid,'%s = %s;\n',fit.data.p{i},fit.data.fp{i});
else
ind = strmatch(fit.data.p{i},fit.pLabel,'exact');
fprintf(fid,'%s = %f;\n',fit.data.p{i},fit.p(ind));
end
end
end
fprintf(fid,'\n');
for i=1:length(fit.data.fu)
fprintf(fid,'%s = %s;\n',fit.u{i},Replacements(fit.data.fu{i}));
end
fprintf(fid,'\n');
for i=1:length(fit.data.fy)
fprintf(fid,'out = %s;\n',Replacements(fit.data.fy{i}));
end
fprintf(fid,'\n');
fprintf(fid,'outSD = NaN(size(out));\n');
fprintf(fid,'outSD(:) = %f;\n',fit.approxErr);
fprintf(fid,'\n');





fclose(fid);
end


function out = Replacements(in)
out = strrep(in,'*','.*');
out = strrep(out,'/','./');
out = strrep(out,'^','.^');

Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
% load tmp fits % if specific fits should only be fitted
%%
for d=1:length(fits)
% try
try
arInit;
arLoadModel('TransientFunction_ForConditionFit2');
% fits{d}.data.fp{4} = '(100)' % for bachmann and conditions with too short tExp
Expand Down Expand Up @@ -156,17 +156,22 @@
plotFitTransient(fits(d),plotfolder);
end

% catch ERR
% pcatch{end+1} = struct('p',ar.p,'lb',ar.lb,'ub',ar.ub);
% save error ok pcatch
% end
catch ERR
pcatch{end+1} = struct('p',ar.p,'lb',ar.lb,'ub',ar.ub);
save error ok pcatch
end
end
save arApproximateTimeCoursesByTransientFunction2
arPlot


%% calculate the approximation error from the fitted SD
for i=1:length(fits)
fits{i}.approxErr = 10.^fits{i}.p(4)./range(fits{i}.data.yExp);
if(isfield(fits{i},''))
fits{i}.approxErr = 10.^fits{i}.p(4)./range(fits{i}.data.yExp);
else
fits{i}.approxErr = NaN*range(fits{i}.data.yExp);
end
end


Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
% This function calls arSetPars but also translates info (e.g. bounds) into
% ar.fit_transient
function arSetParsTransient(pLabel, varargin)
global ar
if length(varargin)>0
p = varargin{1};
else
p = [];
end
if length(varargin)>1
qFit = varargin{2};
else
qFit = [];
end
if length(varargin)>2
qLog10 = varargin{3};
else
qLog10 = [];
end
if length(varargin)>3
lb = varargin{4};
else
lb = [];
end
if length(varargin)>4
ub = varargin{5};
else
ub = [];
end

% translate bounds to fit_transient.bounds
if(isfield(ar,'fit_transient'))
ind = strmatch(pLabel,ar.fit_transient.bounds.pLabel,'exact');
if ~isemtpy(lb)
ar.fit_transient.bounds.lb(ind) = lb;
end
if ~isemtpy(ub)
ar.fit_transient.bounds.ub(ind) = ub;
end
end
% arSetPars(pLabel, [p], [qFit], [qLog10], [lb], [ub], [type], [meanp], [stdp])
arSetPars(pLabel_or_ar, varargin{:})

0 comments on commit 0ec58a9

Please sign in to comment.