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

minimum function

YA_10963798
14-Alexandrite

minimum function

I wrote a program to correct the value of q. I wanted to choose always the minimum value and I put the maximum limit to be 15 MPa , I don't know why it is not working . Do you know how to solve this problem?

Thanks 

YA_10963798_0-1727795104689.png

 

ACCEPTED SOLUTION

Accepted Solutions
StuartBruff
23-Emerald III
(To:YA_10963798)


@YA_10963798 wrote:
Yes . I want it to be choose always the minimum value between the three values
So first it compares it to the previous value and choose the minimum and than to 12 MPa and choose the minimum. And then to the after value and choose the minimum. … this when the value is between (0 and 12)
If the value between (12 and 15) it will remain the same
If the value bigger than 15 it will choose 15.

First, let's look at your limiting program and see where it's going wrong.

 

2024 10 01 E.png

 

If you look at the second value (index O+1), you want the minimum of qmin and the values at O and O+2, which are 12 MPa, 11.4 MPa and 15.8 MPa, respectively.  The minimum of these is 11.4 MPa, which is what we see in result.  

 

Rather than max(qmin,qclongi-1,qclongi+1) what you want is max(qmin,min(qmax,qclongi-1,qclongi+1)).  IOW, you get the minimum of qmax and a values two neighbours and then the maximum of that and qmin.  The min returns a value with a maximum value of qmax.  The outer max returns the min value or qmin.

 

And then there are the end values.  I've used q to limit them because q has the same values as qclong, and it's less typing and horizontal space (thus making it slightly more legible).

 

2024 10 01 F.png

 

Stuart

View solution in original post

20 REPLIES 20
StuartBruff
23-Emerald III
(To:YA_10963798)

last gives the index of the last element in a vector, regardless of ORIGIN. 

 

StuartBruff_1-1727796449727.png

 

Is there any particular reason that you've excluded qc;long's last row ?  last(qc;long)-1

 

Stuart

 

YA_10963798
14-Alexandrite
(To:StuartBruff)

actually, I tried to let i works without doing that, but it shows error that the last value can't be longer than i so I exclude it, it should be included though.

so the reason is just to get the program working

StuartBruff
23-Emerald III
(To:YA_10963798)

Edited to add larger screenshot.  The small version is a bit hard to read on 4K laptop screens.

 

If all you want to do is restrict the vector values to given limits, then I'd recommend that you use a general purpose function that is tailored for that purpose.  Try the function limit shown below.

 

2024 10 01 C.png

 

Stuart

 

YA_10963798
14-Alexandrite
(To:StuartBruff)

What I want the program to do is to always choose the minimum value . I have to three values q , qmin, qmax..

q is the vector I want to test,

q min = 12 MPa,     qmax2= 15 MPa 

q is the vector  which  I want to always be less than the two max values . 

For example the program will compare the q to qmin and will choose the minimum between them. if q= 10 and qmin=12 it will choose 10.

less than 12 will always remain the same 

 

and if q is between 12 -15 it will remain its value. 

But if q is bigger than 15 it will choose 15. 

so the result will be between the smallest value which can be zero and the maximum value which is 15 

Werner_E
25-Diamond I
(To:YA_10963798)

What you write does NOT mean that you always want the minimum value to be chosen.

On contrary!! The minimum value 12 MPa has NO EFFECT at all! Its not used!

A value remains the same if its smaller than qmax=15 MPa and only values above 15 MPa are corrected down to 15 MPa.

 

