1
$\begingroup$

I am trying to highlight the intersection if one exists. For specific parameter values there is no solution and the manipulate output creates error message and red background.

How can I avoid the messages with a conditional statement - highlight the point or drop the point (potentially a message in the legend that intersection does not exist).

Clear["Global`*"]
f[a_, x_] := a + x^2;
g[b_, x_] := b + x;

intersect[a_, b_] := {x, f[a, x]} /. 
  Solve[{f[a, x] == g[b, x], a >= 0, b >= 0, x >= 0}, x][[1]]

Manipulate[
 Plot[Evaluate@{f[a, x], g[b, x]}, {x, 0, 50}, 
  PlotLegends -> Placed["Expressions", {0.7, 0.7}], 
  PlotRange -> {-25, 500}, 
  Epilog -> {Red, Point@intersect[a, b] }], 
{{a, 50}, 0, 100, 1, Appearance -> "Labeled"}, {{b, 75}, -10, 100, 1, Appearance -> "Labeled"}, 
 Button["Reset", {a = 50, b = 75}, ImageSize -> Medium]]

Thank you!

$\endgroup$
1
$\begingroup$
Clear["Global`*"]
f[a_, x_] := a + x^2;
g[b_, x_] := b + x;

intersect[a_, b_] := Module[
 {sol = Solve[{f[a, x] == g[b, x], a >= 0, b >= 0, x >= 0}, x]}, 
  If[sol === {}, {}, {x, f[a, x]} /. sol[[1]]]]

Manipulate[Plot[Evaluate@{f[a, x], g[b, x]}, {x, 0, 50}, 
  PlotLegends -> {Placed["Expressions", {0.7, 0.7}], 
    Placed[PointLegend[{Red}, 
      If[intersect[a, b] === {}, {"no intersection"}, 
        {N@intersect[a, b]}]], {0.7, 0.5}]},
  PlotRange -> {-25, 500}, 
  Epilog -> {Red, Point@intersect[a, b]}], 
{{a, 50}, 0, 100, 1, Appearance -> "Labeled"}, 
{{b, 75}, -10, 100, 1, Appearance -> "Labeled"}, 
Button["Reset", {a = 50, b = 75}, ImageSize -> Medium]]

enter image description here enter image description here

| improve this answer | |
$\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.