Community Tip - Your Friends List is a way to easily have access to the community members that you interact with the most! X
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
I see no reason at all to use a scripted component, see below.
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
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:
Do you understand whta I mean? Can you see now why I was looking for something like this?
Regards,
Guilherme Nunes
Yes, I sort of get the gist
Mike
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
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:
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
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.
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