Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X
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
Solved! Go to Solution.
@YA_10963798 wrote:
Yes . I want it to be choose always the minimum value between the three valuesSo 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 sameIf the value bigger than 15 it will choose 15.
First, let's look at your limiting program and see where it's going wrong.
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).
Stuart
last gives the index of the last element in a vector, regardless of ORIGIN.
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
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.
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
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.
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.
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.
If that's what Yusra wants, of course.
Stuart
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"
making a comparison with the previous and next value !?
@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.
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
@YA_10963798 wrote:
Yes . I want it to be choose always the minimum value between the three valuesSo 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 sameIf the value bigger than 15 it will choose 15.
First, let's look at your limiting program and see where it's going wrong.
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).
Stuart
thanks a lot for you help
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
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
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?
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
Hi Stuart this is so helpful and thoroughly explained. I genuinely appreciate your help. Thanks
No problem, Yusra.
Stuart
Hi Stuart,
I found the thickness of the layer using this sample function ....
and then I found the average value of q in each layer
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
However, it didn't work . can you tell me where is the mistakes I made?
Thank you in advance,
Yusra
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?
Stuart