Curve Fitting Made Easy

easy

I talk to customers a lot. It’s my job. So I answer a lot of questions about PTC Mathcad functionality. One of my favorites is “Can PTC Mathcad do curve fitting?” Why is this one of my favorite questions to answer? Because the answer is not simply “yes”, but is a resounding “Oh yeah!” (Cue the entrance of the Kool-Aid man).

PTC Mathcad has a whole slew of functions for curve fitting, smoothing, interpolation, prediction, and just data analysis in general. So yeah, PTC Mathcad does curve fitting very well.

 

But there is a slight catch. To really be able to do curve fitting well, you need to have a good idea of the nature of the fitting function. Is it linear, exponential, logarithmic, sinusoidal, etc? The built-in PTC Mathcad function that you use depends upon the nature of the data. For example, you don’t want to use expfit for data that has a linear relationship.

 

What if you aren’t sure of the nature of the data? Well, you’ll then have to try out different fits until you find the fitting function that best describes the relationship of the data. But this is cumbersome.

 

One day, after trying to explain this to a customer, I thought that it would be nice to build something out in PTC Mathcad that would allow a user to quickly and simply insert the data and test out the best fitting function. So that’s exactly what I did.

 

In my worksheet , once the data is in, probably through one of our File Access functions, all you have to do is change the number for the Fit variable to see how well different fitting functions match up with the data.

 

blog 1

 

 

 

 

 

 

Using PTC Mathcad’s symbolics engine, the fitting function is automatically displayed.

 

 

blog2

 

 

 

 

I also wanted to give more information than just the fitting function. The built-in functions used for curve fitting return a column vector of the coefficients that define the fit. What the functions do not return is a quantitative indication of the quality of the fit. But Mathcad can handle that easily enough, so I ran a calculation for the R-squared value,

 

blog3

 

 

 

 

where y is the vector of dependent data values, F is the fitting function, x is the vector of independent data values, and μ is the mean of y.

 

 

blog4

 

 

And finally, we produce a plot of the data alongside the fitting function to give a visual of how well the two correspond.

 

blog5

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

As I said, all of this is automatically generated from some programs I tucked away in a collapsible Area, so that all you have to do is change the value of Fit to see which function fits the data best. In the case of my dummy data you see plotted above, the best fit is the exponential function.

 

blog6

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

But another great thing about this is that the results are not simply for display. F(x) is actually a usable function. So let’s say the data on the x-axis are time, such that every second for 150 seconds I collect a data point that then gets plotted on the y-axis. Now that I have my fitting function, I can use it to predict what will happen in the system at the 151st second or the 180th second.

 

Hopefully you’ll find this worksheet useful. If you want, you can use it standalone, entering your own data in, or, if you’d like to incorporate it into worksheets of your own, you can use the Include Worksheet capability to reference the worksheet or just throw all of the calculations into a collapsible Area.

 

Happy curve fitting!

 

Don’t have PTC Mathcad? Try it out for free with PTC Mathcad Express.

 

Download all our free worksheets.

This entry was posted in Mathcad, The Inside Scoop, Tips & Tricks, Uncategorized and tagged , , , . Bookmark the permalink.

One thought on “Curve Fitting Made Easy”

  1. Ian Boulton says:

    Great post! I find this stuff really interesting and I am not a mechanical engineer (just a proud nerd)🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s