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

Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X

How to run a worksheet multiple times while alternating a parameter

Eivind
1-Newbie

How to run a worksheet multiple times while alternating a parameter

Since I am new to MathCad, I am hoping that some skilled experts could enlighten me with a solution to this small (hopefully) problem!

My project(master thesis) is a massflow diagram for a theoretical bioenergy plant. The calculations (about 10 pages), starts with some pre-defined constants, goes through the calculations and calculates different result parameters (flows in/out, energy produced, energy consumed, energy exported ...).

In order to analyse the relationship between the pre-defined constants and the results, I want to run the worksheet multiple times (100) while altenating a chosen parameter. Doing this manually is not an optimal solution.

I am looking for a script which execute these steps:

1. Starting with a list of the different alternatives for a parameter.

2. Run my worksheet (AA.xmcd) once for every alternative.

3. Return the results for each run and store them (together with the alternating parameter) for further data analysis.

Is there a simple way I could automate this procedure? Within the same worksheet, or in a linked worksheet? I have read through the different discussions on the topic, but have not yet manage to utilize the answers... Alternative procedures solving the same problem is also appreciated .

Thank you in advance!

Eivind

NB: Running on MathCad 14.0

1 ACCEPTED SOLUTION

Accepted Solutions

Hi Eivind

I've attached a reworked version that you can look at to see if it helps you.

The Master sheet now opens the working sheet where your main program will go.

The master sheet set up the parameters to control the looping of the working sheet & consolidates the results from each run.

Once the data is all collected in the master sheet you can then post process & display it in any way you like.

Once the sheet is running suggest tiling vertically to see both sheets side by side.

To run again, select the Master Sheet and press <ctrl> <F9>

On my laptop at home I encountered a memory leak which means that I receive a "null pointer error" at varying point during the looping of the work.mcd file.

It is more apparent at larger numbers of iterations (>80) but re-running the master worksheet will give the error happening at earlier stages of the loop time after time (first run 79 iterations, second 40, third 29 etc...)

However on my work computer it doesn't seem to happen.

Hope this doesn't mean that I've caught a virus from any of the dodgy websites I trawl (BBC, GOOGLE, MSN etc)

It doesn't make too much sense, but the functions used aren't documented in the help files & won't be in very common use, so could still have untrapped errors.

regards

Andy

View solution in original post

21 REPLIES 21
Raiko
16-Pearl
(To:Eivind)

Hello Eivind,

I suggest to store the parameters in separate Excel files and to read them in with the readfile command which itself is executed within a loop.

Have a look at the attached image where some code is depicted. Maybe it is of use for you

Cheers

Raiko

MikeArmstrong
5-Regular Member
(To:Raiko)

Raiko Milanovic wrote:

Hello Eivind,

I suggest to store the parameters in separate Excel files and to read them in with the readfile command which itself is executed within a loop.

Have a look at the attached image where some code is depicted. Maybe it is of use for you

Cheers

Raiko

Raiko,

That would mean creating 100 Excel files wouldn't it?

He could use the attached "WRITEEXCEL" function which will write data to a specific worksheet in a single Excel file

Mike

Mike,

yes you're right. That would entail creating 100 Excel files if you have 100 Parameters.

However, if it is only one parameter that is to change than it might be easier to have an array of 100 elements from which you succesively read in the parameter value. This would be much faster than reading from an external file.

Raiko

MikeArmstrong
5-Regular Member
(To:Raiko)

However, if it is only one parameter that is to change than it might be easier to have an array of 100 elements from which you succesively read in the parameter value. This would be much faster than reading from an external file.

Correct. It will be much easier to iterate through a single parameter within Mathcad.

A better description of the problem is required or a sample worksheet.

Mike

You haven't shown your sheet, so we can only guess about how you set it up; but if you're totally new you may not realize the power of making functions instead of single-valued computations. The attached sheet is a simple demonstration of a number of ways to calculate the same thing, adding a bit more variability as you change the way you define a function. It sounds to me like you need to rework your sheet into a set of functions of the variables you want to change, then feed it a vector of the variables.

Good Luck!

Hello Elvind,

there was a topic that may help from the old Collabratory , However be aware that it is prone to crashing Mathcad if variables that are being "read" do not exist in the target worksheet; the error trapping is minimal.

That said here is the post:

Topic: VBScript - creating & control another mathcad (4 of 5), Read 208 times, 2 File Attachments
Conf: Authoring
From: rijackson richard.
Date: Saturday, July 28, 2007 07:44 AM

Sorry, my mistake. I copied the wrong URL. It's the next post in that thread, from Stuart.

http://collab.mathsoft.com/read?72543,13

There are also the attached files from Xavier. I can't locate the original thread.

Richard

I have attached the files that I have & seem to work in version 12, other than this version I can't help I'm afraid.

/upload/Controller_v2.1(6).mcdHope that this is helpfull

Regards

Andy

Hi Andy, and thank you for your time and contribution!

I have tried to set up the mastersheet and link it with my sheets with the calculations. I have managed to remotely change the parameter, but the results are not displayed and I only get the error message: "Error HRESULT E_FAIL has been returned from a call to a COM component".

Any suggestions? I have attached the masterSheet, my calculations are so messy that they would only confuse