You still can use one of the "imit" functions which were posted by Stuart (using if's) and me (using min & max) if you set the minimum value there to minus infinity or, of your values are all positive, you may also set  it to zero.

 

 

Werner_E
25-Diamond I
(To:YA_10963798)

Its not clear to me what you are trying to achieve, especially the second "min(...)" expression where you ignore the element itself but rather choose the smallest of qmin and the two values surrounding the current element.

The reason you have to stop your loop one before the last element is because you are accessing q... with index i+1

It sure would help if you describe in more detailed words what you are trying to achieve, provide one or more explicit examples and attach a short(!!) worksheet with demo/dummy data to play with.

 

To limit a value v between a min (a) and a max (b) value you may use

limit(v,a,b) := max(  min(v, b), a)

Use vectorization to apply to a vector argument.

StuartBruff
23-Emerald III
(To:Werner_E)

It wasn't clear to me, either.  A simpler version of the program would do the trick, and use else if and else to handle the different cases, rather than evaluate q[i twice.  And it handles the two edge cases.

 

2024 10 01 D.png

 

If that's what Yusra wants, of course.

 

Stuart

Werner_E
25-Diamond I
(To:StuartBruff)


If that's what Yusra wants, of course.

I am not sure.

There must be a reason why he used that strange use of "min"

Werner_E_0-1727804169465.png

making a comparison with the previous and next value !?

 

YA_10963798
14-Alexandrite
(To:Werner_E)

Yes . I want it to be choose always the minimum value between the three values
So first it compares it to the previous value and choose the minimum and than to 12 MPa and choose the minimum. And then to the after value and choose the minimum. … this when the value is between (0 and 12)
If the value between (12 and 15) it will remain the same
If the value bigger than 15 it will choose 15.

Werner_E
25-Diamond I
(To:YA_10963798)


@YA_10963798 wrote:
Yes . I want it to be choose always the minimum value between the three values

"the" three values? which values? If you mean q, qmin and qmax, this would be equivalent to set all values above qmin to qmin and let values below qmin unchanged!?


So first it compares it to the previous value and choose the minimum and than to 12 MPa and choose the minimum. And then to the after value and choose the minimum. … this when the value is between (0 and 12)
If the value between (12 and 15) it will remain the same

WHY??? You just wrote that is should be smaller than the previous value!? Why should it remain the same when its between 12 and 15?

 

Sorry, but your explanations do not make it any clearer for me.

StuartBruff
23-Emerald III
(To:Werner_E)

If I read her intent correctly, Yusra doesn't want qi to be the minimum of qi and qmin, but (if qi < qmin) for qi to be qmin if both of qi's neighbours are greater than qmin, but the lower value of the two neighbours if both are greater than qmin, provided they are less than or equal to qmax. 

 

Consequently, if qi < qmin and qi-1 and qi+1 are > qmin, the lower value is wanted (capped at qmax).  Let qi = 11, qi-1 = 13 and qi+1 = 14, then the program should select qi-1.

 

If I read the intent correctly ...

 

Stuart 

StuartBruff
23-Emerald III
(To:YA_10963798)


@YA_10963798 wrote:
Yes . I want it to be choose always the minimum value between the three values
So first it compares it to the previous value and choose the minimum and than to 12 MPa and choose the minimum. And then to the after value and choose the minimum. … this when the value is between (0 and 12)
If the value between (12 and 15) it will remain the same
If the value bigger than 15 it will choose 15.

First, let's look at your limiting program and see where it's going wrong.

 

2024 10 01 E.png

 

If you look at the second value (index O+1), you want the minimum of qmin and the values at O and O+2, which are 12 MPa, 11.4 MPa and 15.8 MPa, respectively.  The minimum of these is 11.4 MPa, which is what we see in result.  

 

Rather than max(qmin,qclongi-1,qclongi+1) what you want is max(qmin,min(qmax,qclongi-1,qclongi+1)).  IOW, you get the minimum of qmax and a values two neighbours and then the maximum of that and qmin.  The min returns a value with a maximum value of qmax.  The outer max returns the min value or qmin.

 

And then there are the end values.  I've used q to limit them because q has the same values as qclong, and it's less typing and horizontal space (thus making it slightly more legible).

 

2024 10 01 F.png

 

Stuart

YA_10963798
14-Alexandrite
(To:StuartBruff)

thanks a lot for you help

YA_10963798
14-Alexandrite
(To:StuartBruff)

Hi Stuart, 

Do you have time to help me with this one more time?

I need to edit the condition there to be like if the layer thickness is 2 m or less compare q to qmin=12MPa and choose the minimum value. 

if the layer thicknesss is bigger than 2m compare qc to qmax =15MPa and choose the minimum. I tried to do it but it shows error

YA_10963798_0-1728814645394.png

 

StuartBruff
23-Emerald III
(To:YA_10963798)

I can look at it later, Yusra.  In the meantime, what is t?  q is a pressure, but to know the thickness of any q's layer, you need to know the depth corresponding to each q.  Probably you're best bet is to create a vector of layer thicknesses and a function to look up the thickness of the layer at depth z (or a vector containing the layer thickness for each z) and then substitute t with the calculated thickness at each q's depth (z).  Hope that makes sense.

 

Stuart 

StuartBruff
23-Emerald III
(To:StuartBruff)

I've had a quick look and used my simpler test data worksheet to try out a solution. 

 

Unfortunately, I'm a Prime 10 user so can't save as Prime 9.  It might be worth you installing Mathcad Prime 10 on one machine just so that you load Mathcad Prime 10 worksheets and view them - what appears on the screen isn't necessarily representative of what's on the worksheet.

 

I intuitively feel there's a simpler solution that I'm overlooking, but I appear to be having a broken brain day and the obvious isn't dancing about in front of my eyes.

 

Is this what you intended?

 

2024 10 13 A.png

 

2024 10 13 B.png

 

2024 10 13 C.png

 

I put in the layerinfo table because my tiny brain was getting confused by what was meant by the "top" of a layer.  I assumed, as depth is negative that the first layer starts at the "surface", ie highest depth within z, and continues down beyond the deepest layer top (-11.1 m, in this case).  So, the highest layer runs from the surface to the first ztop depth, and the lowest level runs from -11.1 m down to -40 m (in this case).

 

Stuart

YA_10963798
14-Alexandrite
(To:StuartBruff)

Hi Stuart this is so helpful and thoroughly explained. I genuinely appreciate your help. Thanks 

StuartBruff
23-Emerald III
(To:YA_10963798)

No problem, Yusra.

 

Stuart

YA_10963798
14-Alexandrite
(To:StuartBruff)

Hi Stuart, 

I found the thickness of the layer using this sample function ....

YA_10963798_4-1728970309067.png

 

YA_10963798_0-1728969922530.png

and then I found the average value of  q in each layer

YA_10963798_1-1728970011223.png

YA_10963798_3-1728970200826.png

 

Then I tried to implement the condition if q which is if the thickness of the layer is 2m or less>> q must be equl or less than 12MPa... 

and if the thickness is bigger than 2m q should be less than 15 MPa 

YA_10963798_2-1728970161079.png

However, it didn't work . can you tell me where is the mistakes I made?

Thank you in advance, 

Yusra 

StuartBruff
23-Emerald III
(To:YA_10963798)

Without looking at your data, I can't be sure, Yusra.  However, if your words specify what you want, then I think the problem lies when limiting layers with thicknesses less than 2 m.

 

You take that minimum of 12 MPa and qavg.  Did you mean qavgi?  

 

 

2024 10 15 A.png

 

Stuart

Announcements

Top Tags