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

Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X

minimum function

YA_10963798
12-Amethyst

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

13 REPLIES 13
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

 

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

 

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 !?

 

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

thanks a lot for you help

Announcements

Top Tags