function parameter_estimation_pH_single(filename) % estimates the parameter of the pH shift using either a exponetial % function or a hyperbolic tangent % % filename refers to the Matlab file (mat--format) that stores the data in % a vector called time (time points) and a vector pH % options and preparations clc close all % type of shift experiment type=1; % forward shift 5.7 -> 4.5 %type=0; % reverse shift 4.5 -> 5.7 % load data load(filename,'time','pH'); % estimate parameters foptions=fitoptions('Method','NonlinearLeastSquares','Lower',[0.15, 115],'Upper',[1.2, 150],'StartPoint',[0.4, 150]); if type==1; foptions2=fitoptions('Method','NonlinearLeastSquares','Lower',[0.05, 115],'Upper',[0.5, 150],'StartPoint',[0.1, 130]); f=fittype('0.6*(1-tanh(a/2*(x-b)))+4.5','options',foptions); f2=fittype('5.7*heaviside(b2-x)+(4.5+1.2*exp(-a2*(x-b2)))*heaviside(x-b2)','options',foptions2); [c2,gof2] = fit(time,pH,f); par_pH=coeffvalues(c2); conf_pH=confint(c2); [c3,gof3] = fit(time,pH,f2); par_pH2=coeffvalues(c3); conf_pH2=confint(c3); else foptions2=fitoptions('Method','NonlinearLeastSquares','Lower',[0.05, 115],'Upper',[0.7, 150],'StartPoint',[0.2, 130]); f=fittype('0.6*(tanh(a/2*(x-b))-1)+5.7','options',foptions); f2=fittype('4.5*heaviside(b2-x)+(4.5+1.2*(1-exp(-a2*(x-b2))))*heaviside(x-b2)','options',foptions2); [c2,gof2] = fit(time,pH,f); par_pH=coeffvalues(c2); conf_pH=confint(c2); [c3,gof3] = fit(time,pH,f2); par_pH2=coeffvalues(c3); conf_pH2=confint(c3); end % vector of parameters and confidence intervals data_tanh=[par_pH(1) conf_pH(:,1)' par_pH(2) conf_pH(:,2)' gof2.sse gof2.rsquare gof2.dfe gof2.adjrsquare gof2.rmse]; data_exp=[par_pH2(1) conf_pH2(:,1)' par_pH2(2) conf_pH2(:,2)' gof3.sse gof3.rsquare gof3.dfe gof3.adjrsquare gof3.rmse]; display('Parameters: exponential'); display(data_exp); display('Parameters: hyperbolic tangent'); display(data_tanh); % compute time-dependent pH value tm=linspace(0,300,500); if type==1 pH_m=0.6*(1-tanh(par_pH(1)/2*(tm-par_pH(2))))+4.5; save('./pH_sim.mat','tm','pH_m'); pH_m2=5.7*heaviside(par_pH2(2)-tm)+(4.5+1.2.*exp(-par_pH2(1)*(tm-par_pH2(2)))).*heaviside(tm-par_pH2(2)); else pH_m=0.6*(tanh(par_pH(1)/2*(tm-par_pH(2)))-1)+5.7; pH_m2=4.5*heaviside(par_pH2(2)-tm)+(4.5+1.2.*(1-exp(-par_pH2(1)*(tm-par_pH2(2))))).*heaviside(tm-par_pH2(2)); end % comparison of experimental data and fit figure plot(time,pH,'LineStyle','none','Marker','o','MarkerFaceColor',[0 0 1],'Color',[0 0 1]); xlabel('time [hours]'); ylabel('pH'); hold on plot(tm,pH_m,tm,pH_m2); legend('experimental data','hyperbolic tangent','exponential'); hold off figureprop;