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

Community Tip - You can change your system assigned username to something more personal in your community settings. X

Mathcad worksheet as a function

DanMadsen
1-Newbie

Mathcad worksheet as a function

Hi forum

I was wondering if anyone know if it is possible to use a worksheet in a similar way as m-flies functions (Matlab) e.g. turn worksheet "alfa.xmcd" into a function which can be called from another worksheet?

Best regards Dan

22 REPLIES 22

Hello,

In main menu select "Insert" -> "Reference" and then choose your worksheet with prepared function:

Pic_1.PNG

Pic_2.PNG

Thanks Vladimir

The functionality I'm after isn't a single function in the worksheet, but the opportunity to use it as a function for an itterative analysis e.g. for monte carlo analysis which is seeded with random values every time the worksheet is called.

I've resently found out that the worksheet can be added as a scripted component (VBscript), but sofar I've only been able to assign a static value to the component output... I'm looking into this.

Btw. havn't written a single line of VBscript before, so if anyone know in advance that this is impossible to do with a scripted component, then please let me know

RichardJ
19-Tanzanite
(To:DanMadsen)

It used to be possible to do this with a scripted component in the worksheet with version 11, but not since then. It is still possible to do it with a scripted component in one worksheet calling another worksheet, if that would be an acceptable solution.

StuartBruff
23-Emerald II
(To:DanMadsen)

This is another long-standing feature request from back when Euler were a lad ... in fact, so far back that the original (Mathsoft Collaboratory) requests are no longer available.

I have a vague recollection of one of the more detailed requests mentioning in it the context of 'rectangular' Areas (rather than the current document wide Areas - see http://communities.ptc.com/message/183988#183988 for a more recent mention of the idea and a demo worksheet). I think one of the proposals took it up as part of a suggestion for an Object Oriented form of Mathcad, whereby a plot or Area, say, could be named and parameters got/set.

Stuart

PS. I did manage to create a Mathcad Component in M15 and to get a variable value from it (using ScriptObj.Worksheet.GetValue(varname)) but attempts to load the worksheet resulted in bad region errors.

RichardJ
19-Tanzanite
(To:StuartBruff)

This is another long-standing feature request from back when Euler were a lad

That late? Are you sure?

StuartBruff
23-Emerald II
(To:RichardJ)

Richard Jackson wrote:

This is another long-standing feature request from back when Euler were a lad

That late? Are you sure?

Well, I was a bit of a late-comer to the Collaboratory, but I have heard rumours that Apollonius made a similar request when he was beta-testing Mαθκαδ α ... and I'm pretty sure he wanted built-in geometric drawing tools as well.

Hamilton is a bit miffed that he still hasn't got direct quaternion support and I think Ricci is getting tensor every time he thinks about his feature request.

Did you know that Mathcad is the only major mathematical application whose name begins with "M" that doesn't properly support multi-dimensional arrays (MDAs)? Mathematica, Maple and Matlab (including the MuPad symbolic processor) all support MDAs. Do you think anybody's requested MDAs might be a useful feature for Mathcad? (even extended indexing for use with nested arrays and an empty array would be a decent starting point)

Stuart

RichardJ
19-Tanzanite
(To:StuartBruff)

MDAs! What a good idea!

PS. I did manage to create a Mathcad Component in M15 and to get a variable value from it (using ScriptObj.Worksheet.GetValue(varname)) but attempts to load the worksheet resulted in bad region errors.

I've been able to get a calculated value passed from a Mathcad worksheet component using the script command mentioned above. This component is simply another mathcad worksheet which is seeded with a random number and returns a different result every time it runs.

I thought if I made it into a function e.g. f(x):=component and called it from a sequential block, then I would be able to force a recalculation of the component. This however is not the case since it returns only the result of the first run. (calling it ten times returns the same constant ten times)

But on the bright side, it seems to work without bad region errors in my case.

Any forced recalculation of a component advice would be very much appreciated.

edit: I'm on Mathcad 15 M005 and M010

Could you post a worksheet with an embedded M15 component, please? I'd like to see if it's just a local problem I've got or whether there's some difference in the xml I can spot.

Thanks,

Stuart

Yup, I've made an ultra light version of a component by:

1) Making a simple worksheet and save

