Pass curvefit start points into function

3 views (last 30 days)
I am new to Matlab, but have somehow figured out how to create a curve fitting function that is called by the main program. I need to curve fit many sets of data. The function has the following line of code:
opts.StartPoint = [226 26 1E07 0.4];
The points are currently hard coded, but I would like to pass into the curve fitting function a set of 4 starting points for each data set. The main routine will figure out the values of the four starting points and then I would like to pass these starting points in to the function.
The function is called from the main routine as follows:
[fitresult gof]=createFit(z,y);

Accepted Answer

Voss
Voss on 27 Dec 2021
You can add an additional input argument to your createFit function:
function [fitresult gof] = createFit(z,y,startPoint) % use your existing input and output variable names
% your code
end
You can make it an optional argument by checking whether it was given, and if not, use the default:
function [fitresult gof] = createFit(z,y,startPoint) % use your existing input and output variable names
if nargin < 3
startPoint = [226 26 1E07 0.4]; % whatever default value
end
% your code
end
Then call createFit with 2 or 3 arguments:
[fitresult gof] = createFit(z,y); % no startPoint specified -> createFit will use the default defined in its function definition
[fitresult gof] = createFit(z,y,[226 26 1E07 0.4]); % explicitly using default startPoint -> same as no startPoint specified
[fitresult gof] = createFit(z,y,[0 0 0 0]); % using some other startPoint

More Answers (0)

Community Treasure Hunt

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

Start Hunting!