Clear Filters
Clear Filters

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

4 views (last 30 days)
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
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
Spottswood on 2 Oct 2016
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)

Walter Roberson
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
Spottswood on 4 Oct 2016
So how do I fix this? I'm very lost and my teacher is of no use
Walter Roberson
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);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!