MATLAB: How to fit multiple datasets with one ore more formulas with the same parameters

MATLAB: How to fit multiple datasets with one ore more formulas with the same parameters

fitting multiple data set with the same parameterslsqcurvefit

I have multiple data with the same Xdata (size 1×7) and different Ydata (Y1,Y2,Y3) all with size 1×7. I have to fit this data to one formula of the form:
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 1×42 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:
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)
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.

Best Answer

  • The problem is that your ‘x’ is (1×7) and you have configured ‘Y’ to be (1×42). The easiest way to solve this is to configure ‘x’ to be (7×1) and ‘Y’ to be (7×6), 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.
  • Add a Comment

    Your email address will not be published. Required fields are marked *