0
$\begingroup$

I need to define a function that behaves as a piecewise function, but will return a string instead of a value. Part of the function that I have written is provided below. The full function continues for several additional lines with additional values of od and different values for the slot positions. The line of code for od == 1.050 illustrates where I need to return the string "XXS". The values od and tol are input values to the notebook. The notebook calculates T.

pipeschedule[od_, tol_, T_] := 
  Which[
    od == 0.405, Piecewise[{{#1, T <= (tol*#2)}, {#3, (tol*#2) < T <= (tol*#4)}}, "___"]&[40, 0.068, 80, 0.095],
    od == 0.540, Piecewise[{{#1, T <= (tol*#2)}, {#3, (tol*#2) < T <= (tol*#4)}}, "___"]&[40, 0.088, 80, 0.119],
    od == 0.675, Piecewise[{{#1, T <= (tol*#2)}, {#3, (tol*#2) < T <= (tol*#4)}}, "___"]&[40, 0.091, 80, 0.126],
    od == 0.840, Piecewise[{{#1, T <= (tol*#2)}, {#3, (tol*#2) < T <= (tol*#4)}}, "___"]&[40, 0.091, 80, 0.126],
    od == 1.050, Piecewise[{{#1, T <= (tol*#2)}, {#3, (tol*#2) < T <= (tol*#4)}, {#5, (tol*#4) < T <= (tol*#6)}, {#7, (tol*#6) < T <= (tol*#8)}}, "___"]&[40,0.109, 80, 0.147, 160, 0.188, "XXS", 0.294]]]

Is there a different function that will accept strings?

$\endgroup$
  • 1
    $\begingroup$ Hi bangeo, welcome to the Mathematica Stack Exchange. Can you give an example of a cases where you have strings? It's hard to envision what you mean to do with the function. In any case, it sounds like you're looking for If or Switch? $\endgroup$ – Carl Lange Apr 20 '19 at 21:58
  • $\begingroup$ Thanks for the help and example of where I have strings from the larges code is pipeschedule[od_, tol_, T_] := Which[ od == 0.405, Piecewise[{{#1, T <= (tol*#2)}, {#3, (tol*#2) < T <= (tol*#4)}}, ""] &[40, 0.068, 80, 0.095], od == 1.050, Piecewise[{{#1, T <= (tol*#2)}, {#3, (tol*#2) < T <= (tol*#4)}, {#5, (tol*#4) < T <= (tol*#6)}, {#7, (tol*#6) < T <= (tol*#8)}}, ""] &[40, 0.109, 80, 0.147, 160, 0.188, "XXS", 0.294]]. In the final case "XXS" is the string. Thanks again for your help. $\endgroup$ – bangeo Apr 20 '19 at 23:13
  • $\begingroup$ Please edit your question and add that information to it inside ``` characters so that it's formatted correctly (see formatting help near the edit box) - comments aren't useful for this type of thing! $\endgroup$ – Carl Lange Apr 20 '19 at 23:24
  • $\begingroup$ I think I'm still not understanding what it is that you're trying to do. If I input pipeschedule[1.050, 0.1, 0.02], I get XXS as the result from your code. Is that not what you want to get? If you want to avoid using Piecewise, it still looks to me like Which should work just fine. $\endgroup$ – MassDefect Apr 20 '19 at 23:27
  • $\begingroup$ Thanks for the response. Interesting that I do not get the same result. I will revise the question to show the result I receive. For pipeschedule[1.050, 1, .2], I get $\endgroup$ – bangeo Apr 23 '19 at 2:39
1
$\begingroup$

I usually use Which for these sorts of things, but it might depend on exactly what your needs are.

f[w_, x_, y_, z_] := Which[
   tmin <= x tol,
   w,
   x tol < tmin <= z tol,
   y]
f[40, 0.1, 80, 0.3]

You can also set it up as a pure function like you have in your question:

Which[
    tmin <= #2 tol,
    #1,
    #2 tol < tmin <= #4 tol,
    #3]&[40, 0.1, 80, 0.3]
$\endgroup$
  • $\begingroup$ Thanks so much for the feedback. I see I need to provide additional detail. $\endgroup$ – bangeo Apr 20 '19 at 23:02
  • $\begingroup$ Please see above comment for additional information. Thanks again $\endgroup$ – bangeo Apr 20 '19 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.