pade function in Octave?

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

pade function in Octave?

vivek guajri
Hello,

May I know if there is a pade and minerr function in Octave? If yes, what
package do I have to load for that!

Thank you,

Vivek Gujari.



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


Reply | Threaded
Open this post in threaded view
|

Re: pade function in Octave?

Doug Stewart-4


On Thu, Oct 31, 2019 at 1:14 PM vivek guajri <[hidden email]> wrote:
Hello,

May I know if there is a pade and minerr function in Octave? If yes, what
package do I have to load for that!

Thank you,

Vivek Gujari.



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




--
DASCertificate for 206392



Reply | Threaded
Open this post in threaded view
|

Re: pade function in Octave?

vivek guajri
Thank you Doug for your response!
I would like to fit some data to a curve using pade approximation. I have
used polyfit and it worked reasonably well, but I think pade may give the
most accurate fit equation.
This is the data I would like to fit -

2.35504, 0.87524, 0.76141, 0.87769, 3.08380, 1.01227, 0.77118, 4.92401,
0.68878 , 0.87189 , 2.60315   0.70099 ,  0.85419 ,  9.17908, 1.07910 ,
0.80322 ,  2.39624 ,  3.36853 ,  1.19812 ,  0.66193 ,  0.78033

When I use this data as an argument to padecoef, I get the following
exception -

error: padecoef: T must be a non-negative scalar
error: called from
    padecoef at line 92 column 5
    padefunction at line 12 column 12

Do I have to convert the data to some form?
Please let me know.

Thank you,

Vivek.



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


Reply | Threaded
Open this post in threaded view
|

Re: pade function in Octave?

Doug Stewart-4


On Thu, Oct 31, 2019 at 4:19 PM vivek guajri <[hidden email]> wrote:
Thank you Doug for your response!
I would like to fit some data to a curve using pade approximation. I have
used polyfit and it worked reasonably well, but I think pade may give the
most accurate fit equation.
This is the data I would like to fit -

2.35504, 0.87524, 0.76141, 0.87769, 3.08380, 1.01227, 0.77118, 4.92401,
0.68878 , 0.87189 , 2.60315   0.70099 ,  0.85419 ,  9.17908, 1.07910 ,
0.80322 ,  2.39624 ,  3.36853 ,  1.19812 ,  0.66193 ,  0.78033

When I use this data as an argument to padecoef, I get the following
exception -

error: padecoef: T must be a non-negative scalar
error: called from
    padecoef at line 92 column 5
    padefunction at line 12 column 12

Do I have to convert the data to some form?
Please let me know.

Thank you,

Vivek.



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


I need to see your code. 


--
DASCertificate for 206392



Reply | Threaded
Open this post in threaded view
|

Re: pade function in Octave?

vivek guajri
Hi Doug,

Here is the code -

input = [-0.0036621,
    2.3550415,
    0.8752441,
   66.8194580,
    0.7614136,
    0.8776855,
    3.0838013,
    1.0122681,
    0.7711792,
    4.9240112,
    0.6887817,
    0.8718872,
    2.6031494
    0.7009888,
   18.9779663,
   -0.0021362,
   -0.0030518,
    0.8541870,
    9.1790771,
    1.0791016,
   80.2191162
   -0.0125122,
    0.8032227,
    2.3962402,
    3.3685303,
   -0.0018311,
   -0.0054932,
    1.1981201,
    0.6619263,
    0.7803345];

j = 1;
AreaT = 0;
for i = 1:size(input)
  if(input(i) > 0 && input(i) < 10)
    AreaT(j) = input(i);
    j++;
  end
end

[num, den] = padecoef(AreaT);




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


Reply | Threaded
Open this post in threaded view
|

Re: pade function in Octave?

Doug Stewart-4


On Thu, Oct 31, 2019 at 5:54 PM vivek guajri <[hidden email]> wrote:
Hi Doug,

Here is the code -

input = [-0.0036621,
    2.3550415,
    0.8752441,
   66.8194580,
    0.7614136,
    0.8776855,
    3.0838013,
    1.0122681,
    0.7711792,
    4.9240112,
    0.6887817,
    0.8718872,
    2.6031494
    0.7009888,
   18.9779663,
   -0.0021362,
   -0.0030518,
    0.8541870,
    9.1790771,
    1.0791016,
   80.2191162
   -0.0125122,
    0.8032227,
    2.3962402,
    3.3685303,
   -0.0018311,
   -0.0054932,
    1.1981201,
    0.6619263,
    0.7803345];

