HELP please, Can't fit data properly

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

HELP please, Can't fit data properly

T.G.ALG
Hi, I'm trying to fit some data to a function of degree 4 or 5, but when I
plot it, it doesn't fit:

DATA:
C30 =

   0.063330   0.057200   0.052870   0.047380   0.044860   0.039710  
0.037070   0.036030   0.034990
t =

    1.0000    1.5000    2.0000    3.0000    5.0000    7.0000   10.0000  
13.0000   18.0000

FIT:
p=polyfit(t,C30,4)

   0.0000027990  -0.0001193780   0.0018221783  -0.0125401220   0.0727802050

The problem is that when I plot(t,C30) and plot(p) I get totally different
curves, am I missing somethiing???



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: HELP please, Can't fit data properly

James Sherman
On Wed, Oct 3, 2018 at 4:07 PM T.G.ALG <[hidden email]> wrote:
Hi, I'm trying to fit some data to a function of degree 4 or 5, but when I
plot it, it doesn't fit:

DATA:
C30 =

   0.063330   0.057200   0.052870   0.047380   0.044860   0.039710 
0.037070   0.036030   0.034990
t =

    1.0000    1.5000    2.0000    3.0000    5.0000    7.0000   10.0000 
13.0000   18.0000

FIT:
p=polyfit(t,C30,4)

   0.0000027990  -0.0001193780   0.0018221783  -0.0125401220   0.0727802050

The problem is that when I plot(t,C30) and plot(p) I get totally different
curves, am I missing somethiing???



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html



Hi T.G.,

Yes, I believe it is safe to say that you are missing something.  Look at vector that is returned from polyfit that you print out:
p=
   0.0000027990  -0.0001193780   0.0018221783  -0.0125401220   0.0727802050

these are the coefficients of the polynomial (see help polyfit for more explanation).  And the "curve" from plotting that vector is just the coefficients wrt their index.  The function you're probably looking for is polyval (help polyval for info) so that you can evaluate the fitted curve at the values of x that you want.

Hope this helps,
James Sherman Jr.


Reply | Threaded
Open this post in threaded view
|

Re: HELP please, Can't fit data properly

Doug Stewart-4


On Wed, Oct 3, 2018 at 4:25 PM James Sherman Jr. <[hidden email]> wrote:
On Wed, Oct 3, 2018 at 4:07 PM T.G.ALG <[hidden email]> wrote:
Hi, I'm trying to fit some data to a function of degree 4 or 5, but when I
plot it, it doesn't fit:

DATA:
C30 =

   0.063330   0.057200   0.052870   0.047380   0.044860   0.039710 
0.037070   0.036030   0.034990
t =

    1.0000    1.5000    2.0000    3.0000    5.0000    7.0000   10.0000 
13.0000   18.0000

FIT:
p=polyfit(t,C30,4)

   0.0000027990  -0.0001193780   0.0018221783  -0.0125401220   0.0727802050

The problem is that when I plot(t,C30) and plot(p) I get totally different
curves, am I missing somethiing???



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html



Hi T.G.,

Yes, I believe it is safe to say that you are missing something.  Look at vector that is returned from polyfit that you print out:
p=
   0.0000027990  -0.0001193780   0.0018221783  -0.0125401220   0.0727802050

these are the coefficients of the polynomial (see help polyfit for more explanation).  And the "curve" from plotting that vector is just the coefficients wrt their index.  The function you're probably looking for is polyval (help polyval for info) so that you can evaluate the fitted curve at the values of x that you want.

Hope this helps,
James Sherman Jr.


Polyfit can do that

[P, S] = polyfit (X, Y, N)

S.yf  should have the fitted Y values for your x values.

--
DAS



Reply | Threaded
Open this post in threaded view
|

Re: HELP please, Can't fit data properly

Przemek Klosowski-7
On 10/03/2018 05:15 PM, Doug Stewart wrote:


On Wed, Oct 3, 2018 at 4:25 PM James Sherman Jr. <[hidden email]> wrote:
On Wed, Oct 3, 2018 at 4:07 PM T.G.ALG <[hidden email]> wrote:
Hi, I'm trying to fit some data to a function of degree 4 or 5, but when I
plot it, it doesn't fit:

DATA:
C30 =

   0.063330   0.057200   0.052870   0.047380   0.044860   0.039710 
0.037070   0.036030   0.034990
t =

    1.0000    1.5000    2.0000    3.0000    5.0000    7.0000   10.0000 
13.0000   18.0000

