Get Help

Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Community
- :
- PTC Mathcad
- :
- PTC Mathcad
- :
- How to implement a solve block in a subroutine

Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-01-2014
12:21 PM

12-01-2014
12:21 PM

How to implement a solve block in a subroutine

I need to fit data to a function with 5 variables and have Mathcad 15. The parameter estimation works fine with a solve block and Minerr in a straight program.

However, when a define a subroutine for the parameter estimation I get an error in the subroutine. I need the subroutine to automate the fitting of a large set of data with Minerr() and the constraints. I wrote an initial program with genfit() that works for large data sets in an Excel file, but I need the constraints that can be used with Minerr() because genfit() gives occasionally physically impossible answers.

2 files are attached, one with the solve block in the main program that works and another with the solve block in a subroutine that gives an error. There are no data to be fitted in the program because they are dynamically loaded from an Excel file. I would appeciate any help for solving my problem. I have not seen example files with solve blocks in a subroutine.

Thanks. Ulrich

Labels:

3 REPLIES 3

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-01-2014
02:10 PM

12-01-2014
02:10 PM

Re: How to implement a solve block in a subroutine

You can't put a solve block in a program. You need to turn the solve block into a function, and then call the function in the program.

Other points:

1) Do not define SSE for minerr. Just create a vector of residuals. It's faster, and the results are more accurate when the algorithm used by minerr is Levenberg-Marquardt, which is the default for Mathcad 12 and later.

2) minerr treats errors in constraints the same as the errors in the residuals. i.e. they are weighted the same, and are therefore soft constraints, not hard constraints. If you want hard constraints weight them heavily.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-03-2014
05:05 PM

12-03-2014
05:05 PM

Re: How to implement a solve block in a subroutine

Richard,

thanks a lot. I rewrote Minerr with constraints as a function. That worked. Your comment about how the constraints are handled are also very helpful.

However, I had to use the residual sum of squares as objective function that was minimized. The vector of residuals that you proposed as superior method did not give give satisfactory results with Minerr. Also, your short-hand version for generating the vector of residuals did not work. I had to program a subroutine with a for-loop to generate the vector.

Again thanks so much for your help. I got a functioning program.

Ulrich

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-03-2014
05:54 PM

12-03-2014
05:54 PM

Re: How to implement a solve block in a subroutine

The vector of residuals that you proposed as superior method did not give give satisfactory results with Minerr

Well, it should do. It should in fact give superior results. If you calculate the SSE after the fit using both methods, what numbers do you get?

Also, your short-hand version for generating the vector of residuals did not work. I had to program a subroutine with a for-loop to generate the vector.

Without the data I had to make some guesses there. It's usually possible to get the vector of residuals with some sort of vectorization, and then the minimization is faster than if they are calculated using a program. I do have cases myself where I had to resort to a program though.