How can I use a vector as a problem-dependent variable for fittype

1 view (last 30 days)
General task description: Programatically fit a series of experimental data sets using a custom equation describing the data as a superposition of two curves.
What's the issue: Vectors are not being recognized in the problem argument of fittype. I have seen on the help file that the program argument is a cell array with one element per problem constant. Is there a way to input a vector to be used in the fit equation? I also seem to be having the same issue passing the X vector into the fit equation. As you will see, I rely on knowing the length of X to construct the fit vector.
Notes: The two curves used to fit the data are named I_1 and I_2 below. The scaling constants are named sc_1 and sc_2. A third scaling constant is used to scale the entire superposition and is named sc_tot. As you will see, "abc_Superpos.m" is run first which then calls the other m files, also shown below.
The Code:
abc_Superpos.m %below are the contents of this m file
% The first part of this code is included to have sample data to run the rest of the code. My real curves are from experimental data, not calculated from a function.
clear all X = (1:31); num_X_points = 31; num_curves = 5;
for dat_r = 1:num_curves for dat_c = 1:length(X) I_1_all_data(dat_r,dat_c) = X(dat_c)^2; I_2_all_data(dat_r,dat_c) = exp(-X(dat_c)); end end I_mix_all_data = 3*I_1 + I_2;
for incr = 1:num_curves
I_1(incr,:) = I_1_all_data(incr,:); %component 1
I_2(incr,:) = I_2_all_times(incr,:); %component 2
I_mix(incr,:) = I_mix_all_data(incr,:); %mix
abc_Superpos_cf_ = abc_Superpos_fcn(X,I_mix,I_1,I_2);
end
----------------------------------
abc_Superpos_fcn(X,Y,I_1,I_2).m %below are the contents of this m file
function [abc_Superpos_cf_] = abc_Superpos_fcn(X,Y,I_1,I_2)
X = X(:);
Y = Y(:);
ex_ = false(length(X),1); ex_([]) = 1; ex_ = ex_ | (Y <= 0);
fo_ = fitoptions('Weight',Y,'Algorithm', 'Trust-Region', 'method','NonlinearLeastSquares',... 'Lower',[0 0 0],'Upper', [1000 1000 1000], 'MaxFunEvals', 10000,'MaxIter',10000,... 'TolFun', 1e-10, 'TolX', 1e-10, 'DiffMinChange', 1e-10, 'DiffMaxChange', 1e-7); ok_ = isfinite(X) & isfinite(Y) & isfinite(Y); st_ = [1 1 1]; set(fo_,'Startpoint',st_);
ft_ = fittype('abc_EQN_Superpos_fcn(I_1,I_2,sc_1,sc_2,sc_tot,X)',... 'dependent',{'Y'},'independent',{'X'},... 'coefficients',{'sc_1','sc_2','sc_tot'}, 'problem',{'I_1', 'I_2'});
if sum(~ex_(ok_))<2
error('Not enough data left to fit ''%s'' after applying exclusion rule ''%s''.','fit 1','nozeros')
else
abc_Superpos_cf_ = fit(X(ok_),Y(ok_),ft_,fo_);
end
if 0 cv_ = { 0.1011653947332, 7.519747020586, 0.0006793571606672}; abc_Superpos_cf_ = cfit(ft_,cv_{:}); end
----------------------------------------------
abc_EQN_Superpos_fcn.m %below are the contents of this m file
function [abc_EQN_Superpos_cf_] = abc_EQN_Superpos_fcn(I_1,I_2,sc_1,sc_2,sc_tot,X)
I_em_specfit_superpos = zeros(x_range); for X_index = 1:x_range I_em_specfit_superpos(X_index) = (sc_1*I_1(X_index) + sc_2*I_2(X_index)); end
abc_EQN_Superpos_cf_ = sc_tot * I_em_specfit_superpos;

Answers (0)

Community Treasure Hunt

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

Start Hunting!