How can I fit a function with many constants and coefficients?
1 view (last 30 days)
Show older comments
Hello,
I am trying to do least-squares fitting of a messy function, which looks like this:
pixelResponseModel = p0_trans.*(Q0.*(1-percent_pol)./2 + ...
Q0.*(1-percent_pol)./2.*p0_contrast + ...
Q0.*percent_pol.*cos(p0_angle-anglesRadians).^2 + ...
Q0.*percent_pol.*sin(p0_angle-anglesRadians).^2.*p0_contrast);
Q0 and percent_pol are known constants that I would like to specify for each fit.
p0_trans, p0_contrast, and p0_angle are parameters which I want the fit to determine.
This is what I tried:
% Define the constants
Q0 = 50000;
percent_pol = 1;
% specify the dependent variable
anglesRadians = [0:180]*pi/180;
% specify the data to be fit
pixelResponseData = something; % this is a vector with data from the workspace
% I create the function using anonymous functions
modelFunction = @(p0_trans,p0_contrast,p0_angle,Q0,percent_pol,anglesRadians) p0_trans.*(Q0.*(1-percent_pol)./2 + Q0.*(1-percent_pol)./2.*p0_contrast + Q0.*percent_pol.*cos(p0_angle-anglesRadians).^2 + Q0.*percent_pol.*sin(p0_angle-anglesRadians).^2.*p0_contrast);
% I create the fit type
pixelResponseModel = fittype(modelFunction,'independent','anglesRadians','problem',{'Q0','percent_pol'},'coefficients',{'p0_trans','p0_contrast','p0_angle'});
% I run the fit
[fitobject] = fit(anglesRadians, medianPixel_11', pixelModel, 'problem', {'Q0', 'percent_pol'}, 'StartPoint', [50000, 1, 0.5, 0.1, 0] );
And I get the error:
Error using fit>iFit (line 367)
Too many inputs to FITTYPE function.
Error in fit (line 108)
[fitobj, goodness, output, convmsg] = iFit( xdatain, ydatain, fittypeobj, ...
Caused by:
Failure in initial user-supplied objective function evaluation. LSQCURVEFIT cannot
continue.
I don't think I'm using too many inputs...How else can I do this fit? I appreciate any help!
0 Comments
Answers (0)
See Also
Categories
Find more on Linear and Nonlinear Regression 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!