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

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

Creating a "For loop" using scripts inside a worksheet:

gapnunes
1-Newbie

Creating a "For loop" using scripts inside a worksheet:

Hello everybody,

I'm trying to create a script that imitates a for loop tool inside a worksheet. I have already read a lot about it on this forum but I don't think that it is perfect clear, so, I'm proposing here to we define a tool once for all.

I know that Mathcad has lot of tool to solve looping problems but some times the equations inside a worksheet are so many and you don't want to polute they with one more variable like F(x,y,w,z,a,b,c,d). So that is the reason to develop a for loop using scripts.

I've written a worksheet, but it has problems, could someone help me on this? It is attached.

Regards,

Guilherme Nunes

15 REPLIES 15
MikeArmstrong
5-Regular Member
(To:gapnunes)

I see no reason at all to use a scripted component, see below.

Clipboard01.jpg

Attached is the worksheet indicating the simple loop. I would highly recommend working with ORIGIN = 0, as most pre-defined Mathcad functions are based on this.

Mike

Mike,

that is just an example, I intend use this with more complex equations.

Can you give me an example of problem I can have using the ORIGIN = 1? I didn't know it could happen.

Regards,

Guilherme Nunes

MikeArmstrong
5-Regular Member
(To:gapnunes)

that is just an example, I intend use this with more complex equations.

Can you give me an example of problem I can have using the ORIGIN = 1? I didn't know it could happen.

I'm not quite sure what you have asked for here.

It doesn't matter how complex the equations get. I see no reason for the use of scripted components.

Mike

Mike,

Usually it is usefull for:

  • simulations of high complex worksheets when you have to evaluate the same worksheet for 1000 times.
  • Optimization, and you have to evaluate the entire worksheet many times.
  • It is usefull and you have problems to develop a new function because this way you can see in detail each variable evaluation.
  • Probaly you work with MATLAB too, and it is very easy to make a rotine run for 1000 times no mater how complex is it. You have just to write "For i = 1 to n" in the begging and an "End" on the end of the rotine. It is just simple like that. It is not necessary to rewrite your equations.

Do you understand whta I mean? Can you see now why I was looking for something like this?

Regards,

Guilherme Nunes

MikeArmstrong
5-Regular Member
(To:gapnunes)

Yes, I sort of get the gist

Mike

RichardJ
19-Tanzanite
(To:gapnunes)

Which version of Mathcad are you using? IN MC15, when I enable the calculation of j I get an infinite loop (which is what I would expect to happen).

I wrote something similar back in the days of version 11 - see the attached worksheet. I have never managed to get it to work properly in any subsequent version (although the exact failure mode varies). I think the problems you are having are not going to be fixable either.

Incidentally, you do not need to assign i to the square root of minus 1. It's built in. Type 1i in the Mathcad worksheet. When you move the cursor out of the expression it will change so that you just see i (this also works for 1j).

Richard,

I will analyze your worksheet, very nice. At beggining I got all results equal to 3.168, it didn't changed, do you know why?

I don't understand why my worksheet gets a infinite looping, strange, it seems that the j value are not being changed correctly.

Thanks for the 1i and 1j tips.

Regards,

Guilherme Nunes

RichardJ
19-Tanzanite
(To:gapnunes)

I will analyze your worksheet, very nice. At beggining I got all results equal to 3.168, it didn't changed, do you know why?

Only in general terms. In version 12 and later it looks like the worksheet and the script are running in different threads, and there is no synchronization between them. I forget what it did in version 12, but in version 13 the GetValue throws an error every time. That makes sense if the script executes so fast it tries to get the value before the worksheet has calculated it. In version 14/15 the first time the component executes it does the same thing, so all the output values are zero. However, it has set the value of Number1 at the last value in the vector, so The_Result1=3.168. The second time the component is executes it picks up this value each time, rather than throwing an error. I don't really understand why Mathcad 13 doesn't do that as well.

I tihnk the fact that you are incrementing j in the component and using it in the worksheet as input to the component is forcing the threads to stay synchronized.

I don't understand why my worksheet gets a infinite looping, strange, it seems that the j value are not being changed correctly.

It can't be chnaged correctly. You use SetValue to chnage it, but that binds the variable at the top of the worksheet. If the j:=1 assignment is active that overrides it, so the component always sees j=1.

Hello everyone,

I have done some changes on the worksheet, now, is not necessary anymore to turn off the first "j" definition. However, I still have problems with the "j = j + 1" command. I can't understand why the j value stops at j = 4. I have to restart the script to let it run forward, very strange. Has anyone an idea?

see attached the new revision.

Regards,

Hey guys,

I think I solved the problem, I was reading a script written by Xavier and I sow that is necessary to turn off the worksheet autocalculation mode before set the variable value, like:

"Worksheet.SetOption mcAutocalc, False"

So, i think that the first step is done, the most simple for loop is scripted, now there is more that can be done.

Looking forward:

  • We could develop betters tools following this model. There are a lot of additional taskes to be done like use buttons to make j = j+1, etc...
  • We could try to find a way to recalculate only the area inside the "FOR-LOOP". I don't know if there it is possible. This way we are going to make the calculation faster, once is not necessary to recalculate the entire worksheet.
  • It would be nice if was possible to have two "FOR-LOOP" inside the each other, this way one has to wait for the other finish to start, I don't know exactly but is a good next step.
  • It would be nice deal with all types off variables like numeric complex, strings and matrix.
  • Other idea is to use the text on the TextBox to use as Inputs, it would be nice.
  • Any one has more ideas about it?

The worksheet is attached, it was writeen in MC15.

Thanks a lot for the help and I hope to hear more from you guys about it.

Regards,

Guilherme Nunes

Hi everyone,

See attahced an improved revision with push button to restart the process,

Regards,

Guilherme Nunes

RichardJ
19-Tanzanite
(To:gapnunes)

I'll look at them later. Maybe not even today though, because I have a lot of real work I have to do first. I can see a couple of possible improvements immediately.

RichardJ
19-Tanzanite
(To:RichardJ)

I'll look at them later. Maybe not even today though, because I have a lot of real work I have to do first.

I haven't forgotten. I am just really busy with real, boring, but paying, work right now.

Hello again,

I just want to share a worksheet using another one as a function inside the scrpted FOR LOOP. It doesn`t have anything new, just the worksheet working as a function.

I hope you enjoyn it,

Regards,

Guilherme Nunes

Hello guys,

I just want to share a "FOR LOOP" using references. The concept is not very smart, but it is very simple and it works fast. There are two worksheets attached, WA and WB, just save both on the same folder and open WA. In resume for each WB reference added we have one loop. You just have to write what you want into WB.

And the best point, it doesn`t uses scripts.

That`s it,

Regards,

Guilherme Nunes

Top Tags