2) Inserting a component into a blank worksheet, choosen scriptable object (VBscript) and just chose my previous worksheet (no format selected)

3) Defined an output in the VBscript

Edit:

I've tried to run it again and now suddenly I get the bad region error. It even started happening in the worksheet which was working flawlessly (opened at least 30 times without any signs of trouble).

The original worksheet was created on M15 M005.

Dan Madsen wrote:

Yup, I've made an ultra light version of a component by:

1) Making a simple worksheet and save

2) Inserting a component into a blank worksheet, choosen scriptable object (VBscript) and just chose my previous worksheet (no format selected)

3) Defined an output in the VBscript

Edit:

I've tried to run it again and now suddenly I get the bad region error. It even started happening in the worksheet which was working flawlessly (opened at least 30 times without any signs of trouble).

The original worksheet was created on M15 M005.

Unfortunately, your worksheet didn't open.

I've just created an embedded Mathcad worksheet in M14, using a saved blank worksheet, put in some code to generate a vector of random numbers, wrapped it up in some VBA code at component level and it worked fine.

I saved the calling worksheet as M14 xmcd, closed it and re-opened it fine.

I then closed Mathcad and checked, using Process Explorer (www.sysinternals.com) - although Task Manager would do as well, and found an instance of Mathcad still open. I waited several minutes then stopped it.

I opened Mathcad again, opened the main worksheet without any problems (noting that Process Explorer showed 2 instances of Mathcad running).

If I increased the number of elements from 9 to 12 in rand(9), then rand worked fine. However, if I decreased it back to 9, I got an error. Some quick playing around indicated that I could increase the size of the vector without getting an error and I could decrease it again, provided I didn't decrease it below 10. Right-clicking on the component and invoking the Editor without making any changes, got rand working again and I could then decrease it still further without problem.

test - subworksheet 01.xmcd is the 'main' worksheet; I don't know whether test - blank 01.xmcd is needed, but I've attached it anyway.

Stuart

If I increased the number of elements from 9 to 12 in rand(9), then rand worked fine. However, if I decreased it back to 9, I got an error. Some quick playing around indicated that I could increase the size of the vector without getting an error and I could decrease it again, provided I didn't decrease it below 10. Right-clicking on the component and invoking the Editor without making any changes, got rand working again and I could then decrease it still further without problem.

I figured out how to fix this problem. See the attached. This version worked reliably every time I loaded the sheet or changed n (in MC15 M010).

I then closed Mathcad and checked, using Process Explorer (www.sysinternals.com) - although Task Manager would do as well, and found an instance of Mathcad still open. I waited several minutes then stopped it.

However, my fix does not fix this. The process is still left running. On the upside, if you close and reopen Mathcad, and load the worksheet again, you do not end up with three Mathcad processes! It seems to limit itself to 2.

I don't know whether test - blank 01.xmcd is needed,

Nope.

StuartBruff
23-Emerald II
(To:RichardJ)

Yes, your fix addresses that particular issue, but at a fairly high cost on my machine. What it appears to do is re-initialize the embedded worksheet which means runif returns the same sequence of values every time (albeit of varying length). It also imposes a noticeable performance overhead.

I can get round the first problem by adding a Seed statement prior to runif and making it dependent upon time(). Can't do much about the performance though.

However, back to the original use and the problem of Solve blocks which I've also suggested this as a solution for, I've enhanced the worksheet slightly by adding part of one of the Quicksheets as a component and shown two ways of using it as a function.

Stuart

Important Notes:

Note 1: If you want to make any changes to the embedded worksheet, you must save it using 'Save' from the main File menu whilst editing the embedded worksheet; if you don't, then the changes will be lost as soon as it's executed.

