Help! Input arguments for a called upon function not working!
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
John D'Errico
el 2 de Oct. de 2016
You are using the curve fitting toolbox, combined in some strange way with fminsearch? Is there a good reason why?
Respuestas (1)
Walter Roberson
el 2 de Oct. de 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 comentarios
Walter Roberson
el 5 de Oct. de 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);
Ver también
Categorías
Más información sobre Get Started with Curve Fitting Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!