I have a simple example in the following code:

Clear[f, f1, f2, x, y, c, conditions, substitution]
f1 := x
f2 := y
f := f1 + f2 + c
conditions := f1 > 0 && f2 > 0 && f > 5
substitution := {c -> 0}
Plot3D[ConditionalExpression[f /. substitution, 
  conditions /. substitution], {x, -10, 10}, {y, -10, 10}]

Output for the code:

enter image description here

Using ConditionalExpression[] and substitution for the constant, I can Plot3D[] the function and the plot is realised where the condition is true.

My goal is not to hide the plot where the condition is false, instead of this, I would like to use another value for the false function. For example, if f1>0 is false: f1=0, if f2>0 is false: f2=0, if f>5 is false: f=5.

I have tried to do something like this, but it doesn't seem to work properly:

conditionSubst := {f1 -> 0, f2 -> 0, c -> 0}
Plot3D[ConditionalExpression[f /. conditionSubst, 
  Not[conditions] /. substitution], {x, -10, 10}, {y, -10, 10}]

enter image description here

If it worked fine, the surface would be at a constant z level, because I substituted f1 and f2 with a constant, thus f isn't a function of x and y anymore (or possibly I don't think about ./ properly).

When there's f->5 in conditionSubst too, it works fine, the surface has the constant z value 5.

But this is still not what I want, because for example when only the f1 condition is false, I would like to substitute only the f1 with 0, f2 should remain the same, function of y in this case, and f is computed with the definition. And so on for the other variables, f2 and f.

Hope you can help with a simple solution. :)


1 Answer 1


Perhaps this is a sufficiently simple solution

Plot3D[Max[Max[x, 0] + Max[y, 0], 5], {x, -10, 10}, {y, -10, 10}]

enter image description here

  • $\begingroup$ Thanks, it's very useful for me. $\endgroup$
    – Maci0503
    Aug 1, 2018 at 18:05
  • $\begingroup$ This way, I can add a threshold for a function. However, there could be more complicated replaces (for example: if f1>0 is false: f1=-1) which may not be solved by this method, or improvements needed. But for my case, it is completely enough. :) $\endgroup$
    – Maci0503
    Aug 1, 2018 at 18:19
  • $\begingroup$ It is hard to word a question in a way that gets exactly the answer that is desired. People often post a question, get an answer and then post: A different problem needs a different answer. $\endgroup$
    – Bill
    Aug 1, 2018 at 18:58

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.