Eivind,

MikeArmstrong
5-Regular Member
(To:Eivind)

Eivind,

Have a look at Richards thread below.

He suggests writing everything as a function of the parameter you wish to change and then create a loop. This is the easiest way to achieve what you are trying to do. If your data or worksheet won't allow this then another method could be adopted, but without the data we'll never know.

If you don't want to paste the actual data/calc for any reason, just replicate the problem and post that worksheet.

Mike

Hi Eivind,

Can you save a version 11 or 12, I can't read it at version 14.

(I'll ask around to see if anyone here can down rev it, i'm guessing that its not going to be possible.)

Regards

Andy

Here is the version in MathCad 12. The error might be due to a newer version of MathCad?

Eivind

Hi Eivind

I've attached a reworked version that you can look at to see if it helps you.

The Master sheet now opens the working sheet where your main program will go.

The master sheet set up the parameters to control the looping of the working sheet & consolidates the results from each run.

Once the data is all collected in the master sheet you can then post process & display it in any way you like.

Once the sheet is running suggest tiling vertically to see both sheets side by side.

To run again, select the Master Sheet and press <ctrl> <F9>

On my laptop at home I encountered a memory leak which means that I receive a "null pointer error" at varying point during the looping of the work.mcd file.

It is more apparent at larger numbers of iterations (>80) but re-running the master worksheet will give the error happening at earlier stages of the loop time after time (first run 79 iterations, second 40, third 29 etc...)

However on my work computer it doesn't seem to happen.

Hope this doesn't mean that I've caught a virus from any of the dodgy websites I trawl (BBC, GOOGLE, MSN etc)

It doesn't make too much sense, but the functions used aren't documented in the help files & won't be in very common use, so could still have untrapped errors.

regards

Andy

Thank you so much Andy!

This really makes my analysis easier to perform and easier to controll! I had to save all the worksheets in Mathcad 12 versions in order for them to communicate properly. Again, thank you for your effort.

Sincerely,

Eivind

RichardJ
19-Tanzanite
(To:Eivind)

This has come up many times. As already pointed out, the right way to do it is to write everything as a function of the parameter you wish to vary. Then at the end of the worksheet you just have a loop that calls the function multiple times. There are other ways to do it, but they are all kludges and not necessarily trvial to implment either.

Seeing the worksheet would help.

As you can see in the attached worksheet, there are many calculations ending up in many different parameters. It is not easy to combine all these calculations in one loop (Maybe it is?). Maybe you have other suggestions to how the worksheet 'actual' should have been build? Are there some tricks or way of building the model which would have simplified the model. The reason for defining all the parameters is that this quantifies all the stages in the process, making it easier to check, controll and monitor the calcultations.

I think that the best way to be able to simulate the concept for different values for ("A.L") is to define the values in a remote worksheet with a loop function.

Any suggestions are welcome!

Eivind

MikeArmstrong
5-Regular Member
(To:Eivind)

The reason for defining all the parameters is that this quantifies all the stages in the process, making it easier to check, controll and monitor the calcultations.

Without being too critical, if somebody gave me that calculation to check in our company I wouldn't be best please. The input variables are scattered everywhere and it’s hard to read / follow. Mathcad has the ability to produce calculations as they are found in textbooks, documents...etc.

Regarding your problem - You haven't stated which variables / parameter you want change in order to run the calculation numerous times.

Mike

No offense taken Mike

I know it's messy, thats the reason why I didn't want to post it in the first place. It's my first worksheet in MathCad and I have therefore kept the calculations to basic calculus making it volumous...

The variable I want to alternate is "A.L". This variable should get it's values from a table/array consisting of 100 predefined numbers.

Hopefully it is possible to have many output variables. Three of these should be: "Methane", "E.Area" and "E.X"

Are there any standards/templates on how the worsheet should be contructed and organized?

Thank you for your time and contribution!

Eivind,

MikeArmstrong
5-Regular Member
(To:Eivind)

Are there any standards/templates on how the worsheet should be contructed and organized?

Thank you for your time and contribution!

Eivind,

I can only suggest what is acceptable for my employer. On the basics I would say a calculation should be clean, tidy, fully referenced and easy to read. Remember that is only my point of view. Your more than welcome to discard my comments.

I can post an example of a calculation I've produced if you want?

Mike

Hi Mike,

Please do post one of your examples, so I can get a grip on how it could be structured

Eivind,

MikeArmstrong
5-Regular Member
(To:Eivind)

This is an example of a Pulling Head calculation I completed this year. I have removed the headers and footer for privicy reasons.

Remember this is just for indication as your calculation layout is your choice.

Mike

Please see http://communities.ptc.com/message/148006

Using FRAME is a good solution for me, even maybe not the more elegant.

Regards. Alvaro.

Hi,

You can try to do this by embedding your calculation into Excel or a VB application. It is a elegant solution as you can take advantage of Excel's nice charts. There are a couple of good examples in the MathCAD help.

I'm using version 11, and a good Excel-MathCAD integration example is located under: "C:\Program Files\Mathsoft\Mathcad 11 Enterprise Edition\qsheet\Samples\Excel". I think you can easily figure out where it is on your computer.

Regards,

Lucian

Top Tags