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

Community Tip - When posting, your subject should be specific and summarize your question. Here are some additional tips on asking a great question. X

How to implement a solve block in a subroutine

ptc-2930462
1-Newbie

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

3 REPLIES 3
RichardJ
19-Tanzanite
(To:ptc-2930462)

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.

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

RichardJ
19-Tanzanite
(To:ptc-2930462)

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.

Top Tags