Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Curve Fitting

64 views
Skip to first unread message

Bryan Lavrich

unread,
Nov 3, 2002, 3:03:25 PM11/3/02
to
I am a high school student and need to know how to a best fit curve given a
set of data. For example:

x: y:
-2 -8
-1 -.3
0 5
1 5.5
2 3.3
3 -3.5
This is one example my teacher gave us. Too bad everybody but me and 3 of
my friends use TI's.
~shudders~
We all use HP49Gs and have been at a loss the past few days.

If you know how to find the best fit curve's equation and correlation (that
should be enough) rather easily (or not), then please reply to this post or
my e-mail. This method should be applicable to polynomials at least up to
5th power.

I have checked the manual and the best I could come up with is probably a
logarithmic fit, but I am in pre-calc and have not yet covered natural logs.

Thanks in advance.

--Bryan


Veli-Pekka Nousiainen

unread,
Nov 3, 2002, 3:43:29 PM11/3/02
to
Above number key [ 5 ] says in red letters: STAT
Press the red shift key pointing right and then [ 5 ]
Select number
3. Fit data
Go into the field Model:
and press key [F2] above which the menu label says |CHOOS|
Choose the last one (by going up using the up-arrow key)
and you are done!!
VPN

"Bryan Lavrich" <bry...@twcny.rr.com> wrote in message
news:hifx9.57334$c51.18...@twister.nyroc.rr.com...

Virgil

unread,
Nov 3, 2002, 6:27:07 PM11/3/02
to
In article <hifx9.57334$c51.18...@twister.nyroc.rr.com>,
"Bryan Lavrich" <bry...@twcny.rr.com> wrote:

> I am a high school student and need to know how to a best fit curve given a
> set of data. For example:
>
> x: y:
> -2 -8
> -1 -.3
> 0 5
> 1 5.5
> 2 3.3
> 3 -3.5


> This is one example my teacher gave us. Too bad everybody but me and 3 of
> my friends use TI's.
> ~shudders~
> We all use HP49Gs and have been at a loss the past few days.

To fit a polynomial exactly, create a 2 by n matrix with the data
points as columns then use the LAGRANGE command to get a polynomial
fitting all the points.

Note that if you have created a sigmaDATA matrix for the points, you
must transpose it before the LAGRANGE command.

For your data (and in RPL mode):
Put the following on the stack
[[-2 -1 0 1 2 3][-8 -.3 5 5.5 3.3 -3.5]]

Executing XL produces
[[-2 -1 0 1 2 3][-8 -3/10 5 11/2 33/10 -7/2]]

Then executing LAGRANGE produces, in about 1/40 of a second,
'-((17*x^5-45*x^4-79*x^3+621*x^2-634*x-1200)/240)'

If you do LAGRANGE in approximate mode then the non-integral
coefficients will be given in approximate decimal format, and it
takes about the same time in approximate mode.


There are a number of libraries allowing least squares fitting of
polynomials of degree n to data sets of more than n+1 points.

I prefer the one called FITn, available from http://www.hpcalc.org/

Bryan Lavrich

unread,
Nov 3, 2002, 6:30:09 PM11/3/02
to
Thanks, but I have already tried that. I'm not a basic user, I've delved
into UserRPL and started SysRPL, I should have made that clear, sorry. All
that does is give me a best fit LINE, and this is obviously a parabola.
Nice effort, but the best fit option doesn't seem to work too well. Maybe
it can't do it :-(

Bryan Lavrich

unread,
Nov 3, 2002, 6:38:21 PM11/3/02
to
Thanks a lot, that really helps. I would have never found that command.
Too bad you posted right after I replied to VPN, and I'm sorry to VPN, I was
perhaps to harsh in my reply.


Nick Karagiaouroglou

unread,
Nov 4, 2002, 3:15:46 AM11/4/02
to
Hi Bryan!

You are right, these data can be only used for a linmear fit on the
HP449G (out of the box) because you have negative x, y values. If you
try a logarithmic or any other fit (except linear) you'll get an error
"LN(neg)". You must first "move" the data (all by the same amount) so
that they baecome all positive, do the fit and then move x and y back
to their original positions.

For example you can apply x1=x+3 and y1=y+9 on your original data, do
the fit, then press LR and back substitute x=x1-3, y=y1-9 in the
result.

Greetings,
Nick.

"Bryan Lavrich" <bry...@twcny.rr.com> wrote in message news:<5kix9.57640$c51.18...@twister.nyroc.rr.com>...

Nick Karagiaouroglou

unread,
Nov 4, 2002, 3:19:22 AM11/4/02
to
Hi all!

