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

Community Tip - Want the oppurtunity to discuss enhancements to PTC products? Join a working group! X

Why has Mathcad 15 sometimes fitting problems

FDS
12-Amethyst
12-Amethyst

Why has Mathcad 15 sometimes fitting problems

I use CurveExpert (version 2.4.0) for getting an initial idea on what is the best fitting model for a data set. However, when I want to use then the proposed equation in Mathcad I do not get optimal results. In the rational fit I am completely out of range while in the sinusoidal fit my R² is lower. I assume that I am not familiar enough with all fitting possibilities within Mathcad 15. So I would appreciate any help as I would like to improve my skills or to understand why the results in Mathcad are inferior to those of CurveExpert. I have added 2 worksheets to demonstrate the issue.

1 ACCEPTED SOLUTION

Accepted Solutions
Werner_E
24-Ruby V
(To:FDS)

You are not doing anything wrong. Curve fitting in Mathcad can be quite tricky and also disappointing. Most of the time when it fails the fit is very sensitive according to the guess value. CurveExpert Pro as a specialized program is far better in doing its task. Especially as it has the ability to chose guess values itself and also, as I suspect, changes those guesses in trying to find the best fit.

Mathcad works with the guesses you provide, runs down the error surface and stops, when it has found a relative minimum. Other guesser might yield a different, better relative min.

There is also nothing which could be said against using a solve block with minerr. Usually this method has advantages over genfit as you have more control, can add additional constraints, etc.

Vladimir suggested using the built-in genfit and actually it can provide a much better result than your solve block:

But you can achieve the very same result with your solve block if you change the algorithm to "Levenberg-Marquardt" which usually is a good choice anyway. Right click at the word "Minerr" to change the algorithm. You can also change the algorithm for genfit, but you can just chose between LM and optimized LM (which is default). Interestingly enough genfit fails in this example when LM is chosen.

Regards

Werner

View solution in original post

10 REPLIES 10
VladimirN
24-Ruby II
(To:FDS)

Use "genfit" built-in function to determine parameters of the function. Has been shown before here: Re: Problem with 4 parameter logistic fit for immunoassay

Werner_E
24-Ruby V
(To:FDS)

You are not doing anything wrong. Curve fitting in Mathcad can be quite tricky and also disappointing. Most of the time when it fails the fit is very sensitive according to the guess value. CurveExpert Pro as a specialized program is far better in doing its task. Especially as it has the ability to chose guess values itself and also, as I suspect, changes those guesses in trying to find the best fit.

Mathcad works with the guesses you provide, runs down the error surface and stops, when it has found a relative minimum. Other guesser might yield a different, better relative min.

There is also nothing which could be said against using a solve block with minerr. Usually this method has advantages over genfit as you have more control, can add additional constraints, etc.

Vladimir suggested using the built-in genfit and actually it can provide a much better result than your solve block:

But you can achieve the very same result with your solve block if you change the algorithm to "Levenberg-Marquardt" which usually is a good choice anyway. Right click at the word "Minerr" to change the algorithm. You can also change the algorithm for genfit, but you can just chose between LM and optimized LM (which is default). Interestingly enough genfit fails in this example when LM is chosen.

Regards

Werner

FDS
12-Amethyst
12-Amethyst
(To:Werner_E)

Werner and Vladimir thank you for this information. So I guess it is best to make CurveExpert do the first fit and then use his coefficients as guess values in Mathcad.

Werner_E
24-Ruby V
(To:FDS)

FDS wrote:

Werner and Vladimir thank you for this information. So I guess it is best to make CurveExpert do the first fit and then use his coefficients as guess values in Mathcad.

This sure is a good idea - especially if you have no clue which type of function to chose or in which area the guesses should be. But for many tasks you know exactly which kind of function you are hunting for and also have a rough idea concerning its parameters. So as you have seen genfit does a nice job and the solve block, too, if you chose the LM algorithm.

Nevertheless Curveexpert sure is easier to use, better suited and often gives you also better results.

Mathcad is an all-purpose program. It isn't really good at symbolics (Maple and Mathematica are far better), its not state of the art when it comes to curve fitting and its even not that good when it comes to numerical algorithms (compared to Matlab). But it can do it all, handles units like a charm and its white board interface is unrivaled.

WE

FDS
12-Amethyst
12-Amethyst
(To:Werner_E)

Yes, that is a good summary. I am in that respect VERY disappointed that PTC does not provide a way for MC11 users to install MC11 on a new computer so that we would have access to better symbolics. But I am sure I will never understand PTC way to keep customers satisfied.

StuartBruff
23-Emerald II
(To:FDS)

FDS wrote:

Werner and Vladimir thank you for this information. So I guess it is best to make CurveExpert do the first fit and then use his coefficients as guess values in Mathcad.

Not necessarily.  A quick look at CurveExpert indicates that it uses the Levenberg-Marquardt algorithm to do non-linear fitting.  You may find it generally more convenient to ensure that you are using LM, only going to CurveExpert if you are having difficulty with Mathcad.  OTOH, as Werner says, the CurveExpert user interface is geared towards finding curve fits, so the ability to play with curves may be a factor in overall ergonomics ... it's just that experience has made me leery of copying / transcribing data from other sources due to the potential for errors to creep in!

https://docs.curveexpert.net/curveexpert/basic/modeling.html

https://docs.curveexpert.net/curveexpert/pro/calculation.html#nonlinear-regression

https://docs.curveexpert.net/curveexpert/pro/appendix-theory.html#theorynonlinearregression

Stuart

RichardJ
19-Tanzanite
(To:Werner_E)

change the algorithm to "Levenberg-Marquardt" which usually is a good choice anyway.

That is the fundamental problem. The default is "autoselect", which tries LM first and only uses the others if it can't converge. So minerr had already been manually set to a different algorithm, and that's why it failed. Specifically, if you pass minerr a vector of residuals only LM will work. For CG or QN it is necessary to calculate the sum of the squares of the residuals and pass that to minerr (which, however, almost always yields worse results for the LM algorithm). The sinusoidal model has the same problem.

RichardJ
19-Tanzanite
(To:RichardJ)

Once the minerr algorithm has been fixed mathcad gives a sinusoidal model with a lower error that CurveExpert:

I don't know why the standard error is slightly different from that given by CurveExpert when I use the parameter values from CurveExpert.

FDS
12-Amethyst
12-Amethyst
(To:RichardJ)

Thank you Richard for these additional comments. Highly appreciated! Filip

RichardJ
19-Tanzanite
(To:FDS)

I'll make one additional comment. The sinusoidal fit is incredibly badly conditioned. Minerr changes the parameter values by orders of magnitude for only a modest reduction in the error. This is also why the standard errors for the parameters shown by CurveExpert are so huge: many times larger than the values of the parameters. The error surface is basically almost flat, and the solvers are wandering around trying to find the minimum of that. It's not surprising that different solvers (which are all iterative) call it quits at somewhat locations on the surface.

Top Tags