How to use the formula extracted from a curve fitting algorithm

Hello everybody,
Actually I'm using the code generated by the curve fitting toolbox because it's an easy way for fit.
x = linspace(0,10,100);
y = sin(2*pi*10.*x);
plot(x,y)
[fitresult, gof, xData, yData] = createFit_sin(x, y);% call of the fitting function
fit=fitresult;
% extracting equation loop
eq = formula(fitresult); %Formula of fitted equation
parameters = coeffnames(fitresult); %All the parameter names
values = coeffvalues(fitresult); %All the parameter values
for idx = 1:numel(parameters)
param = parameters{idx};
l = length(param);
loc = regexp(eq, param); %Location of the parameter within the string
while ~isempty(loc)
eq = [eq(1:loc-1) num2str(values(idx)) eq(loc+l:end)];%Substitute parameter value
loc = regexp(eq, param);
end
end
eq
function [fitresult, gof, xData, yData] = createFit_sin(x, y)
%%Fit: 'untitled fit 1'.
[xData, yData] = prepareCurveData( x, y );
% Set up fittype and options.
ft = fittype( 'sin1' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.Lower = [-Inf 0 -Inf];
opts.StartPoint = [1 0.628318530717959 -1.16676298065496e-14];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
end
Normally if you take a look over the "eq" variable the loop that I'm using extract me the formula and the coefficient values, and the problem is how to use this equation i have tried it with "anonymous function", "inline" but I do not succeed to use it properly because the expression "eq" is converted to char and I don't succed to holding it. I'm already using it through the use of the coefficient but i have to write the equation for each fit, it might be some way to make it simplier.
Thanks in advance.

 Akzeptierte Antwort

dpb
dpb am 19 Jun. 2017
It's not clear to me why you can't just use the returned fit structure for whatever it is that you needs must want to do, but to create an anonymous function handle from the equation, use something similar to
>> ft=fit([1:10]',[linspace(0,10,10)]','poly1');
>> str2func(['@(x)' formula(ft)])
ans =
@(x)p1*x+p2
>>
NB: the prefixing of the returned formula string with the handle and dummy argument list is required in recent releases of Matlab--early versions would try to impute that from the string but that feature now errors.
NB2: do NOT use eq for a variable name; that aliases the builtin name for the '==' operator which could wreak havoc elsewhere in code if were need for it. Something like eqn would be short, meaningful abbreviation and not conflict.

2 Kommentare

Thanks!! That's exactly what I needed. I didn't knew "str2func" function. Have a nice day
NB: Embedding the text strings returned for coefficients into the functional form as you've done has potential for roundoff effects if the values aren't converted with enough precision. I've no idea if that will happen or not; if I were to undertake such a mission (and as noted, I don't see what can be gained by doing this that can't be done more straightforwardly with the fit object itself) I think I'd use the formula with variables identified in the string that would be embedded from the variable values themselves when the anonymous function is created; that will embed the actual double representation of the value as stored in memory rather than the string representation of the value.
Or, you could build the argument list to include both the independent variables and the coefficients and pass the saved coefficients when called altho that requires keeping those as well as the function handle.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Get Started with Curve Fitting Toolbox finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 19 Jun. 2017

Kommentiert:

dpb
am 21 Jun. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by