> Note that if you have created a sigmaDATA matrix for the points, you
> must transpose it before the LAGRANGE command.
>
> For your data (and in RPL mode):
> Put the following on the stack
> [[-2 -1 0 1 2 3][-8 -.3 5 5.5 3.3 -3.5]]
>
> Executing XL produces
> [[-2 -1 0 1 2 3][-8 -3/10 5 11/2 33/10 -7/2]]
>
> Then executing LAGRANGE produces, in about 1/40 of a second,
> '-((17*x^5-45*x^4-79*x^3+621*x^2-634*x-1200)/240)'
>
> If you do LAGRANGE in approximate mode then the non-integral
> coefficients will be given in approximate decimal format, and it
> takes about the same time in approximate mode.
>

One can do that, but this is *not* a fit! You have the same number of
parameters (coefficients of the polynomial) as you had pairs of x-y
values before the fit. The physical meaning of the fit is to reduce
parameters to a few values which eventually can be assigned to some
quantities with physical meaning according to some theoretical model.

Greetings,
Nick.

Veli-Pekka Nousiainen

unread,
Nov 4, 2002, 7:23:49 AM11/4/02
to
Not at all! I overlooked to check the value domain.
Nick got it right. The substitution seems to work.

VPN
"Bryan Lavrich" <bry...@twcny.rr.com> wrote in message
news:Nrix9.57641$c51.19...@twister.nyroc.rr.com...

John H Meyers

unread,
Nov 4, 2002, 11:47:30 AM11/4/02
to
You don't change non-linear curve fits by making a translation?


If the class exercise was looking for a parabolic fit,
however, then only a polynomial fit will produce it.

[r->] [OFF]


.


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 80,000 Newsgroups - 16 Different Servers! =-----

Virgil

unread,
Nov 4, 2002, 3:50:55 PM11/4/02
to
In article <cd9ca36b.0211...@posting.google.com>,
n...@imos-consulting.com (Nick Karagiaouroglou) wrote:

That is why I mentioned the FITn library. For those, like you, who
believe that "fit" doesn't mean an exact fit.

In fact, there is a pretty good quadratic fit to the data set given.

Roger D Metcalf DDS

unread,
Nov 4, 2002, 4:11:33 PM11/4/02
to

"Bryan Lavrich" <bry...@twcny.rr.com> wrote in message
news:hifx9.57334$c51.18...@twister.nyroc.rr.com...

> I am a high school student and need to know how to a best fit curve given
a
> set of data. For example:

Bryan: Take a look at the program "Curve Expert" at
http://www.ebicom.net/~dhyams/cvxpt.htm . It is for your desktop and not
the 49, so install it and under "Tools" try "CurveFinder" to see what you
come up with.

Regards,
Roger Metcalf
Arlington, Texas, USA
Please let me know if you don't receive this message :)


Nick Karagiaouroglou

unread,
Nov 5, 2002, 6:08:35 AM11/5/02
to
John H Meyers <jhme...@miu.edu> wrote in message news:<3DC6A4A2...@miu.edu>...

> You don't change non-linear curve fits by making a translation?

I don't understand the question.

Perhaps I didn't explain very good. Example:

Data is:
[[ -2. -9.1 ]
[ -1. -7.2 ]
[ -0. -3.7. ]]

SCATRPLOT shows perhaps power relation of the form y=a*x^b.

Because we have negative/zero x's and y's we run into troubles when we
try powerfit. So: Change data to y+y0 and x+x0 with x0=3 and y0=10.
Now data is:

[[ 1. .9 ]
[ 2. 2.8 ]
[ 3. 6.3 ]]

The equation of the fit is now: y+y0=a*(x+x0)^b => y=a*((x+x0)^b)-y0

A powerfit returns now the fit function:
y=.8801+x^1.7568

This must be transformed to y=.8801+((x+3)^1.7568)-10 which can be
used to calculate original y-values out of original x values.

Test:
calculated data
y(-2.)=-9.12 -9.1
y(-1.)=-7.03 -7.2
y(0.)=-3.94 -3.7

The function can be used with x-values down to -3. (It can be used
also with x<-3 but then complex analysis has to be used). So, if it
should be used for even smaller x-values (more negative) than -3, (and
if we tend to map each and everything on real numbers), then translate
using an even bigger x0.

>
> If the class exercise was looking for a parabolic fit,
> however, then only a polynomial fit will produce it.

Sigh! So out of the box one has to take the long way and try to solve
sum((Yi-a*Xi^2-b*Xi-c)^2) for a, b, c trying to find a set of
parameters that minimizes the sum.

