Help! Input arguments for a called upon function not working!
Show older comments
I start with two two scripts, one with the function I'm trying to graph and get the value of x for a y, and the other that will help with fitting. The code for the two is here:
f = fittype(@(a,b,c,d,e, x) real((a.*sqrt(1-(x./b).^2)+(a./2).* ...
sqrt(1-(x./(2.*b)).^4)+c.*(e).^(-abs(x)./d) + e)));
coeffs = coeffnames(f);
pin = [-5,20,-275,50,20];
[fitobject,gof] = fit(x,y,f,'StartPoint', pin);
fitter(x,y,fitobject)
And the other is here:
if true
% code
function [pout, yfit, res] = fitter(x,y,f, pin)
% x - x data points vector
% y - y data points vector
% f - handle to function f(x,p) to be fitted, i.e. model
% pin - function/model parameters initial guess vector
%
% pout - parameters vector providing the best fit
% yfit - y values calculated by the functions f(x,pout)
% res - residuals (y-yfit)
function yfit=yfitted(x,p)
f_of_x=@(x) f(x,p);
yfit=arrayfun(f_of_x,x);
end
function res=residuals(x,p)
yfit = yfitted(x,p);
res = y-yfit;
end
% our merit function sum of residuals squared
function E=chi_sq(p)
res=residuals(x,p);
E = sum( (res).^2 );
end
% the main job of fitting
% (i.e. optimization/best parameter finding)
% is done by fminsearch
pout=fminsearch(@chi_sq, pin); %fit is complete
yfit=yfitted(x,pout);
res=residuals(x,pout);
end
end
So now, the problem is that I when I run the first function, which calls upon the other one. It says there are not enough input arguments. But I can't figure out why because all the inputs are, well, inputted. The exact error occurs on line 30 of the function 'fitter' saying "pout=fminsearch(@chi_sq,pin); which causes an error when called upon in the fittype function.
2 Comments
John D'Errico
on 2 Oct 2016
You are using the curve fitting toolbox, combined in some strange way with fminsearch? Is there a good reason why?
Spottswood
on 2 Oct 2016
Answers (1)
Walter Roberson
on 2 Oct 2016
You are calling
fitter(x,y,fitobject)
but you define fitter() to require 4 input arguments, with the 4th of them being pin
6 Comments
Spottswood
on 2 Oct 2016
Walter Roberson
on 2 Oct 2016
fitter(x,y,fitobject) is an explicit call to fitter(), and you are only passing in 3 arguments. It does not matter that there is a variable named pin available in that workspace that matches the name of the parameter: MATLAB does not match by name, only by position.
You need to call
fitter(x, y, fitobject, pin)
The pin will not be used by the fitting itself: it will sit idle until you use it in the fminsearch() call.
Spottswood
on 4 Oct 2016
Edited: Walter Roberson
on 4 Oct 2016
Walter Roberson
on 4 Oct 2016
fitobject will be a function of one argument, x, but in the line
f_of_x=@(x) f(x,p);
you are calling it with two arguments.
Spottswood
on 4 Oct 2016
Walter Roberson
on 5 Oct 2016
Get rid of most of that stuff. Replace it with
function pout = do_the_fit(x, y, pin)
f = @(a,b,c,d,e) real((a .* sqrt(1-(x./b).^2) + (a./2) .* sqrt(1-(x./(2.*b)).^4) + c .* (e) .^ (-abs(x)./d) + e));
resid = @(v) sum((f(v(1), v(2), v(3), v(4), v(5)).^2 - y).^2);
pout = fminsearch(resid, pin);
Categories
Find more on Spline Postprocessing 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!