## Why do my coeffvalues not produce a sensible result?

### Nicholas Turton (view profile)

on 28 Aug 2019
Latest activity Commented on by Torsten

on 28 Aug 2019

### Torsten (view profile)

I have a script that uses the fit fuction on some data using a poly55 fit type. I then use this to get the coeffvalues for the fittted curve and try to replot the surface using the coeffvalues but the result produced is orders of magnitude different from the original data.
Script attached as well as some example data.
Can anyone explain what is happening or recommend a way forward?

### Torsten (view profile)

on 28 Aug 2019

If you use the 'Normalize','on' fitting option, your polynomial has powers of (x-meanx)/stdx and (y-meany)/stdy instead of x and y.

Nicholas Turton

### Nicholas Turton (view profile)

on 28 Aug 2019
Hi Torsten,
Can you elaborate. I am new to matlab and not following fully what you mean
I have used
[Psi_d_xData, Psi_d_yData, Psi_d_zData] = prepareSurfaceData( id_ind, iq_ind, Psi_d);
[Psi_d_fitresult, Psi_d_gof] = fit( [Psi_d_xData, Psi_d_yData], Psi_d_zData, ft, 'Normalize', 'on' );% Fit model to data.
Psi_d_coeffvals = coeffvalues(Psi_d_fitresult);
which when produced numel(Psi_d_coeffvals) = 21
Using the poly55 eqation
f(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 + p50*x^5 + p41*x^4*y + p32*x^3*y^2
+ p23*x^2*y^3 + p14*x*y^4 + p05*y^5
...I have then called up each of these coeffvals as well as my desired x and y values to try and reproduce the curve but only could manage the following
Are you saying I need to calculate the mean and std values of x and y and use these in the calculation?
Torsten

### Torsten (view profile)

on 28 Aug 2019
if you switch the mentioned option off, you can use the polynomial you defined in the calculation.
Otherwise you will have to insert (x-meanx)/stdx for x and (y-meany)/stdy for y in the calculation for your polynomial value:
f(x,y) = p00 + p10*(x-meanx)/stdx + p01*(y-meany)/stdy + p20*((x-meanx)/stdx))^2 + ...

### Nicholas Turton (view profile)

on 28 Aug 2019

Thanks Torsten, it worked perfectly
function output_fit = fittingcurve(x_data, y_data, coeffvals)
x_mean = mean(x_data(1,:));
x_std = std(x_data(1,:));
y_mean = mean(y_data(:,1));
y_std = std(y_data(:,1));
output_fit = ...
coeffvals(1) ...
+ coeffvals(2).*((x_data-x_mean)./x_std) ...
+ coeffvals(3).*((y_data-y_mean)./y_std) ...
+ coeffvals(4).*((x_data-x_mean)./x_std).^2 ...
+ coeffvals(5).*((x_data-x_mean)./x_std).*((y_data-y_mean)./y_std) ...
+ coeffvals(6).*((y_data-y_mean)./y_std).^2 ...
+ coeffvals(7).*((x_data-x_mean)./x_std).^3 ...
+ coeffvals(8).*((x_data-x_mean)./x_std).^2.*((y_data-y_mean)./y_std) ...
+ coeffvals(9).*((x_data-x_mean)./x_std).*((y_data-y_mean)./y_std).^2 ...
+ coeffvals(10).*((y_data-y_mean)./y_std).^3 ...
+ coeffvals(11).*((x_data-x_mean)./x_std).^4 ...
+ coeffvals(12).*((x_data-x_mean)./x_std).^3.*((y_data-y_mean)./y_std) ...
+ coeffvals(13).*((x_data-x_mean)./x_std).^2.*((y_data-y_mean)./y_std).^2 ...
+ coeffvals(14).*((x_data-x_mean)./x_std).*((y_data-y_mean)./y_std).^3 ...
+ coeffvals(15).*((y_data-y_mean)./y_std).^4 ...
+ coeffvals(16).*((x_data-x_mean)./x_std).^5 ...
+ coeffvals(17).*((x_data-x_mean)./x_std).^4.*((y_data-y_mean)./y_std) ...
+ coeffvals(18).*((x_data-x_mean)./x_std).^3.*((y_data-y_mean)./y_std).^2 ...
+ coeffvals(19).*((x_data-x_mean)./x_std).^2.*((y_data-y_mean)./y_std).^3 ...
+ coeffvals(20).*((x_data-x_mean)./x_std).*((y_data-y_mean)./y_std).^4 ...
+ coeffvals(21).*((y_data-y_mean)./y_std).^5;