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

Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X

Translate the entire conversation x

stepwise function, or better?

AV_14389071
2-Explorer

stepwise function, or better?

Hi folks, 

My problem is a simple one - calculate the vertical overburden stress in a soil mass.  The simplified equation is just [ unit weight of soil ] * [ depth ].  However, in a layered system with a different unit weight for each layer, the calculation is best formed as the sum of the vertical stress increments. To me that looks like a stepwise function constructed as a series of IF-ELSEIF-ELSE statements as shown in my example below.  It works but there is surely a more clever way.

 

Lets say the bottom of layer 1 is z1 and the stress is p1, and the stress at some mid-depth in layer 2, say z2, is delta_p2, such that the stress at the point in question is p(z2) = p1 + delta_p2.  What is the most efficient way of structing that calculation as a function depth (z) so that it can be scaled to any number of layers.  My best idea so far is an "If" function to evaluate the stress at the bottom of each layer plus the portion of the n-th layer at the target depth.  Its  cumbersome.  In a 4 layer system, the function has 4 steps.

 

Obviously depth z and unit weight gamma are defined.  They are each single column matrices. For depth, z_top is the depth to the top of each layer, and the unit weight is define similarly. In this case z is negative to indicate depth below ground surface.

 

AV_14389071_0-1762274430800.png

 

9 REPLIES 9
LucMeekes
23-Emerald IV
(To:AV_14389071)

There's at least one thing you can do better. Rename each of those sigma'(z) occurrences inside the program to something else, say R.

As it is now, I suspect the program never ends, because it calls itself repeatedly, recursively.

 

Success!
Luc

Werner_E
25-Diamond I
(To:LucMeekes)



As it is now, I suspect the program never ends, because it calls itself repeatedly, recursively.

I would have thought so to that this would happen if no local function is defined which is the case for z>z.top[0.

But both MC15 and P11 throw an error in this case:

Werner_E_0-1762291135009.png

Werner_E_1-1762291230149.png

The error message in MC15 ("Type "2" can not be loaded.") is a bit strange but both seem to refuse to refer recursively to the main/worksheet function if a first pass showed that a function with that name is defined in one of the if branches.

Both programs detect the infinite recursion in case no local function of that name is defined anywhere in the program

Werner_E_2-1762291547869.png

Werner_E_3-1762291565474.png

Its also remarkable that the first error message from MC15 came in German (the language set in Windows) even though my language settings for Mathcad are to be in English.

 

So I guess that the program made by @AV_14389071  works OK for him as long as he does not provide a z value larger than z.top_0.

But nonetheless I agree that he should replace the local sigma function definitions for a simple variable or simply do without and use the return statement (which also means that he could do without the if_else and just use a couple of ifs).

 

 

Werner_E
25-Diamond I
(To:Werner_E)

Speaking of recursion, we can use it to make the function definition a little bit smaller which can be especially useful if there would be even more layers:

Werner_E_4-1762293164650.png

 

Or even shorter using a for-loop which works for any number of layers

Werner_E_7-1762294557151.png

 

stacking minus infinity on top of ztop was necessary because Prime unfortunately does not use short curcuit evaluation of combined Boolean expressions.

 

 

 

 

A better option might be determine which layers are below the required depth.  Use the built in function match , for example to find all those layers whose tops are below z.  

The use the summation operator to calculate the cumulative stress for those layers (if there are any) and add it to the stress at the bottom of the layer in which z lies.  

Stuart

 

(I’m not near my PC at the moment, and I’ve only got Mathcad Express, anyway. Mathcad Express has no programming, which can make solutions somewhat convoluted for those used to standard programming, as Express encourages functional programming)

LucMeekes
23-Emerald IV
(To:AV_14389071)

If you want to get rid of the if/else statements and have a more flexible way of expanding the set of layers, you may want to try this:

Define:

LucMeekes_0-1762293894415.png

and, just for the purpose of illustration:

LucMeekes_3-1762294149543.png

then this function:

LucMeekes_4-1762294170188.png

 

will give you gamma values for each value of z:

LucMeekes_5-1762294221853.png

Of course you can replace/redefine gamma.s with any other vector of expressions, just make sure there are as many as there are comparisons.
Your example would be covered by:

LucMeekes_7-1762295019057.png

There may be a more clever method to construct the vector with the gamma values, but it's late...

 

Success!
Luc

Like this is one way:-

Just negate z layer to positive depth.

Capture.JPG

Cheers

Terry

 

Werner_E
25-Diamond I
(To:AV_14389071)

Here are some ways to achieve what you are looking for.

All three approaches work, regardless of the number of layers, provided that the number of elements in ztop and gamma is the same, and that the values in ztop are in descending order.

All three functions are defined in a way so they return a zero value in case z is larger than the first (largest) value in ztop.

 

I already posted the solution using recursion:

Werner_E_0-1762348723133.png

 

Here an approach inspired by Terry using linear interpolation

Werner_E_1-1762348800440.png

 

And here a version using the "match" function as suggested by Stuart

Werner_E_2-1762348879494.png

 

EDIT: Added a further variant. Probable more conventional, using a for-loop and taking advantage of the fact the program execution is stopped when a "return" statement is encountered (otherwise we had to use a while-loop).

Werner_E_0-1762358068765.png

 

Comparison of the various methods

Werner_E_2-1762358250281.png

 

 

 

Prime 11 worksheet attached

 

 

 

Thought I would add some text and allow for the negative values of depth.

See file.

Hi @AV_14389071,

 

I wanted to see if you got the help you needed.

If so, please mark the appropriate reply as the Accepted Solution. It will help other members who may have the same question.
Please note that industry experts also review the replies and may eventually accept one of them as solution on your behalf.
Of course, if you have more to share on your issue, please pursue the conversation. 

 

Thanks,
Anurag 

Announcements

Top Tags