Help! Input arguments for a called upon function not working!

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

You are using the curve fitting toolbox, combined in some strange way with fminsearch? Is there a good reason why?
It was written up by my professor and given to help fit this function with our data. This combination has worked before but I'm not sure why now it is saying input arguments. So to answer your question I am not sure why he is using the curve fitting toolbox combined with fminsearch

Sign in to comment.

Answers (1)

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

yes but fitobject calls the pin, no? I am really not good at Matlab. I understand that the function calls for 4 input arguments, but pin is well defined in the script and a part of the fitobject, which is called in the function. It has worked with this code before and now it is saying input argument error
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.
Well I added the pin to the inputs and got even more errors that I don't know what they mean. Here is the full list of them:
Error using cfit/subsref>iParenthesesReference (line 36)
Too many inputs to CFIT/SUBSREF.
Error in cfit/subsref (line 15)
out = iParenthesesReference( obj, currsubs );
Error in fitter>@(x)f(x,p) (line 12)
f_of_x=@(x) f(x,p);
Error in fitter/yfitted (line 13)
yfit=arrayfun(f_of_x,x);
Error in fitter/residuals (line 17)
yfit = yfitted(x,p);
Error in fitter/chi_sq (line 23)
res=residuals(x,p);
Error in fminsearch (line 189)
fv(:,1) = funfcn(x,varargin{:});
Error in fitter (line 30)
pout=fminsearch(@chi_sq, pin1); %fit is complete
Error in LabReport5 (line 23)
fitter(x,y,fitobject,pin1)
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.
So how do I fix this? I'm very lost and my teacher is of no use
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);

Sign in to comment.

Categories

Asked:

on 2 Oct 2016

Commented:

on 5 Oct 2016

Community Treasure Hunt

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

Start Hunting!