j = 1;
AreaT = 0;
for i = 1:size(input)
  if(input(i) > 0 && input(i) < 10)
    AreaT(j) = input(i);
    j++;
  end
end

[num, den] = padecoef(AreaT);




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

padecoef  is not curve fitting code. 
 
It just approximates a delay in the input data.
I don't know what you are trying to do, So I can't help you

--
DASCertificate for 206392



Reply | Threaded
Open this post in threaded view
|

Re: pade function in Octave?

vivek guajri
Okay. Thank you for letting me know.
I thought it is similar to polyfit function where it fits the data to a
polynomial equation.

Thank you.



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


Reply | Threaded
Open this post in threaded view
|

Re: pade function in Octave?

Dmitri A. Sergatskov
On Thu, Oct 31, 2019 at 7:15 PM vivek guajri <[hidden email]> wrote:
>
> Okay. Thank you for letting me know.
> I thought it is similar to polyfit function where it fits the data to a
> polynomial equation.

If you want to fit the data to orthogonal polynomials than you want to
use something like Chebyshev
or Legendre polinomial fit. Pade is to approximate functions not data.

Dmitri.
--


Reply | Threaded
Open this post in threaded view
|

Re: pade function in Octave?

vivek guajri
Thank you Dmitri for your response! I do not want to fit orthogonal
polynomials!
I have some data and would like to fit it to a polynomial equation. The
polyfit function is fine, but I need something which can fit better than
that. Maybe a combination of polyfit and lsqin will do the job!



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


Reply | Threaded
Open this post in threaded view
|

RE: pade function in Octave?

Windhorn, Allen E [ACIM/LSA/MKT]
Vivek,

> -----Original Message-----
> From: Help-octave <help-octave-
> bounces+allen.windhorn=[hidden email]> On Behalf Of vivek
> guajri
>
> I have some data and would like to fit it to a polynomial equation. The
> polyfit function is fine, but I need something which can fit better than
> that. Maybe a combination of polyfit and lsqin will do the job!

The polyfit function fits the BEST POSSIBLE polynomial to your data, as long
as you want the "best" in a least-square sense, so it's no use trying to find
a better polynomial.  If you want "best" in a minimax sense then Chebyshev
is the way to go.  But generally most data isn't well-fit by a high-order
polynomial -- the higher the order, the more it wants to shoot off to infinity,
and real data generally don't go there.  So you probably want to fit another
type of function instead of a polynomial.
You may have been on the right track with a Pade approximant, which fits a
rational function, but apparently the Pade package is for some specialized
purpose and not suitable for function fitting.  You can use leasqr to fit any
function including rational functions.  It's in the optim package.

The data you provided had only Y values -- are the X values equally spaced?
Plotting the data, it's very "spiky" and probably hopeless to fit any kind of
reasonable function to it, certainly not a polynomial.  What are you trying
to predict?

Regards,
Allen

Reply | Threaded
Open this post in threaded view
|

RE: pade function in Octave?

vivek guajri
Hi Allen,

Thank you for your email. I have attached the excel file (includes x and y
values and pdf file of the fit and observed curve)
  x&y.xlsx <https://octave.1599824.n4.nabble.com/file/t373519/x%26y.xlsx>  
  curvefitting.pdf
<https://octave.1599824.n4.nabble.com/file/t373519/curvefitting.pdf>  -

   



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


Reply | Threaded
Open this post in threaded view
|

RE: pade function in Octave?

vivek guajri
In the plot you can see that there are some negative slopes in the fit
function. How do I make it non-decreasing?



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


Reply | Threaded
Open this post in threaded view
|

Re: pade function in Octave?

Octave - General mailing list
In reply to this post by vivek guajri
On 11/1/19 1:56 PM, vivek guajri wrote:
> Hi Allen,
>
> Thank you for your email. I have attached the excel file (includes x and y
> values and pdf file of the fit and observed curve)
>    x&y.xlsx <https://gcc01.safelinks.protection.outlook.com/?url=https%3A%2F%2Foctave.1599824.n4.nabble.com%2Ffile%2Ft373519%2Fx%2526y.xlsx&amp;data=02%7C01%7Cprzemek.klosowski%40nist.gov%7C052da038cec047eabf0808d75ef4c953%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C1%7C637082277749877919&amp;sdata=DDhJqUN9oL9mHlip9pcIqw1gFKyNpz2ruqPxXlXYtUo%3D&amp;reserved=0>
>    curvefitting.pdf
> <https://gcc01.safelinks.protection.outlook.com/?url=https%3A%2F%2Foctave.1599824.n4.nabble.com%2Ffile%2Ft373519%2Fcurvefitting.pdf&amp;data=02%7C01%7Cprzemek.klosowski%40nist.gov%7C052da038cec047eabf0808d75ef4c953%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C1%7C637082277749877919&amp;sdata=JWvH%2BDTY1CPTHBbIL7%2FplIxk7uwY4QlhhJ2MJg6jSWU%3D&amp;reserved=0>  -

