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

Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X

Given/Find in function definition doesn't work right - parameters passed in ignored

JasonC
1-Visitor

Given/Find in function definition doesn't work right - parameters passed in ignored

I'm trying to define a function with a Given/Find solve block inside. i.e. the function calls a Given/Find block.

 

I found this thread on how to do it

Embedding Solve block in a function - PTC Community

It had an example file:

https://www.ptcusercommunity.com/servlet/JiveServlet/download/30310-2824/collab%20-%20solve%20block%20demo%2001.mcd

 

The problem I run into is that the values I pass into the function (which is used by the Find block), is ignored. Well, not exactly ignored - I think it uses it like a guess value. It solves the system of equations, but the parameters I passed in are changed.

 

My file is attached.


TIA,
Jason

9 REPLIES 9
LucMeekes
23-Emerald III
(To:JasonC)

First off:

You cannot use an assignment (shows as :=  ) inside a Solve block. In a Solve block you describe constraints, you must use boolean equals (normall a bold =  ).

Luc

LucMeekes
23-Emerald III
(To:JasonC)

Then the result of the Solve block should contain all variables you want to solve for:

So after a small rebuild:

All constraints are satisfied.

Luc

There's something not right

This statement

     i0 = iavg - di / 2

is NOT a constraint. It is merely an initial value for the solver. That's why I used := instead of = (bold)
The correct answer is at the bottom of the sheet.  The correct answer for i0 is NOT equal to iavg - di/s

??

JasonC
1-Visitor
(To:JasonC)

Also:
find.png

Werner_E
25-Diamond I
(To:JasonC)

If you don't want "i.avg" and "di" to change, you should not solve for them. Just solve for the other 6 variables.

You have 6 variables to solve for, but seven equations. It looks like

is redundant (at least with the equations Ti() and Qt() you provide) and can be omitted. You had done this yourself in your last solve block where you said that the results would be correct. (For some reason you also had changed i.avg from 2.1 to 2 there). EDIT: Just noticed that you had not omitted that equations but that it was hidden underneath the Find region. Nevertheless the equation is redundant, other Mathcad would refuse to find a solution.

As your system is quite simple, you may also consider to solve it symbolically and use the resulting expression which are depending on i.avg, di and the two functions Ti, Qi, to calculate the variables you are looking for.

You would have to go for a symbolic solution before you define your functions and the guess values. Heres the numeric solve block:

Werner_E
25-Diamond I
(To:JasonC)

Just read your answer to Luc and that you want the guess value for i.0 to be dependent on the arguments i.avg and di.

This is a little bit tricky and can be done, by turning the solve block into an auxiliary function with three arguments and defining the real function findit to call it with the appropriate guess value:

JasonC
1-Visitor
(To:Werner_E)

Werner,

The functions Ti and Qt are placeholders; the actual functions are much more complicated and have no closed form. That was the first thing I tried. This sheet I attached is a much abbreviated, simplified version that illustrates the problem I'm having with the Give/Find block.

I will go and digest your answer now.  Thanks to both you and Luc.

JasonC
1-Visitor
(To:JasonC)

OK this lesson from Werner was the key:
"If you don't want "i.avg" and "di" to change, you should not solve for them. Just solve for the other 6 variables."

I only added it to the variable output list as a short cut to see if it changed it or not. But now I know that actually allows Find to change it.


Also, it looks like placing a guess value using the assignment := operator inside of the Given/Find block is legit.

Lastly, when defining variables before the Given/Find block, it seems the values you assign to them matter, because they seem to be also used as a first guess, even if the equations (bold '=') appear to over-ride them.

Attached is the fixed version, for posterity.

Thanks again all

Werner_E
25-Diamond I
(To:JasonC)

Yes, you are right. Guess values can be provided before the block begins with "Given" (which is the usual procedure) but also inside the solve block.

BUT

Yes, it overrides it, but it always uses the values for i.avg and di you have provided above the block. It DOES NOT USE the function arguments, as you obviously intended. So in your case the guess value for i.0 always is 1, no matter of the values for i.avg and di you provide as function arguments.

It seems that Mathcad has no problems finding a suitable solution with that guess, so it would be more clear if you simply place i.0:=1 before the solve block and delete the assignments for i.avg and di there.

If you really want the guess value dependent on the function arguments, you will have to resort to some kind of tricky construction like the one I had shown above in my secon answer.

Regards

Werner

P.S.: If you don't have any further questions, you should close the thread.

Announcements

Top Tags