How can I fit multiple datasets with one ore more formulas with the same parameters?

2 views (last 30 days)
I have multiple data with the same Xdata (size 1x7) and different Ydata (Y1,Y2,Y3) all with size 1x7. I have to fit this data to one formula of the form:
x=a/(((1+(sqrt((b.^2).*Ypa+(Ype.^2)))./c.^d)
The totaal Ydata is combined in one matrix and then divided in parallel and perpendicular direction for the three different angles. This matrix has of length of 1x42 Y=[Y1pa Y2pa Y3pa Y1pe Y2pe Y3pe].. I have used several methods to try to find the values of the parameters so it will fits to all the three measurement data sets, but I have problems with the dimensions of the matrix. I try to solve it the following way:
mf=@(C,Y)(C(1)./(((1+(sqrt((C(2)^2).*Y(1:21))+(Y(22:42).^2)))./C(3)).^C(4)));
R1=lsqcurvefit(mf,[280,0.25,1,0.7],Xtotaal,Ytotaal);
a=R1(1);
b=R1(2);
c=R1(3);
d=R1(4);
I have to fit this formula for the three data sets (X,Y1),(X,Y2) and (X,Y3). So I used the vectors Itotaal=[X X X] and Ytotaal=[Y1 Y2 Y3]. But when running this code following error occurs:
Index exceeds matrix dimensions.
Error in Final_fit_try>@(C,Y)(C(1)./(((1+(sqrt((C(2)^2).*Y(1:21))+(Y(22:42).^2)))./C(3)).^C(4)))
Error in lsqcurvefit (line 199)
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Error in Final_fit_try (line 23)
R1=lsqcurvefit(mf,[280,0.25,1,0.7],Xtotaal,Ytotaal);
Caused by:
Failure in initial user-supplied objective function evaluation. LSQCURVEFIT cannot continue.
What must be the size of the vectors Xtotaal and Ytotaal? I tried different sizes but the error still occurs.

Accepted Answer

Star Strider
Star Strider on 23 Dec 2016
The problem is that your ‘x’ is (1x7) and you have configured ‘Y’ to be (1x42). The easiest way to solve this is to configure ‘x’ to be (7x1) and ‘Y’ to be (7x6), and then change your ‘mf’ function to reflect those changes.
I do not have your data, so you will have to experiment with this:
Xtotaal = x(:);
Ytotaal = [Y1pa(:) Y2pa(:) Y3pa(:) Y1pe(:) Y2pe(:) Y3pe(:)];
mf = @(C,Y)(C(1)./(((1+(sqrt((C(2)^2).*Y(:,1:3))+(Y(:,4:6).^2)))./C(3)).^C(4)));
NOTE: This is UNTESTED CODE but should work. If it does not work, attach your data here (preferably as a ‘.mat’ file). If you are not familiar with them, see the documentation for the save function and its friends for details.
  10 Comments
Nikky
Nikky on 24 Dec 2016
Thank you! This is the way to produce the results. Unfortunately the fitting results are not very good (see the fitting results in the matlab file).
Star Strider
Star Strider on 24 Dec 2016
My pleasure!
The important thing is that my code works!
I cannot control the success of the fit. That is dependent on the initial parameter estimates and your data.
That is the reason I suggested a more robust parameter estimation approach, the easiest being patternsearch. Your function quite likely has many local minima in its hyperplane, and the one it finds first depends on where it starts. You want it to find the global minimum, that should provide the best fit. You can guess randomly at different initial parameter estimates, or use patternsearch, or their friends, such as ga and GlobalSearch to estimate the parameters.
If my Answer solves your problem, please Accept it!

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!