Note 2: Ensure you close all instances of Mathcad before opening worksheets with embedded Mathcad components; if you don't then it won't work (corrupted region upon load) but will reset your default worksheet size and toolbar positions/sizes.

PS. I hate this ruddy editor - it's Undo is useless

RichardJ
19-Tanzanite
(To:StuartBruff)

Yes, your fix addresses that particular issue, but at a fairly high cost on my machine. What it appears to do is re-initialize the embedded worksheet which means runif returns the same sequence of values every time (albeit of varying length).

I admit that I didn't notice that.

It also imposes a noticeable performance overhead.

I did notice that. However, I assume that the overhead would be constant, regardless of how long the worksheet takes to calculate, so for a long worksheet it would be less of an issue. I didn't actually test that to see if it's true, but it's moot anyway because...

... I think I have found another solution

big_grin.gif

The attached seems to work reliably for me in MC15 M010, with negligible overhead.

StuartBruff
23-Emerald II
(To:RichardJ)

Richard Jackson wrote:

... I think I have found another solution

big_grin.gif

The attached seems to work reliably for me in MC15 M010, with negligible overhead.

Ah, yes, that deserves a

http://communities.ptc.com/servlet/JiveServlet/downloadImage/2-185237-34469/16-16/big_grin.gifhttp://communities.ptc.com/servlet/JiveServlet/downloadImage/2-185237-34469/16-16/big_grin.gif

(I wonder what the cause of the problem is in the first place?)

Stuart

RichardJ
19-Tanzanite
(To:StuartBruff)

(I wonder what the cause of the problem is in the first place?)

Good question. I was actually a bit surprised that the recalculate fixed it.

Edit: Maybe I should qualify that statement a bit. Once I trapped the error so that the component didn't crash, I noticed that when the problem occurred VarType(w), said w was empty. So the GetValue was failing, which was because the SetValue was failing. So I just grabbed at the nearest available straw and tried a worksheet.recalculate, followed by a new SetValue. That doesn't answer your question about what the cause of the problem is, but at least it is an answer with a slightly higher information content than "Good question".

RichardJ
19-Tanzanite
(To:StuartBruff)

Note 2: Ensure you close all instances of Mathcad before opening worksheets with embedded Mathcad components; if you don't then it won't work (corrupted region upon load) but will reset your default worksheet size and toolbar positions/sizes.

I have not seen this problem at all. Maybe they have fixed it in M010.

... Forgot to add a screenshot on my other post ...

collab - 12 07 06 subworksheets 01.jpg

Well you on to something, I looked in the taskmanager and there we had a mathcad.exe process hanging.

Closed it, ran my simple test and it worked again.

Closed the worksheet and another mathcad.exe process where left hanging --> closed it again.

Saved it as .mcd (M11 format) ran it again and it still works.

The problem is an idle mathcad.exe process, how it relates to the length/size/number of commands in the component - I don't know, but shutting it down before relaunch of mathcad works.

RichardJ
19-Tanzanite
(To:DanMadsen)

Well, altohugh I said calling one worksheet from another still worked, it seems I was wrong. Something got broken at some point, and now it seems to fail some of the time, on a seemingly random basis. The attached worksheet "fixes" the problem, although the fix is not exactly elegant (it just traps the failure, and tries again).

RichardJ
19-Tanzanite
(To:DanMadsen)

Somehow I missed exactly what you are proposing, Back in version 11 it was possible to iterate a worksheet using a textbox or button embedded in the worksheet. That is now not possible.

Like you and Stuart, I have also tried in the past to create a component in a worksheet that is another Mathcad worksheet. Like both of you, I failed. I don't remember the exact details, just that I had lots of nasty problems, and eventually gave up. Unfortunately, I suspect it's not possible. Instead of asking, for example, Excel to call Mathcad, or Mathcad to call Excel, you are asking Mathcad to call Mathcad. It then gets itself in a twist, and hence the problems.

Mathcad worksheets as a cloud functions.

See please

http://twt.mpei.ac.ru/ochkov/WSPHB/Web-function-Power-Eng.pdf

Top Tags