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

Community Tip - Want the oppurtunity to discuss enhancements to PTC products? Join a working group! X

using Solve in a loop

AH_10527716
4-Participant

using Solve in a loop

I am having trouble understanding why I can't get a solution using the defined variables but am able to get one when I put in a value for the variable.  My issue arises because the values X, Y and Z depend on Twall.  I want to be able to loop this with an update to Twall at each dt.  Any help would be appreciated.

 

mathcad prime9

 

.

AH_10527716_0-1724695810465.png

 

 

 

 

ACCEPTED SOLUTION

Accepted Solutions
Werner_E
25-Diamond I
(To:AH_10527716)


@AH_10527716 wrote:

I figured it out.  I can't have the "=" after the X1 and Z1.  I was just checking units, but for some reason it didn't like it,

 

thanks


This is because you cant numerically evaluate a function definition!

After all, which result would you expect from f(x):=x^2= ?

Even if you already had defined a value for a variable x before - the x in the function definition is a formal function argument and has nothing to do with that variable x.

 

View solution in original post

18 REPLIES 18
LucMeekes
23-Emerald III
(To:AH_10527716)

Please attach the Prime worksheet (.mcdx file).

 

Other than that: I guess you're not interested in the complex answers for T, nor the negative one, so only the first answer is needed.

And you only need a numeric answer. In that case it's better to use the root() function, instead of solve. You can set it to not give you negative or complex answers by supplying two extra parameters that give limits of the range within which you expect your answer.

Look it up in the help information of the program.

 

Success!
Luc

AH_10527716
4-Participant
(To:LucMeekes)

Thanks Luc.  What I am doing is a transient heat transfer case.  Yes, I only need the positive root.  But, I need to use that root and resolve for all the variables after a time-step and resolve for Twall.  Repeat until Twall is equal to Thot.  I know how to do the programming, I just can't get the initial part to work.  My worksheet has some other stuff at the bottom that I was trying to get to work as well.

 

thanks

LucMeekes
23-Emerald III
(To:AH_10527716)

This is what I'm talking about:

LucMeekes_0-1724711393006.png

Now, I don't see you calculating new values of A,B,C,D,E,F,X,Y or Z based on this temperature value. You're using the same inputs over and again. Then there's no point in iterating.

 

Success,

Luc

 

ttokoro
20-Turquoise
(To:AH_10527716)

image.png

AH_10527716
4-Participant
(To:ttokoro)

thanks.. 

I am not sure what happened.  it was working

AH_10527716_0-1724955357059.png

 

LucMeekes
23-Emerald III
(To:AH_10527716)

You should attach the prime worksheet file.

 

Success!
Luc

AH_10527716
4-Participant
(To:LucMeekes)

I figured it out.  I can't have the "=" after the X1 and Z1.  I was just checking units, but for some reason it didn't like it,

 

thanks

Werner_E
25-Diamond I
(To:AH_10527716)


@AH_10527716 wrote:

I figured it out.  I can't have the "=" after the X1 and Z1.  I was just checking units, but for some reason it didn't like it,

 

thanks


This is because you cant numerically evaluate a function definition!

After all, which result would you expect from f(x):=x^2= ?

Even if you already had defined a value for a variable x before - the x in the function definition is a formal function argument and has nothing to do with that variable x.

 

AH_10527716
4-Participant
(To:Werner_E)

Hi again -

 

I am having trouble updating the value of Ai based on the ratio of z/L  I am not it is updating at all.  Ai should increase by the ratio each time step.  The sheet is messy

 

thanks

 

AH_10527716_0-1728584211647.png

 

Werner_E
25-Diamond I
(To:AH_10527716)

Not sure what you are trying to achieve.

Initial value of Ai is 11.08 ft^2, but this value isn't used anywhere.

Starting with z=1 the last value of Ai gets multiplied by z/L, making it smaller an smaller.

At least thats what you have programmed.

If you let the program return Ai1 instead of Twnew you can see the values of Ai calculated:

Werner_E_0-1728608933233.png

Could it be that you had  Ai+Ai*z/L=Ai*(1+z/L) in mind instead of Ai*z/L ?

Here the modified program. I streamlined it by omitting the unnecessary variable T1 ( = dt) and its also not necessary to create that many vectors A, B , etc. if the only vector you are interested in is Twnew:

Werner_E_1-1728609636846.png

You will notice the two gaps in the plot. These stem from the root function returning non-real solutions with a very tiny imaginary part - presumable because of numerical inaccuracies.

Werner_E_2-1728610300395.png

You may cope with this by taking the absolute value or by using just the real part of the solution

Werner_E_3-1728610369070.png

P9 worksheet attached

 

AH_10527716
4-Participant
(To:Werner_E)

thanks for that,.  Ai, as you can tell, an area.  Basically, what it represents is a column filled with sand that is being drained at 1" per second.  So, at time=0 Ai is small, and at the end of the time it is Ai*1..  So, as z approaches L it should get closer to 1, right?  that is what I was trying to program.  Thanks again for the help.

Werner_E
25-Diamond I
(To:AH_10527716)

But you define Ai to be 11.08 square feet right from the start! at time 0?

So what area should it be when z=L ?

 

In case the definition Ai=11.08 ft^2 is meant to be the end value at z=L, the following may be what you are looking for?

I also got rid of the external definition of dt:=0 and also the external creation of time vector T.

Werner_E_1-1728663626704.png

Remark: Using just the real part of the result of root seems not to be necessary now. There are no gaps in the plot when the "Re(..)" is removed.

 

AH_10527716
4-Participant
(To:Werner_E)

thanks.  That was my problem - the redifining of Ai.. that is why I was using Ai1 and then setting Ai = Ai1, but that must not be correct.

Werner_E
25-Diamond I
(To:AH_10527716)

Using a variable Ai1 like you tried would do the job as well, of course:

Werner_E_0-1728678781200.png

Here the end value Ai is not touched in any way and its Ai1 which is increased step by step.

Your initial program, while a bit too elaborate, would work OK, too, if you delete the line Ai <-- Ai1[z and if you use Ai1[z in the calculations for A[z and B[z instead of Ai.

 

AH_10527716
4-Participant
(To:Werner_E)

I am now trying to get the cooldown (below the else statement) to work, but for some reason it is not starting at the last value of Twall, but jumping to Tamb.

AH_10527716_0-1729709638974.png

AH_10527716_1-1729709699606.png

 

 

Werner_E
25-Diamond I
(To:AH_10527716)


@AH_10527716 wrote:

I am now trying to get the cooldown (below the else statement) to work, but for some reason it is not starting at the last value of Twall, but jumping to Tamb.


'Last' value of Tamb? Tamb isn't changing, its constant 400 K.

I also see no reason why there should not be a discontinuity at l=700.
After all the else branch is using completely different calculations.

Only dt continues to increase the same way, but the functions you solve for l=699 and l=700 seem to be completely different. At least I can see no reason why the results should be the same.

You may try to write out both calculations, for l=699 and l=700 so you can see where and why there is a difference you don't expect.

 

BTW, not that it would  make any difference, but I am a bit confused as of

Werner_E_0-1729723669798.png

Neither putting L in the nominator AND denominator, not that multiplication by Twnew seem to have and effect.

 

Werner_E
25-Diamond I
(To:Werner_E)

Here you see that values of the coefficients for l=699 and l=700 along with the results of the corresponding equations

Werner_E_1-1729724635720.png

 

Announcements

Top Tags