Pass curvefit start points into function
3 次查看(过去 30 天)
显示 更早的评论
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);
0 个评论
采纳的回答
Voss
2021-12-27
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
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!