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

Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X

New user, really need some help writing my first loop

Steve8
4-Participant

New user, really need some help writing my first loop

Hello, I am new to MathCAD and need some help writing a loop. I have done this in MATLAB before when I was in college, but it seems like MathCAD works a bit differently and I just can't figure it out.

 

I have two variables for which I am inputting to a starting value, say A:= 1.5 and Dp:=3. Then there are a few lines of equations that all use A and Dp, and eventually output a final value that I named Apercent. My goal is to have the program automatically increase the inputs A and Dp from their starting values until Apercent is at least 100.

 

 

My plan is to create a while loop that says "while Apercent is less than 100, A will increase by 1/16 at a time. But if I reach an iteration where increasing A no longer increases Apercent, stop increasing A and instead start increasing Dp by 1/16". But I can just not figure out how to write this in MathCAD. Can anyone please help me and show me the proper formatting?

1 ACCEPTED SOLUTION

Accepted Solutions
LucMeekes
23-Emerald III
(To:Steve8)

Your loop runs endlessly because:

It only stops when Apercent >= 100. So how does Apercent change during the loop...?

Just before the last statement in the While block, you assign Arep/Ar to Apercent. Within the while block you do NOT assign new values to Arep or Ar, which means that Apercent gets assigned the same value over and over again, and (apparently) that value is not over 100...Well, you do assign to Arep, namely A1+A2A3+A3a, but those values don't change in the program.

 

Some other remarks:

- Francesco is right, you shouldn't name the function the same as its internal variable. I renamed it to Result.

- if( <condition> , <action if true> , <action if false> ) is a function, like sin() and mean(), that you can just use as is. You don't need to precede it with a programming bar. I took all these items out. {That is different for an 'if' statement that you find on the programming tab, but you've not used that. (I'm as limited with Prime as Fred, for me Prime Express only, so I can't program it for you).}

- You can have evaluation with every definition such as when you define Lv := L - M, you follow it by an equals '=' to display the resulting value. What you should know that this sometimes results in unexpected behaviour of the sheet. I would advise against it. You did the same when you defined the program, now called Result(). You immediately evaluate it (by having that = follow it).  - don't - (That's why I took it off)

- In your program you assign a value to dttr, but it's used nowwhere in the program. So why assign it?

I think there's room for improvement, your turn again.

(Your worksheet opened in Prime 4, I'm assuming you're using Prime 4, otherwise you can't open my sheet.)

 

Success!
Luc

 

 

View solution in original post

9 REPLIES 9
Fred_Kohlhepp
23-Emerald I
(To:Steve8)

Rather than build a routine to search you might consider a solve block.  It may take some reading thru the help files, or looking thru this web site for examples but a solve block will hunt for the values of A and Dp that satisfy your conditions.  It will certainly find values for Apercent = 100.

 

But you can program what you're looking for.  Since I only have Express I cannot write programs or solve blocks; but someone will surely assist.

 

It would be better if you attempted something and posted your attempt rather than just a cry for help--you'll get better responses faster.  😉

Steve8
4-Participant
(To:Fred_Kohlhepp)

Thank you, I'll post what I've attempted very soon.

I chose the while loop over a solve block for few reasons. For my purpose
it is optimal to to increase A as much as possible before I even begin
changing Dp, and a solve block won't consider that. Also, the solutions
must be a multiple of 1/16th which a solve block won't recognize.


Right now my while loop is just running endlessly - I'll post it as soon as
I get back home to my PC
-MFra-
21-Topaz II
(To:Steve8)

Hi,
Mathcad 15 and Prime offer an extensive library of examples and instructions on how to use it, see the help. It has several toolbars that you can have at your leisure. I, for example, have arranged them as shown. Your problem, I understand, is to write a little program that performs simple operations. When you launch Mathcad it automatically opens an untitled worksheet. Click anywhere on the page, the red cross pointer will remain there. Then click on : (two points), the definition operator will appear: =. In the operator's right placeholder write the name of the small program you want to create, then click with the pointer to the right placeholder of the same operator. After that, click on the add Line of the programming toolbar..etc...see photo:

toolbars.gif

Steve8
4-Participant
(To:-MFra-)

Thank you for your help. I began writing the loop earlier and I am still having trouble with the syntax. I attached a pdf of what I'm doing. I have my inputs A and Dp there, a bunch of equations using those inputs that eventually pops out an Apercent value, and then at the end a while loop that tries to adjust A to get Apercent over 100. The problem Im having now is the loop runs endlessly. 

 

I attached an image (cropped some info out to reduce clutter), and my mcdx file. Any idea why the loops not working. I tested it by hand and it should be able to solve

 

-MFra-
21-Topaz II
(To:Steve8)

I can't help you, since my version of Prime has expired. However, I downloaded the file and opened it. The program containing the while loop should not have the same name as a variable contained in it (ie Apercent).

LucMeekes
23-Emerald III
(To:Steve8)

Your loop runs endlessly because:

It only stops when Apercent >= 100. So how does Apercent change during the loop...?

Just before the last statement in the While block, you assign Arep/Ar to Apercent. Within the while block you do NOT assign new values to Arep or Ar, which means that Apercent gets assigned the same value over and over again, and (apparently) that value is not over 100...Well, you do assign to Arep, namely A1+A2A3+A3a, but those values don't change in the program.

 

Some other remarks:

- Francesco is right, you shouldn't name the function the same as its internal variable. I renamed it to Result.

- if( <condition> , <action if true> , <action if false> ) is a function, like sin() and mean(), that you can just use as is. You don't need to precede it with a programming bar. I took all these items out. {That is different for an 'if' statement that you find on the programming tab, but you've not used that. (I'm as limited with Prime as Fred, for me Prime Express only, so I can't program it for you).}

- You can have evaluation with every definition such as when you define Lv := L - M, you follow it by an equals '=' to display the resulting value. What you should know that this sometimes results in unexpected behaviour of the sheet. I would advise against it. You did the same when you defined the program, now called Result(). You immediately evaluate it (by having that = follow it).  - don't - (That's why I took it off)

- In your program you assign a value to dttr, but it's used nowwhere in the program. So why assign it?

I think there's room for improvement, your turn again.

(Your worksheet opened in Prime 4, I'm assuming you're using Prime 4, otherwise you can't open my sheet.)

 

Success!
Luc

 

 

Steve8
4-Participant
(To:LucMeekes)

Hi, thank you very much for your help. I couldn't open your sheet, but I revised my program thoroughly per your suggestions, mainly in having it recalculate a new Arep each loop. However, I'm still having the endless loop problem. Do you see what's going wrong here still? Also since I can't open yours, maybe you could take a screenshot?

 

Thanks again

Steve8
4-Participant
(To:LucMeekes)

Disregard my last message, I got it working! I forgot to multiply Arep by 100 in the loop so I was getting impossibly small returns that were always under 100 - as soon as I fixed this the loop works perfectly. Thanks for the help!

LucMeekes
23-Emerald III
(To:Steve8)

Ah, I see you're using Prime 3.1.

Attached is my file in Prime3.1.

 

Success!
Luc

Top Tags