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

Define Piecewise Function from Input Table

SOLVED
Regular Member

Define Piecewise Function from Input Table

All, 

 

I'm attempting to start what should be a simple geotechnical effective stress calculation. My first step is defining a soil unit weight profile as a function of depth (z) in an efficient and robust method. In the past, I've used "if statements" to control this however they are cumbersome and not very sophisticated. An example of what I've done in the past is shown on Page 2 of the attached worksheet. 

 

On Page 1 I attempted using "vlookup", however I ran into numerical issues and precision errors due to TOL...ideally, I would like to end up with a function that is as simple as the "vlookup" approach, but with the precision of the function defined by the "if statement". Any suggestions would be appreciated...I've attempted "for loops" and "while loops" without any success. 

 

Thanks in advance!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted

Re: Define Piecewise Function from Input Table

The lack of precision of "match", "lookup", etc. (especially when used with units) can be very annoying quite often.

I would suggest implementing your own specialized lookup function to do the job better.

B.png

 

View solution in original post

12 REPLIES 12
Highlighted

Re: Define Piecewise Function from Input Table

Hi,

 

Look up linear interpolation in help.  You can arrange a matrix of depths and values to interpolate.

For example here is one that changes from compass direction to polar plot direction.  The tolerance is built in.

Capture.JPG

Highlighted

Re: Define Piecewise Function from Input Table

Wouldn't

2018-10-03_13-46-02.png

be a simpler but equivalent solution to your compass problem?

 

Highlighted

Re: Define Piecewise Function from Input Table

The lack of precision of "match", "lookup", etc. (especially when used with units) can be very annoying quite often.

I would suggest implementing your own specialized lookup function to do the job better.

B.png

 

View solution in original post

Highlighted

Re: Define Piecewise Function from Input Table

I can't demonstrate it because "linterp" is a "premium function", but this should do what you want:

Capture.PNG

Highlighted

Re: Define Piecewise Function from Input Table

Or, you look up the help info on VLOOKUP and find that there's a way around TOL dependency:

LM_20181003_Vlookup.png

 

Success!
Luc

Highlighted

Re: Define Piecewise Function from Input Table

Nice idea and it even can be streamlined a little bit:

B.png

Drawback may be that it fails for input values greater or equal to 30 ft.

 

Highlighted

Re: Define Piecewise Function from Input Table

Thanks so much for the assistance on this. There were a lot of great solutions to this question, but implementing a custom look-up function seemed to be the most efficient method in defining my profile of soil properties as a function of depth. 

 

Thanks again!

Highlighted

Re: Define Piecewise Function from Input Table

Does anybody here use "SMath Studio"? I'm trying to recreate this sheet using SMath, but can't seem to get the same lookup function to work out (see attached screenshot). SMath - Function Error.JPG

Highlighted

Re: Define Piecewise Function from Input Table

Attach the sheet... NOT just a picture.

 

Luc

Highlighted

Re: Define Piecewise Function from Input Table

There is no "else" (or "otherwise" in Mathcad) in my suggestion.

The for-loop simply looks for the correct index which is then used in the last line.

Instead of using a for-loop with break it would be a cleaner approach to use a while loop.

That means following the rule  "Use a for-loop only if the number of iterations is known", which I had ignored in favor of comfort 😉

I am not sure but I also think that there is no system variable "Origin" in SMath and vector indices always start with 1 which the routine would have to consider.

Highlighted

Re: Define Piecewise Function from Input Table

It was very long ago that I test drived SMath so I downloaded and installed it and gave it a try.  Feels a bit uncomfortable at first and I was missing a comprehensive help. Had to use a search machine to find help for various topics. Especially demanding was to find out how to add a new line in a program without adding a new vertical line, too. Found no keyboard short but just a way to do it with the mouse 😞

I found no equivalent to Mathcad's "ORIGIN", so I assume indexing starts with 1 and you can't change that.

Also I found no way to get rid of the "else" branch, which is not needed in our example. You may put anything there, a zero would be just fine. I decided for "continue" as in our case that's exactly what happens.

I also include the "cleaner" version which uses a "while" loop.

B.png

 

BTW, I remember there was an installer package which also included the most important add-ons and settings, which made for a good start. At first glance I was not able to find anything like that now and installed the pure program alone (and missed a lot, of course).

While I am aware that this is not the SMath forum - two questions anyway:

Does anybody know if there are that kind of "all-inclusive" installation packages still available?

I also found that many menus are unusable because I use a notebook with a 4K display. Does anybody know if there is a fix for this. The pic below show what I am experiencing and talking about:

B2.png

EDIT: I was not able to attach the SMath file directly as I experienced the "The contents of the attachment doesn't match its file type" error message. We know this problem very well as it took endless until this problem had been finally resolved for Mathcad files. So it was needed  to zip the file.

 
Highlighted

Re: Define Piecewise Function from Input Table

OK, the lookup-functions that were presented here have no error check anyway but I thought that the "while" version should at least be as good as the "for" one, So I guess its a good idea to prevent the index going up to high:

B.png

Announcements