2
$\begingroup$

LogLinearPlot is capable of plotting multiple functions in the same graph.

ex. LogLinearPlot[{graph1[w], graph2[w]}, {w, 1, 100}]

Is there a way to select automatically whether both graph1 and graph2 are executed? The reason is that in my code, I have certain conditions under which graph1 and/or graph2 would actually exist. In the case where one of the graphs does not exist, I get problems with the plotting.

Thanks in advance!

$\endgroup$
  • $\begingroup$ what version you are using? in V10 this will works fine: g1[w_] := Erf[w]; LogLinearPlot[{g1[w], g2[w]}, {w, 1, 100}] $\endgroup$ – Algohi Dec 6 '14 at 0:48
4
$\begingroup$

Just make sure that in the invalid region the function doesn't evaluate to anything numerical. For example,

Plot[{If[x < 0.5, Sin[x]], If[x > -0.5, x^2]}, {x, -2, 2}]

Mathematica graphics

$\endgroup$
0
$\begingroup$

You can check whether a plot is empty or not with

FullForm[expr]

ploA = Plot[{Sin[x]}, {x, -1, 1}];    
ploB = Plot[{Cos[]}, {x, -1, 1}];    
FullForm[ploB]

enter image description here

ploB[[1]] == {}
(* True *)

With this Strategy you can check if your plots are empty:

ploA = Plot[{Sin[x]}, {x, -1, 1}][[1]] == {}
(* False *)

ploB = Plot[{Cos[]}, {x, -1, 1}][[1]] == {}
(* True *)

As shown by @Szabolcs You can use the If statement and control plots according to your needs:

f1[x_] := Sin[x]
f2[x_] := Cos[x]
Plot[{If[ploA == False, f1[x]], If[ploB == False, f2[x]]}, {x, -2, 2}]

enter image description here

ploB = Plot[{Cos[x]}, {x, -1, 1}][[1]] == {}
(* False *)

Plot[{If[ploA == False, f1[x]], If[ploB == False, f2[x]]}, {x, -2, 2}]

enter image description here

$\endgroup$

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.