1
$\begingroup$

I have the following function. Now I want to add condition to not plot when the function is
ComplexInfinity, Indeterminate, or always negative for all value of d.
So the code shouldn't plot cases with {a, b} = {1,2}, {a, b} = {1,1}, {a, b} = {1,3}.
How can I add the condition?
I know you may come up with some simple method with a different code but I'd like to use this structure so I can apply it to my other problem by MassDefect's answer here.

f[a_, b_, d_] := (a + b)/(a - b) d;
tup1 = Tuples@{{1, 0}, {2, 1, 0, 3}}
Quiet@Plot[{f[##, d], 1}, {d, 0, 1}, 
    PlotLabel -> Style[StringForm["a= `` b= ``", ##]]] & @@@ 
 tup1[[1 ;; 4]]

EDIT: the function shouldn't plot if it's smaller than or equal to 0 for all value in (0,1)

$\endgroup$
6
  • $\begingroup$ for Quiet@(f[##, 0] & @@@ tup1) you have {0, Indeterminate, 0, 0, 0, 0, Indeterminate, 0}, so it is effectively never "always negative" if d ==0. Am I missing something? $\endgroup$
    – garej
    Dec 26 '20 at 8:59
  • $\begingroup$ and Quiet@(f[##, 1] & @@@ tup1) returns {-3, ComplexInfinity, 1, -2, -1, -1, Indeterminate, -1}. Does it mean that you expect just one plot to be shown from sample tuple? $\endgroup$
    – garej
    Dec 26 '20 at 9:04
  • $\begingroup$ d is a range not a specific value $\endgroup$
    – anhnha
    Dec 28 '20 at 10:26
  • $\begingroup$ I understand that and that is the essence of my question - your range will never return "always negative" but you ask to rule it out. So we have different results in different points. Can you say exactly what the tuple in your example should return? $\endgroup$
    – garej
    Dec 28 '20 at 10:50
  • $\begingroup$ I think I made too strict condition. The function shouldn't plot if it's smaller than or equal to zero for the whole range of d. Is that clear? $\endgroup$
    – anhnha
    Dec 28 '20 at 10:58
2
+100
$\begingroup$

Notice that NumberQ rules out ComplexInfinity and NumericQ rules out Indeterminate.


Clear[twoStepCheckAndPlot1];
twoStepCheckAndPlot1[tup_] := 
 Block[{condition1 = {NumberQ, NumericQ}, condition2 = NonNegative, 
   tup$n, tup$d, d, check},
  
  (* make function that rules out errors *)
  check = AllTrue[Through[condition1[#]], TrueQ] &;

  (* save in temporary variable tup$n only those pair from tuple that raise no errors*)
  tup$n = Pick[tup, Boole@Quiet@(check /@ (f[##, 0] & @@@ tup)), 1];

  (* save in temporary variable tup$d only those pairs from tup$n that makes f => 0 *)
  tup$d = 
   Pick[tup$n,Boole@Resolve[ForAll[d, 0 <= d <= 1, condition2@f[##, d]], 
        Reals] & @@@ tup$n, 1];
  
  (* plot only pairs that saved in tup$d and satisfy all conditions *)
  Plot[{f[##, d], 1}, {d, 0, 1}, 
     PlotLabel -> Style[StringForm["a= `` b= ``", ##]]] & @@@ tup$d
] 

Update with another example and a bit alternative procedure.

g[a_, b_, d_] := 1/(a + b + d);
tup1 = Tuples@{{1, 0}, {2, 1, 0, 3}}

The code should rule out {0, 0} because it is ComplexInfinity while d == 0.

Clear[twoStepCheckAndPlot];
twoStepCheckAndPlot[tup_] := 
 Block[{condition2 = NonNegative, tup$d, d, someN = -100},
  
  h[a_, b_, d_] := 
   Quiet @ g[a, b, d] /. {ComplexInfinity -> someN, Indeterminate -> someN};
  
  tup$d = 
   Pick[tup, 
    Boole@Resolve[ForAll[d, 0 <= d <= 1, condition2 @ h[##, d]], 
        Reals] & @@@ tup, 1];
  
  Plot[{h[##, d], 1}, {d, 0, 1}, 
     PlotLabel -> Style[StringForm["a= `` b= ``", ##]]] & @@@ tup$d]

Hope that helps.

$\endgroup$
5
  • 1
    $\begingroup$ Comments are not for extended discussion; this conversation has been moved to chat. $\endgroup$
    – Kuba
    Dec 29 '20 at 8:18
  • $\begingroup$ just a side question: why did you name variables like tup$d, tup$n? Couldn't find any information about this. $\endgroup$
    – anhnha
    Jan 8 at 6:42
  • 1
    $\begingroup$ @anhnha, no special purpose, no meaning in MMA, these are just my way of var naming. I'd used something like tup_n or tup_d elsewhere, but MMA precludes that. This is just to remind yourself, that those variables originated from tup and n is for 'numeric', d that it is connected with d parameter in the function. $\endgroup$
    – garej
    Jan 8 at 7:15
  • $\begingroup$ Would it be OK to use something like tup$1, tup$2? Does it differ from $tup1, $tup2? $\endgroup$
    – anhnha
    Jan 8 at 7:31
  • 1
    $\begingroup$ @anhnha, you can do both, but first option is not recommended and second is against the convention. You may take a look here. $ in the beginning is a convention for constants. You'd better avoid this sign altogether :)) $\endgroup$
    – garej
    Jan 8 at 7:37
1
$\begingroup$

Try e.g.:

f[a_, b_, d_] := (a + b)/(a - b) d; tup1 = 
 Tuples@{{1, 0}, {2, 1, 0, 3}}; 
Quiet@Check[
    t = Plot[{f[##, d], 1}, {d, 0, 1}, 
      PlotLabel -> Style[StringForm["a= `` b= ``", ##]]]; t, 
    Nothing[]] & @@@ tup1[[1 ;; 4]]

enter image description here

$\endgroup$
3
  • $\begingroup$ Can you explain it a bit more? Also it still plots the function when it's negative for all d like cases {a, b} = {1,2}, {a, b} = {1,3} $\endgroup$
    – anhnha
    Dec 23 '20 at 17:37
  • $\begingroup$ Check will check if there are any messages generated. If not, it returns the plot, if yes, it returns Nothing[] $\endgroup$ Dec 23 '20 at 17:49
  • $\begingroup$ I want to add some more general conditions so that doesn't really solve all cases $\endgroup$
    – anhnha
    Dec 23 '20 at 17:51

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.