Help! Input arguments for a called upon function not working!
7 次查看(过去 30 天)
显示 更早的评论
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 个评论
John D'Errico
2016-10-2
You are using the curve fitting toolbox, combined in some strange way with fminsearch? Is there a good reason why?
回答(1 个)
Walter Roberson
2016-10-2
You are calling
fitter(x,y,fitobject)
but you define fitter() to require 4 input arguments, with the 4th of them being pin
6 个评论
Walter Roberson
2016-10-5
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);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Fit Postprocessing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!