# Fitting 2 data sets simultaneously using two different equations with some shared fit parameters.

1 view (last 30 days)
Joel Patrow on 11 Dec 2019
Answered: Star Strider on 11 Dec 2019
I have a global fit that I am trying to do. Where I have two data sets. dataset A and dataset B. Both A and B are vectors. I am trying to fit them using exponential fits. The two fit expressions are as follows:
fitA = a1*exp(-t/b1) + a2*exp(b2) + c
fitB = 1-(a1*exp(-t/b1) + a2*exp(b2) + c)
The following is the code I have written for it.
global_fit_data = [A; B]; % A and B are vectors of size 1X85
t = 0:10:840;
global_fit_function = @(p) [p(3)*exp(-t/p(1))+p(4)*exp(-t/p(2))+p(5); 1-p(3)*exp(-t/p(1))-p(4)*exp(-t/p(2))-p(5)]; % Fitting functions
squared_errors = @(p) sum((global_fit_data - global_fit_function(p)).^2); % Parameter to be fit in fminsearch
options=optimset('MaxFunEvals', 1000000, 'MaxIter',1000000, 'Display', 'off', 'TolX', 1e-0012);
fit = fminsearch(squared_errors,[50 700 0.1 1.1 1.3],options);
However, everytime I run this, I get the following error:
Subscripted assignment dimension mismatch.
Error in fminsearch (line 191)
fv(:,1) = funfcn(x,varargin{:});
Is there anything obviously wrong with how I am trying to utilize fminsearch?
P.S. I am using Matlab ver 2013b
Thanks!

Star Strider on 11 Dec 2019
Try this:
global_fit_data = [A; B].'; % A and B are vectors of size 1X85
t = (0:10:840).';
global_fit_function = @(p,t) [p(3)*exp(-t/p(1))+p(4)*exp(-t/p(2))+p(5), 1-p(3)*exp(-t/p(1))-p(4)*exp(-t/p(2))-p(5)]; % Fitting functions
squared_errors = @(p) norm(global_fit_data - global_fit_function(p,t));
options=optimset('MaxFunEvals', 1000000, 'MaxIter',1000000, 'Display', 'off', 'TolX', 1e-0012);
fit = fminsearch(squared_errors,[50 700 0.1 1.1 1.3],options);
I substituted the norm function for the sum-of-squares, and that worked. I also transposed ‘global_fit_data’ and changed ‘global_fit_function’ to conform to it. (I generally use column data and functions rather than row data and functions.) It ran without error with simulated data.