Convert an SFIT object to anonymous function - Confusing Parameters

14 次查看(过去 30 天)
Hello together,
I created a three dimensional polynomial fitting from a data set and saved it as an 'sfit' object. Also, I tried to create an anonymous function with the formula and the given parameters, but when I compare the two functions, the results are different.
The code I used is as follows:
form_FQH=formula(FQH_model); %Provides a string object of the formula
coeff_FQH=coeffvalues(FQH_model); %Creates an array with the parameter values
%where FQH_model is my sfit object
Using this function you get the formula and the parameters of the fitting function, but when I enter these values as follows:
p00=40;
p10=14.2605978895365;
p01=-4.32069502395558;
p20=0.883779655092065;
p11=0.293546862260376;
p02=-2.44778787502561;
p30=-0.0279238049639190;
p21=-0.188469631089263;
p12=0.381288857623379;
p03=-0.547180902615911;
p40=-0.00281359396688073;
p31=0.00929378621295907;
p22=-0.0128340523997197;
p13=-0.0799529397944919;
p04=0.202041163712288;
fqhF = @(x,y) (p00 + p10.*x + p01.*y + p20.*x.^2 + p11.*x.*y + p02.*y.^2 + p30.*x.^3 + p21.*x.^2.*y + p12.*x.*y.^2 + p03.*y.^3 + p40.*x.^4 + p31.*x.^3.*y + p22.*x.^2.*y.^2 + p13.*x.*y.^3 + p04.*y.^4);
comp1=fqhF(40,10);
comp2=FQH_model(40,10);
The results are different. I did enter everything the exact same way it was visible in the string and the array. My assumption would be that the order of the parameters in the string does not match to the array that is created.
What am I doing wrong?
Thanks in advance for your support and with best regards

回答(5 个)

Steven Lord
Steven Lord 2017-3-7
The coefficient values you typed manually are not identical to the coefficient values stored in the object. They may be very close, but they are slightly different. You can check this by subtracting your values from the appropriate elements of the vector returned by coeffvalues on your fitting object. Those small differences, particularly in the value of something like p30 or p40, can become significant when multiplied by 40^3 or 40^4.
If you must write your own function to evaluate the fit (which most of the time you don't need to do, since you can directly evaluate the fit) don't type the coefficients yourself. save them to a MAT-file then load them and use the loaded values to define your anonymous function. If that's really not an option, and you need them written into a code file, use num2hex and hex2num to convert from double precision to an exact text representation and back again.

Prashant Arora
Prashant Arora 2017-3-7
I am not able to reproduce this behavior using the code below. I am using the exact same syntax and order for polynomial fit.
x = 3 - 6 * rand( 49, 1 );
y = 3 - 6 * rand( 49, 1 );
z = peaks( x, y );
sf = fit( [x, y], z, 'poly44' );
names = coeffnames(sf);
values = coeffvalues(sf);
formula(sf)
for i=1:numel(values)
assignin('base',names{i},values(i));
end
h = @(x,y)(p00 + p10.*x + p01.*y + p20.*x.^2 + p11.*x.*y + p02.*y.^2 + p30.*x.^3 + p21.*x.^2.*y + p12.*x.*y.^2 + p03.*y.^3 + p40.*x.^4 + p31.*x.^3.*y + p22.*x.^2.*y.^2 + p13.*x.*y.^3 + p04.*y.^4);
h(10,4)
sf(10,4)

moTo
moTo 2017-3-7
编辑:moTo 2017-3-7
Hello, thanks a lot for your answer. So even if you copy these values directly from the workspace the values are not the same? Is there a possible way to upload my sfit file?
If you must write your own function to evaluate the fit (which most of the time you don't need to do, since you can directly evaluate the fit)
In this case I have to as I would like to use this sfit in a Simulink Block. But as Simulink does not allow sfit objects to be used I have to find a way around (I did also ask a question about that: https://de.mathworks.com/matlabcentral/answers/326950-using-sfit-objects-from-model-workspace-in-simulink-embedded-matlab-functions
Best regards

moTo
moTo 2017-3-7
Okay, so I tried to do it the way you explained it to me. Still, the results are different. Not slightly different, but completely (29.87 which is correct vs. -6.455e+03, which is total nonsense)
I attached the sfit and my function. Please check it yourself, I really don't know what is the error here.
Thank you again for your support and with best regards, moTo

moTo
moTo 2017-3-7
Alright, I was able to narrow down the problem. If you use the Curve Fitting Tool and select ''Center and scale'' the coefficients change and the formula does not fit to the sfit object anymore. If you uncheck this box, everything is fine. I am just asking myself if there is an explanation for that or if this is an error within Matlab.
Best regards, moTo

Community Treasure Hunt

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

Start Hunting!

Translated by