﻿

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.

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

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,

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.

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

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.

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!