FIT:
p=polyfit(t,C30,4)

   0.0000027990  -0.0001193780   0.0018221783  -0.0125401220   0.0727802050

The problem is that when I plot(t,C30) and plot(p) I get totally different
curves, am I missing somethiing???

Yes, I believe it is safe to say that you are missing something.  Look at vector that is returned from polyfit that you print out:
p=
   0.0000027990  -0.0001193780   0.0018221783  -0.0125401220   0.0727802050

these are the coefficients of the polynomial (see help polyfit for more explanation).  And the "curve" from plotting that vector is just the coefficients wrt their index.  The function you're probably looking for is polyval (help polyval for info) so that you can evaluate the fitted curve at the values of x that you want.

Hope this helps,
James Sherman Jr.


Polyfit can do that

[P, S] = polyfit (X, Y, N)

S.yf  should have the fitted Y values for your x values.

True, but it does not show how bad the fit is. These datapoints aren't likely to fit a polynomial model, and to see it it helps to see the actual fitted polynomial with more resolution than the 9 points provided:

C30 = [ 0.063330   0.057200   0.052870   0.047380   0.044860   0.039710  0.037070   0.036030   0.034990]
t      = [ 1.0000      1.5000       2.0000       3.0000      5.0000       7.0000    10.0000     13.0000     18.0000]

plot(t,C30,'o',x=1:.1:20,polyval(polyfit(t,C30,4),x))

which shows nicely the deficiency of the fourth-order polynomial, by creating artifacts where the data has none.



Reply | Threaded
Open this post in threaded view
|

Re: HELP please, Can't fit data properly

Doug Stewart-4


On Wed, Oct 3, 2018 at 6:19 PM Przemek Klosowski <[hidden email]> wrote:
On 10/03/2018 05:15 PM, Doug Stewart wrote:


On Wed, Oct 3, 2018 at 4:25 PM James Sherman Jr. <[hidden email]> wrote:
On Wed, Oct 3, 2018 at 4:07 PM T.G.ALG <[hidden email]> wrote:
Hi, I'm trying to fit some data to a function of degree 4 or 5, but when I
plot it, it doesn't fit:

DATA:
C30 =

   0.063330   0.057200   0.052870   0.047380   0.044860   0.039710 
0.037070   0.036030   0.034990
t =

    1.0000    1.5000    2.0000    3.0000    5.0000    7.0000   10.0000 
13.0000   18.0000

FIT:
p=polyfit(t,C30,4)

   0.0000027990  -0.0001193780   0.0018221783  -0.0125401220   0.0727802050

The problem is that when I plot(t,C30) and plot(p) I get totally different
curves, am I missing somethiing???

Yes, I believe it is safe to say that you are missing something.  Look at vector that is returned from polyfit that you print out:
p=
   0.0000027990  -0.0001193780   0.0018221783  -0.0125401220   0.0727802050

these are the coefficients of the polynomial (see help polyfit for more explanation).  And the "curve" from plotting that vector is just the coefficients wrt their index.  The function you're probably looking for is polyval (help polyval for info) so that you can evaluate the fitted curve at the values of x that you want.

Hope this helps,
James Sherman Jr.


Polyfit can do that

[P, S] = polyfit (X, Y, N)

S.yf  should have the fitted Y values for your x values.

True, but it does not show how bad the fit is. These datapoints aren't likely to fit a polynomial model, and to see it it helps to see the actual fitted polynomial with more resolution than the 9 points provided:

C30 = [ 0.063330   0.057200   0.052870   0.047380   0.044860   0.039710  0.037070   0.036030   0.034990]
t      = [ 1.0000      1.5000       2.0000       3.0000      5.0000       7.0000    10.0000     13.0000     18.0000]

plot(t,C30,'o',x=1:.1:20,polyval(polyfit(t,C30,4),x))

which shows nicely the deficiency of the fourth-order polynomial, by creating artifacts where the data has none.



You are correct. And this is very important to see what the curve looks like between the given data points.
Thanks :-)
-- 
DASCertificate for 206392



Reply | Threaded
Open this post in threaded view
|

Re: HELP please, Can't fit data properly

jbect
Le 04/10/2018 à 01:11, Doug Stewart a écrit :


On Wed, Oct 3, 2018 at 6:19 PM Przemek Klosowski <[hidden email]> wrote:
On 10/03/2018 05:15 PM, Doug Stewart wrote:


On Wed, Oct 3, 2018 at 4:25 PM James Sherman Jr. <[hidden email]> wrote:
On Wed, Oct 3, 2018 at 4:07 PM T.G.ALG <[hidden email]> wrote:
Hi, I'm trying to fit some data to a function of degree 4 or 5, but when I
plot it, it doesn't fit:

DATA:
C30 =

   0.063330   0.057200   0.052870   0.047380   0.044860   0.039710 
0.037070   0.036030   0.034990
t =

    1.0000    1.5000    2.0000    3.0000    5.0000    7.0000   10.0000 
13.0000   18.0000

FIT:
p=polyfit(t,C30,4)

   0.0000027990  -0.0001193780   0.0018221783  -0.0125401220   0.0727802050

The problem is that when I plot(t,C30) and plot(p) I get totally different
curves, am I missing somethiing???

Yes, I believe it is safe to say that you are missing something.  Look at vector that is returned from polyfit that you print out:
p=
   0.0000027990  -0.0001193780   0.0018221783  -0.0125401220   0.0727802050

these are the coefficients of the polynomial (see help polyfit for more explanation).  And the "curve" from plotting that vector is just the coefficients wrt their index.  The function you're probably looking for is polyval (help polyval for info) so that you can evaluate the fitted curve at the values of x that you want.

Hope this helps,
James Sherman Jr.


Polyfit can do that

[P, S] = polyfit (X, Y, N)

S.yf  should have the fitted Y values for your x values.

True, but it does not show how bad the fit is. These datapoints aren't likely to fit a polynomial model, and to see it it helps to see the actual fitted polynomial with more resolution than the 9 points provided:

C30 = [ 0.063330   0.057200   0.052870   0.047380   0.044860   0.039710  0.037070   0.036030   0.034990]
t      = [ 1.0000      1.5000       2.0000       3.0000      5.0000       7.0000    10.0000     13.0000     18.0000]

plot(t,C30,'o',x=1:.1:20,polyval(polyfit(t,C30,4),x))

which shows nicely the deficiency of the fourth-order polynomial, by creating artifacts where the data has none.


Depending on what you're actually trying to do with your data, non-parametric regression might be a suitable alternative.

Here is a solution using kriging (a.k.a. Gaussian process regression) :

```
pkg load stk  % assuming stk installed

C30 = [ 0.06333  0.0572  0.05287  0.04738  0.04486  0.03971   0.03707   0.03603   0.03499]';
t   = [ 1.0000   1.5000  2.00000  3.00000  5.00000  7.00000  10.00000  13.00000  18.00000]';

model = stk_model (@stk_materncov52_iso);
model.lognoisevariance = nan;  % assume noisy data
[model.param, model.lognoisevariance] = stk_param_estim (model, t, C30);

t_box = stk_hrect ([0; 20], {'t'});
t_pred = stk_sampling_regulargrid (100, [], t_box);
C30_pred = stk_predict (model, t, C30, t_pred);

stk_plot1d (t, C30, t_pred, [], C30_pred);  legend show

```

See attached figure for the result.

@++
Julien





kriging.eps (74K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

RE: HELP please, Can't fit data properly

Windhorn, Allen E [ACIM/LSA/MKT]
In reply to this post by T.G.ALG
> -----Original Message-----
> From: Help-octave [mailto:help-octave-
> bounces+allen.windhorn=[hidden email]] On Behalf Of T.G.ALG
>
> Hi, I'm trying to fit some data to a function of degree 4 or 5, but when I
> plot it, it doesn't fit:
>
> DATA:
> C30 =
>
>    0.063330   0.057200   0.052870   0.047380   0.044860   0.039710
> 0.037070   0.036030   0.034990
> t =
>
>     1.0000    1.5000    2.0000    3.0000    5.0000    7.0000   10.0000
> 13.0000   18.0000

As some others have pointed out, a polynomial isn't the best curve to
fit to these data.  You should always look for a function that curves
the way the data points curve, and your function seems to be trying
to be asymptotic to the y axis on the left and some constant ~0.03 on
the right.

You should use a nonlinear least squares curve fit like leasqr to fit a
function like u*t^(v*t)+w (I was in a hurry, so used Excel).  This
gives coefficients of u = 0.04256, v = -0.3827, w = 0.0265 for a
minimax fit.  The fit was somewhat sloppy, and the v coefficient isn't
far from -0.5, so I tried fitting with that value, and got:
C30 = 0.0384/sqrt(t)+0.0263 with a pretty good fit, but the point at
t=5.0 was throwing things off.  Assuming that was an outlier, I took
it out, and got an excellent fit with C30=0.0384/sqrt(t)+0.0254, with
maximum error around 0.5%.  

Regards,
Allen