cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X

Get a best fit line from a function for system programing

jroth
5-Regular Member

Get a best fit line from a function for system programing

Hello,

I am looking at trying to get a best fit curve line, say a quadratic to the data function at the bottom of document. My intent is to program in the dP(m) for the most part, disregarding the temperature and the pressure as those don't have a drastic effect on the curve considering my dP meter is from 0 to 20" of water. (about 3% at most)

I was unsure if there is a simple way in mathcad rather than finding 3 pionts and setting my data to those 3 points for a best fit, to use the gen fit to find the coefficients.

ACCEPTED SOLUTION

Accepted Solutions
RichardJ
19-Tanzanite
(To:jroth)

What you want to do is called functional approximation. There are many methods for doing this, but a good one is an approximation based on Chebyshev polynomials. I have attached two worksheets. One explains the math behind Chebshev functional approximation. It is obviously from a handbook, but I can't figure out which one it comes from (I saved it as a separate worksheet years ago). The second shows the implementation for your example. That worksheet also includes a stand-alone function I wrote to do all the necessary steps. It takes the polynomial degree, the name of the function, and the interval for the approximation as inputs, and returns the coefficients of the approximating polynomial.

View solution in original post

10 REPLIES 10
Fred_Kohlhepp
23-Emerald I
(To:jroth)

The function "regress" will do a least square polynomial fit. If you use only three points, a second order ploynomial will fit those three points but may not approximate the curve correctly. Build vectors of many points, use regress, and plot the errors to choose the correct order of the polynomial.

RichardJ
19-Tanzanite
(To:jroth)

What you want to do is called functional approximation. There are many methods for doing this, but a good one is an approximation based on Chebyshev polynomials. I have attached two worksheets. One explains the math behind Chebshev functional approximation. It is obviously from a handbook, but I can't figure out which one it comes from (I saved it as a separate worksheet years ago). The second shows the implementation for your example. That worksheet also includes a stand-alone function I wrote to do all the necessary steps. It takes the polynomial degree, the name of the function, and the interval for the approximation as inputs, and returns the coefficients of the approximating polynomial.

jroth
5-Regular Member
(To:RichardJ)

Richard,

That is quite amazing and exactly what I was looking for. I will have to look more into the Chebyshev Polynomials and see what else I can apply them to.

LouP
12-Amethyst
(To:RichardJ)

Richard,

Do you have a Prime2 or earlier mathad version of these (at least the general sheet from the workbook) that you can post?

Lou

RichardJ
19-Tanzanite
(To:LouP)

Here's the worksheet in MC15 and MC11 formats. The self contained function is added near the end. In MC11 it's not quite self contained, as it needs two helper functions.

LouP
12-Amethyst
(To:RichardJ)

Thanks. I'm familiar with equiripple filters using Chebychev approximations. I see that this technique is a more general method of getting optimal equiripple error approximations to functions (instead of min mean square error); useful in cases where the peak error bound is more critical than the error "energy."

Lou

RichardJ
19-Tanzanite
(To:LouP)

Yes, it minimizes the maximum error, rather than the least squares error. Well, almost anyway. The polynomial is an approximation to the polynomial that minimizes the maximum error. Ther more terms, the better the approximation.

This describes exactly how that is achieved:

http://www-solar.mcs.st-and.ac.uk/~clare/Lectures/num-analysis/Numan_chap3.pdf

http://www-solar.mcs.st-and.ac.uk/~clare/Lectures/num-analysis/Numan_chap4.pdf

jroth
5-Regular Member
(To:RichardJ)

Richard,

The PDFs you linked, are they out of a numerical analysis book? If so, could you provide the title. I would be interested in picking up a book in that area as my libaray is somewhat lacking in numerical methods.

RichardJ
19-Tanzanite
(To:jroth)

jroth
5-Regular Member
(To:RichardJ)

Richard,

Thank you. The lectures are helpful. I have done a B Cubic Spline fit where I have extracted the derivative of my curved data. Attached is a PDF showing the results.

As a side note, I have actually coded the process in MathCad 15 to plow through the data accuratly using VBA code. All I do is feed it the vector form of the data, and it will send back the new derivative data vector as I want.

Announcements

Top Tags