Or get MODA. (Very good - it will fit any function we give it.)

Nick.

Nick Karagiaouroglou

unread,
Nov 5, 2002, 6:14:56 AM11/5/02
to
"Veli-Pekka Nousiainen" <DROP...@welho.com> wrote in message news:<bGtx9.123$Z6.6...@reader1.news.jippii.net>...

> Not at all! I overlooked to check the value domain.
> Nick got it right. The substitution seems to work.

Yes, only we must watch out to use the transformed fit function and
not what the hp retuns.

Greetings,
Nick.

Bill Storey

unread,
Nov 6, 2002, 4:43:04 PM11/6/02
to
In article <hifx9.57334$c51.18...@twister.nyroc.rr.com>, "Bryan Lavrich"
<bry...@twcny.rr.com> writes:

>I am a high school student and need to know how to a best fit curve given a
>set of data. For example:
>
>x: y:
>-2 -8
>-1 -.3
>0 5
>1 5.5
>2 3.3
>3 -3.5
>This is one example my teacher gave us. Too bad everybody but me and 3 of
>my friends use TI's.
>~shudders~
>We all use HP49Gs and have been at a loss the past few days.

See section 6 in your user's guide "representing systems as matrices". You
create the A matrix as columns containing the model you want to fit to the
data.

For the example above, you want to fit A0 +A1*X +A2*X^2 + A3 So your A matrix
is

A maxtrix B matrix

X^0 X^1 X^2 X^3

[[1 -2 4 -8] [[
-8]
[1 -1 1 -1] [
-.3]
[1 0 0 0] [
5]
[1 1 1 1] [
5.5]
[1 2 4 8] [
3.3]
[1 3 9 27]
[-3.5]

This gives the results

4.62 +2.98*X - 1.7*X^2 - 4.3E-2*X^3

I had an automatic program for assembling the A matrix of any order from
columns given X, but programs to automate the whole curve fit process are
plentiful at www.hpcalc.org.

Bill
alternate E-dress wtst...@ieee.org.no.spam.please
(Use the obvious)

John H Meyers

unread,
Nov 7, 2002, 7:24:38 AM11/7/02
to
Seen:

> Because we have negative/zero x's and y's we run into troubles when we
> try powerfit. So: Change data to y+y0 and x+x0 with x0=3 and y0=10.

Why not x0=5 and y0=15 ?

Do all arbitrary choices of x0 and y0 produce the same final result?

If not, then this procedure might yield different "best fits"
for different people, depending on what x0, y0 they pick,
which would be questionable.

-[]-

Nick Karagiaouroglou

unread,
Nov 8, 2002, 7:36:06 AM11/8/02
to
John H Meyers <jhme...@miu.edu> wrote in message news:<3DCA5B86...@miu.edu>...

> Seen:
>
> > Because we have negative/zero x's and y's we run into troubles when we
> > try powerfit. So: Change data to y+y0 and x+x0 with x0=3 and y0=10.
>
> Why not x0=5 and y0=15 ?
>
> Do all arbitrary choices of x0 and y0 produce the same final result?
>
> If not, then this procedure might yield different "best fits"
> for different people, depending on what x0, y0 they pick,
> which would be questionable.

Copied from my email reply:

----------------------------------------------------
Fit function: f(x;x0,y0;a,b,c...) with:
x the indep. var.
x0,y0 the transl. in x,y-direction
a,b,c... the fittable parameters

Fit (Gauss): The quantity must be minimized:

num of
points
-----
\
\ / \2
/ | f(xi;x0,y0;a,b,c)-yi |
/ \ /
-----
i=1

Please let us call the sum simply S, OK?

Without x0, y0 we would have to solve:

dS/da+dS/db+dS/dc+....=0

We consider x0 and y0 as additional parameters so that the
above becomes:

dS/da+dS/db+dS/dc+....dS/dx0+dS/dy0=0

This is the condition for "maximum perfectness" of the fit from which
also the well known formulae of linear fit etc comes from.

The above doesn't take into consideration that x0 and y0 can't
be varied freely if we are to stay real. For example in the case
of power fit we have the additional constraints x+x0>0 y+y0>0
which for a given data set can be written as x0>A and
y0>B with A=max(-x) and B=max(-y).

So we take into consideration the constraints and we

a) either solve ad hoc numerically using the constraints also as
guarants for the fitting function to stay in real. At least a good
numeric solver should be able to do this.

b)or even better we pluck the constraints as a kind of init conditions
in dS/da+dS/db+dS/dc+....dS/dx0+dS/dy0=0 using the well known method
of Lagrange multipliers. Sometimes analytic solutions for a,b,c,...x0,y0
will be possible, most of the time not possible. But the resulting
equations can be used by numeric solvers.
-------------------------------------------------

