Skip to main content
14-Alexandrite
October 1, 2024
Solved

minimum function

  • October 1, 2024
  • 2 replies
  • 4136 views

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

 

Best answer by StuartBruff

@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

2 replies

23-Emerald V
October 1, 2024

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

 

14-Alexandrite
October 1, 2024

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

23-Emerald V
October 1, 2024

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

 

25-Diamond I
October 1, 2024

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.

23-Emerald V
October 1, 2024

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

25-Diamond I
October 1, 2024

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