But the data in the .pdf are completely different from the data in your
previous email---the former look like they may be fitted by a polynomial
of a reasonably low degree, but the latter aren't. You of course can fit
any data if you allow high order polynomials, but it's almost always a
bad idea because such polynomials will behave crazily between and
outside your data points.

Even the data from your .xslx spreadsheet look funny---there are
discontinuities in the x values (between point 14 and 15, and between 19
and 20) that don't look real but suggest some sort of measurement error.
They could be fit by a third-order polynomial but it looks wrong.

What is the data, what are the uncertainities of the data, and what is
the physical model that you expect to predict the data? If you can
answer these questions, then the fitted parameters of this model will
have a physical meaning. Otherwise it'll be just more-less random numbers.



Reply | Threaded
Open this post in threaded view
|

Re: pade function in Octave?

James Sherman
In reply to this post by vivek guajri

On Fri, Nov 1, 2019 at 1:57 PM vivek guajri <[hidden email]> wrote:
In the plot you can see that there are some negative slopes in the fit
function. How do I make it non-decreasing?



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



Hi Vivek,

There is no standard way to do this, and I can't see any straight forward way to do so, even if you set this up as a kind of constrained optimization, for example even if you were fitting a 3rd degree polynomial ax^3 + bx^2 + cx^1 + dx^0

Then you could put a constraint that the first derivative is positive or zero:
3ax^2 + 2bx^1 + cx^0  >= 0

But this would still only be a constraint at the values for x in your data set, and not for all positive(?) values for x that you seem to want it for.

Hope this helps,

James Sherman Jr.


Reply | Threaded
Open this post in threaded view
|

Re: pade function in Octave?

vivek guajri
In reply to this post by Octave - General mailing list
Yes, the data is different from the previous data I sent.
The data is from our in built neutron spectrometer. The y values are the
area values of the pulses and x values are just the normalized ascending
ordered number. I do not know the uncertainty or model of the data at this
point!



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


Reply | Threaded
Open this post in threaded view
|

Re: pade function in Octave?

Nir Krakauer-3
In reply to this post by James Sherman
PCHIP [1] is an interpolation method that is guaranteed to be preserve monotonicity. If the data has noise, you can fit a non-parametric monotone function to it using, e.g. monotone_smooth [2].



Reply | Threaded
Open this post in threaded view
|

Re: pade function in Octave?

vivek guajri
In reply to this post by James Sherman
Yes, I think I am going to just constrain the polynomial to be non-decreasing
wherever there is negative slope! That should be the closest I can get!



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


Reply | Threaded
Open this post in threaded view
|

Re: pade function in Octave?

vivek guajri
In reply to this post by Nir Krakauer-3
Reply | Threaded
Open this post in threaded view
|

Re: pade function in Octave?

Octave - General mailing list
In reply to this post by vivek guajri
On 11/1/19 3:19 PM, vivek guajri wrote:
> The data is from our in built neutron spectrometer. The y values are the
> area values of the pulses and x values are just the normalized ascending
> ordered number. I do not know the uncertainty or model of the data at this
> point!
As it happens, I am involved with neutron spectroscopy so if you explain
how you set up your experiment I may be able to suggest a way to get
some useful information from the data.


Reply | Threaded
Open this post in threaded view
|

RE: pade function in Octave?

Windhorn, Allen E [ACIM/LSA/MKT]
In reply to this post by vivek guajri
Vivek,

> -----Original Message-----
> From: Help-octave <help-octave-
> bounces+allen.windhorn=[hidden email]> On Behalf Of vivek
> guajri
>
> Yes, I think I am going to just constrain the polynomial to be non-decreasing
> wherever there is negative slope! That should be the closest I can get!

Generally there is some physical principle that determines the curve you should be fitting -- is that true in this case?

Getting off-topic a bit, but curve fitting being a hobby of mine, I used a function 0.005276*(exp(7.66x)-1)+0.66 which is guaranteed monotonic and fit pretty well.  I didn't even bother with Octave, just fit it in Excel using Solver.

Regards,
Allen

12