Nick.

John H Meyers

unread,
Nov 9, 2002, 1:21:54 AM11/9/02
to
Anybody recall the original question?

"I am a high school student and need to know
how to a best fit curve given a set of data.
For example:"

[data omitted]

Data suggests a polynomial fit (looks parabolic), but the
student initially guessed "logarithmic fit," and the whole thing
has ever since gone off into a new orbit of its own,
including the invention of a new statistics fudge factor, of sorts.

Bryan's class has by now probably graduated,
got jobs, and retired on their 401K,
but if anyone is still interested, look here:

http://www.hpcalc.org/search.php?query=pregr

There you will find both HP48 and HP49 packages for PREGR,
a UserRPL program by Dennis York of HP, posted in 1992,
based on an HP-28S program submitted to HP
by Dr. Robert C. Wyckoff.

"Performs polynomial regression,
by taking a matrix stored in SigmaDAT
and returning a polynomial of the specified degree
least square fit to the data."
[also plots the original data and its poly fit,
and goes to a solver for the equivalent of PREDY and PREDX]

The one person who commented about the program said:

"This is a great program for finding out quadratic regression;
IMO, one of the most important things missing in the 49g (& 48g)."


But this is so profound a newsgroup, there's nothing missing here!


Note:

The HP49 binary version of PREGR was loaded in Exact mode,
but should have been in Approximate mode; you should set
Approximate mode and then edit the program, once, after
installing it -- don't change anything, but just press
ENTER -- this will change all numbers to reals.

For the original data supplied, and degree 2 for the poly,
a very nice fit is obtained, giving the expression

'Y=-1.83035714286*X^2.+2.79607142857*X^1.+4.73142857143'

Trying degree 3 for the poly:

'Y=-4.35185185185E-2*X^3.
-1.76507936508*X^2.+2.9832010582*X^1.+4.62698412698'

As you can see by the relatively small coefficient of X^3,
and the other coefficients being relatively the same,
a quadratic poly was pretty much on the mark.

But there being six data points, you could even go to
a 5th degree poly, which would be "exact"
for this specific data set only:

'Y=-7.08333333333E-2*X^5.+.1875*X^4.
+.329166666667*X^3.-2.5875*X^2.
+2.64166666667*X^1.+5.'

Which is the same as given by \GSDAT TRN XQ LAGRANGE

'-17/240*X^5+3/16*X^4+79/240*X^3-207/80*X^2+317/120*X^1+5'

Bill Storey

unread,
Nov 9, 2002, 9:12:40 AM11/9/02
to
In article <3DCCA982...@miu.edu>, John H Meyers <jhme...@miu.edu>
writes:

>Anybody recall the original question?
>
>"I am a high school student and need to know
> how to a best fit curve given a set of data.
> For example:"
>
>[data omitted]
>
>Data suggests a polynomial fit (looks parabolic), but the
>student initially guessed "logarithmic fit," and the whole thing
>has ever since gone off into a new orbit of its own,
>including the invention of a new statistics fudge factor, of sorts.
>
>Bryan's class has by now probably graduated,
>got jobs, and retired on their 401K,
>but if anyone is still interested, look here:

John,

I posted information using the built-in solver to match any type of generic
model by creating an A matrix containing the model to be fit. I was hoping
that you would post the answer to his question about how to obtain the
correlation factor. In any case, thanks for the reference to PREGR. I'll load
that one onto my calc.

Virgil

unread,
Nov 10, 2002, 1:03:44 AM11/10/02
to
In article <20021109091240...@mb-dd.aol.com>,
wtst...@aol.comspamit (Bill Storey) wrote:

SOLVESYS can also be used to fit functions of arbitrary form to a
set of data.

Set up your equation in the form 'Y=f(X,A,B,...,C),
where A,B,...,C are parameters whose values are to be found to best
fit a known set of (X,Y) data points.

WARNING: you must replace all plus signs (+) in your equation with
the ADD command or with double minuses (--) before executing EVAL
below! Failure to do so causes "+" to act on lists instead of on
values, and will really mess things up. This is really the only
drawback to using this method to generate your equations.

Put the X data values in a list and store it as 'X', and the
corresponding Y values in a list stored as 'Y'. Put the equation on
the stack and execute EVAL.

Store the resulting equations as a list in 'EQ' and use SOLVESYS.

It will solve for your parameters.

0 new messages