How can I fit a custom function that contains workspace vector variables?

3 visualizaciones (últimos 30 días)
% data values
X = [-7.2460 -7.3355 -7.4320 -7.5752 -7.6996 -7.8780 -8.1560 -8.3517 -8.6170 -8.8883 -9.1808 -9.6629 -10.5808 -13.6249 -21.4783] ;
Y = [ -14.5516 -12.5218 -15.2762 -13.5768 -11.7304 -13.1678 -13.5329 -13.7761 -12.5162 -10.0737 -12.3137 -12.0351 -9.6663 -9.0393 -4.9973];
%fit
customfit = fit(X, Y, @(s, x) fun (s, X));
fittedfunc = fun(customfit.s, X);
figure;
plot(X, Y, 'r')
hold on
plot(X, fittedfunc, 'b')
function y = fun(s, x)
gme = 1.0e-04 *[0.0000 0.1978 0.2404 0.2862 0.2871 0.3603 0.4394 0.5141 0.5789 0.7762 0.7014 0.7476 0.8268 0.5400 0.0216];
y = log(((gme./x).^2))+log(s);
end
this is the code that does not work. I have to use that specific funcion with those specific values of "gme" and I'm interested in fitting it with those data and find the "s" parameter. Matlab's answer is:
Error using fit>iFit (line 127)
X must be a matrix with one or two columns.
Error in fit (line 108)
[fitobj, goodness, output, convmsg] = iFit( xdatain, ydatain, fittypeobj, ...
Error in Untitled (line 6)
customfit = fit(X, Y, @(s, x) fun (s, X));
Anybody knows how to solve this problem?? Thanks a lot!!

Respuestas (1)

darova
darova el 5 de Mayo de 2020
Try following recommendations
  2 comentarios
Matteo Masto
Matteo Masto el 5 de Mayo de 2020
Error using fittype/testCustomModelEvaluation (line 16)
Custom equations must produce an output vector, matrix, or array that is the same size and shape as the input data. This custom equation fails to meet that requirement:
fun(s,x)
Error in fittype>iCreateFittype (line 371)
testCustomModelEvaluation( obj );
Error in fittype (line 328)
obj = iCreateFittype( obj, varargin{:} );
Error in fit>iFit (line 157)
model = fittype( fittypeobj, 'numindep', size( xdatain, 2 ) );
Error in fit (line 108)
[fitobj, goodness, output, convmsg] = iFit( xdatain, ydatain, fittypeobj, ...
Error in pratica (line 50)
customfit = fit(X(:), Y(:), @(s, x)fun (s, x));
it says this.. :(
darova
darova el 5 de Mayo de 2020
I changed your code
% data values
gme = 1.0e-04 *[0.0000 0.1978 0.2404 0.2862 0.2871 0.3603 0.4394 0.5141 0.5789 0.7762 0.7014 0.7476 0.8268 0.5400 0.0216];
X = [-7.2460 -7.3355 -7.4320 -7.5752 -7.6996 -7.8780 -8.1560 -8.3517 -8.6170 -8.8883 -9.1808 -9.6629 -10.5808 -13.6249 -21.4783] ;
Y = [ -14.5516 -12.5218 -15.2762 -13.5768 -11.7304 -13.1678 -13.5329 -13.7761 -12.5162 -10.0737 -12.3137 -12.0351 -9.6663 -9.0393 -4.9973];
gme = flip(gme);
X = flip(X);
Y = flip(Y);
f1 = griddedInterpolant(X,gme);
f2 = @(s,x) log(((f1(x)./x).^2))+log(s);
% F = fit(X(:), Y(:), f2);
plot(X, Y, 'r')
hold on
plot(X, f2(10,X), 'b')
hold off
Blue curve is the curve you want to fit
But only thing you are changing is s, it only moves the curve up and down. Are you sure your fit function is correct?

Iniciar sesión para comentar.

Categorías

Más información sobre Interpolation en Help Center y File Exchange.

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by