Convert an SFIT object to anonymous function - Confusing Parameters
14 views (last 30 days)
Show older comments
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
0 Comments
Answers (5)
Steven Lord
on 7 Mar 2017
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.
0 Comments
Prashant Arora
on 7 Mar 2017
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)
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!