Fittype with anonymous function of multiple parameters and variables

Dear Matlab users,
I'm new to Matlab so I apologize if the solution to my problem is obvious.
I want to do two fit in a row, the second fit taking as input two values calculated through the first fit.
I have been reading about the anonymous functions in order to give as input to the second fit two variables and not parameters to optimize, I don't know what I'm missing but it does not work...
My first fit :
data = readtable("SF.xlsx");
tab_0 = data(data.C == 0,:);
[xData, yData] = prepareCurveData( tab_0.Dose, tab_0.SF );
ft0 = fittype( 'exp(-(a*x+b*x*x))', 'independent', 'x', 'dependent', 'y' );
opts.StartPoint = [0.678735154857773 0.757740130578333];
[ModelFit0,fitresult0, gof0] = fit( xData, yData, ft0, opts );
Now I want my second fit to optimize parameter gamma according to variables x and y and formula 'exp(-(alpha*x+beta*x*x))*exp(-(gamma*x*y))' with
alpha = ModelFit0.a;
beta = ModelFit0.b;
I tried
alpha = ModelFit0.a;
beta = ModelFit0.b;
ft0 = fittype( @(gamma,x,y) exp(-(alpha*x+beta*x*x))*exp(-(gamma*x*y)), 'independent', 'x', 'dependent', 'y' );
but it seems to be a problem between coefficients and parameters...
Has someone an idea how to solve this issue?
Many thanks for your help!

2 Comments

You haven't shown us the result of running the code, so we don't know what problem you're seeing.
Please provide copy-pastes of any error messages and attach the .xlsx input file. Ideally, you would just run the code for us here, using the forum's Matlab engine.
Dear Matt J,
Thank you for your response and sorry, the error message is the following :
So I tried to add 'coefficients','gamma' to the fittype arguments but it seems that my model is not a valid matlab expression:
Here is my entire code :
data = readtable("SF.xlsx");
tab_0 = data(data.C == 0,:);
% first fit
[xData, yData] = prepareCurveData( tab_0.Dose, tab_0.SF );
ft0 = fittype( 'exp(-(a*x+b*x*x))', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [0.678735154857773 0.757740130578333];
[ModelFit0,fitresult0, gof0] = fit( xData, yData, ft0, opts );
% second fit
alpha = ModelFit0.a;
beta = ModelFit0.b;
[xData, yData, zData] = prepareSurfaceData( data.Dose, data.C, data.SF );
ftC = fittype( @(gamma,x,y) exp(-(alpha*x+beta*x*x))*exp(-(gamma*x*y)),'coefficients','gamma', 'independent', 'x', 'dependent', 'y' );
Error using fittype/testCustomModelEvaluation
Expression exp(-(alpha*x+beta*x*x))*exp(-(gamma*x*y)) is not a valid MATLAB expression, has non-scalar coefficients, or cannot be evaluated:
Error while trying to evaluate FITTYPE function :

Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the matrix individually, use TIMES (.*) for elementwise multiplication.

Error in fittype>iCreateFittype (line 373)
testCustomModelEvaluation( obj );

Error in fittype (line 330)
obj = iCreateFittype( obj, varargin{:} );

Caused by:
Error using fittype/evaluate>I_ERROR_FCN_
Error while trying to evaluate FITTYPE function :

Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the matrix individually, use TIMES (.*) for elementwise multiplication.
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [0.840717255983663,0,0];
[ModelFitC,fitresultC, gofC] = fit( [xData, yData], zData, ftC, opts);

Sign in to comment.

 Accepted Answer

alpha=1;
beta=2;
ft0 = fittype( @(gamma,x,y) exp(-(alpha.*x+beta*x.^2)).*exp(-(gamma*x.*y)),...
'coefficients','gamma',...
'independent', {'x','y'},'dependent','z' )
ft0 =
General model: ft0(gamma,x,y) = exp(-(alpha.*x+beta*x.^2)).*exp(-(gamma*x.*y))

2 Comments

It works, thank you so much for your help !
Can you maybe explain to me why we have to use the .* operator in this case?
You're welcome, but please Accept-click the answer to indicate to the public that it worked.
The .* and .^ are needed because the fit() command will pass in the dependent variables x,y in vector form, with the expectation that the model will return a corresponding vector of responses. If you don't use element-wise multiplication, it will assume you meant matrix multiplication instead.

Sign in to comment.

More Answers (0)

Asked:

on 3 Jul 2023

Commented:

on 11 Jul 2023

Community Treasure Hunt

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

Start Hunting!