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

Nonlinear curve fitting of piecewise-defined function

dwarren
1-Newbie

Nonlinear curve fitting of piecewise-defined function

As suggested by the title, I'm attempting to fit a piecewise-defined curve to a set of data points.  This is also the first time I have used Mathcad, so it is exceedingly likely I'm missing something obvious.  My worksheet is attached to this post.

I'm attempting to follow the example at http://www.phys.uconn.edu/~hamilton/phys258/P/genfit.pdf, but when I call the genfit routine, I get the error "This value must be a function".

So, question number 1: what is this error message trying to tell me, and how can I resolve it?

While I have your attention, I also have question number 2.  The function is a horrible mess because I'm trying to combine data points with continuity restrictions.  I don't know how to specify symbolic relations between fitting parameters (e.g. forcing the second derivative to be continuous at x = 60 yields "e = b + 24").  So I've solved the continuity restrictions by hand and substituted them back in.  Is there a way to get Mathcad to recognize additional restrictions on the fitting parameters beyond just the list of data points?

And branching further afield still, question number 3: can Mathcad do symbolic differentiation of piecewise functions?  When I ask for d/dx(f), I get an error message that f "must be a scalar".

Thanks in advance for any help you all can offer.

7 REPLIES 7
RichardJ
19-Tanzanite
(To:dwarren)

Unfortunately, your worksheet is in Prime 3.1 format, and I have Prime 3.0, which can't read it (ridiculous, but true: Prime file formats are not even compatible within a major release version, and Prime cannot save a file back to any earlier version). So I can't answer question 1. For question 2, you would be better off using a solve block with minerr, which can handle constraints. The answer to question 3 is yes, in principle. Without seeing the worksheet I can't say why you are getting an error though.

Is there a particular reason you are using Mathcad Prime rather than Mathcad 15?

Richard Jackson wrote:

Unfortunately, your worksheet is in Prime 3.1 format, and I have Prime 3.0, which can't read it (ridiculous, but true: Prime file formats are not even compatible within a major release version, and Prime cannot save a file back to any earlier version).

I am waiting from PTC Mathcad team for a new technology: We cannot read Prime 3.0 files in Mathcad 3.1 and 4.0

Richard Jackson wrote:

Is there a particular reason you are using Mathcad Prime rather than Mathcad 15?

This is a question I *can* answer.  I just bought Mathcad as part of starting a new postdoc (my Ph.D. advisor has been using Mathcad since its single-digit version number days).  When I searched on PTC's website, I didn't find a way to buy Mathcad 15.  After further looking just now I still don't.  If 15 is more powerful than Prime (and is available for purchase) then maybe I should contact PTC about upgrading to a potentially more useful version.

RichardJ
19-Tanzanite
(To:dwarren)

Your license for Prime will also work for Mathcad 15, which you can download here: http://www.ptc.com/product/mathcad/free-trial/thank-you

Mathcad 15 is more powerful than Prime in almost all respects.

Don Warren wrote:

As suggested by the title, I'm attempting to fit a piecewise-defined curve to a set of data points.  This is also the first time I have used Mathcad, so it is exceedingly likely I'm missing something obvious.  My worksheet is attached to this post.

I'm attempting to follow the example at http://www.phys.uconn.edu/~hamilton/phys258/P/genfit.pdf, but when I call the genfit routine, I get the error "This value must be a function".

So, question number 1: what is this error message trying to tell me, and how can I resolve it?

What it's telling you, after using the error trace facility, is that you have type d(...) in several instances.  Mathcad interprets this to mean that d is a function; you need to explicitly type the multiplication operator between d and the parenthesis.

After this, you have another slight problem.  Your guess values for x = 0, give F(x) = complex, which genfit won't handle.  Looking at your expression for f, this is because with b and e positive, you will get roots of negative numbers.  The fix is to make b and e negative.  It should work then. See attached modification to your worksheet.

Question 2: Don't have time to look at it, unfortunately.

Question 3: It appears as if Mathcad can't directly symbolically solve conditional functions.  You can break the function up and take the derivatives of each part, though ... but not a very good choice.  Probably imposes because the results, without much thought about constraints, could turn out to be horrendously messy!

Interestingly, the example worksheet you were working from doesn't work as written in Prime, because the genfit solver algorithm (an Optimized L-M) is quite fussy about initial values.  In Mathcad 15 you have the option to choose an ordinary L-M solver, which gives the expected answer.  In Prime you have to amend the guess value to [500, -0.1]

Stuart

What is this error trace facility you're referring to?  Is it a feature that exists in Prime 3.1?  (The first Google hit suggested that right-clicking on the offending function's name should bring up a ... something ... but that doesn't happen.)  On the other hand, Prime 3.1 also has a button for converting xmcd files that is permanently grayed-out and unusable, so this may be something of a trend.  I really should look into Mathcad 15, whatever that is.

Don Warren wrote:

What is this error trace facility you're referring to?  Is it a feature that exists in Prime 3.1?  (The first Google hit suggested that right-clicking on the offending function's name should bring up a ... something ... but that doesn't happen.)  .

Look on the Calculation tab in the Ribbon.  You should see a collection of slightly washed-out red "video" buttons in the Error Tracing group.  Click on the expression with an error and the buttons should get brighter (they don't always; it depends on whether the error tracing facility can look back any further than the expression with the error).   You then click on the appropriate Error Trace button to follow the calculations back to where Prime thinks the problem originated.  Your next task is then to work out what the sometimes cryptic error messages actually mean!

The right-click Google suggestion refers to error tracing in Mathcad 15.  I actually prefer the Prime method of tracing errors, but I do miss M15's debug window.

On the other hand, Prime 3.1 also has a button for converting xmcd files that is permanently grayed-out and unusable, so this may be something of a trend.  I really should look into Mathcad 15, whatever that is.

You need to have Mathcad 15 installed for the xmcd converter to work.  If you have a full Prime 3.1, rather than Prime Express, you are automatically licenced for a copy of Mathcad 15.  If you haven't got it with your Prime distribution, then you can download it from

http://www.ptc.com/product/mathcad/free-trial.

You should definitely check it out, if only to make you realize what Prime is still missing compared to the original Mathcad.

... and, to be fair, to notice Prime's

significant improvements in matrix entry,

mixed unit arrays

and change from static type checking (a form of Nannyware introduced in Mathcad 12 that stopped some people from making unit errors at design time but really hacked off a number of experienced users by stopping them doing useful and interesting things)

Oh, and control-J for entering programming keywords ....

Stuart

Announcements
Check out the latest
Mathcad Tip
"PTC Mathcad 15 / Prime 1-6 Update."