3
$\begingroup$

I would like the solutions of FindRoot (using NIntegrate under it) to be drawn in different colours by Plot, like in the last plot but without defining a separate function for each element in the solution list of FindRoot:

Clear[w, x, y, z, plt1, plt2, plt3, w1, w2]
w[x_?NumericQ] := {y, z} /. 
   FindRoot[{x*Exp[y] - y, 
     x*(Exp[z] - z)}, {{y, 0.45, 0, 1}, {z, 0.52, 0, 1}}];
plt1 = Plot[w[x], {x, 0, 1}, Evaluated -> True];
plt2 = Plot[Evaluate[w[x]], {x, 0, 1}];
w1[x_?NumericQ] := w[x][[1]]
w2[x_?NumericQ] := w[x][[2]]
plt3 = Plot[{w1[x], w2[x]}, {x, 0, 1}];
GraphicsRow[{plt1, plt2, plt3}]

Is there some simple plot option that does this? Evaluate as in this question does not seem to work for me.

Mathematica 11.3 on Ubuntu 18.04.

$\endgroup$
4
  • $\begingroup$ Have you seen this ? $\endgroup$ Dec 22, 2020 at 8:04
  • $\begingroup$ The second equation is x*(Exp[z] - z)==0 ? it seems that we can solve it directly. $\endgroup$
    – cvgmt
    Dec 22, 2020 at 8:42
  • $\begingroup$ @b.gates thank you - Plot[{f[a][[1]], f[a][[2]]}, {a,0,1}] is simple enough. @cvgmt yes, but the point is to illustrate plotting FindRoot results or some other vector valued function. $\endgroup$ Dec 22, 2020 at 9:17
  • $\begingroup$ 1 up for Evaluated $\endgroup$ Dec 22, 2020 at 13:04

2 Answers 2

1
$\begingroup$
Clear["Global`*"]

w[x_?NumericQ] := {y, z} /. 
   FindRoot[{x*Exp[y] - y, 
     x*(Exp[z] - z)}, {{y, 0.45, 0, 1}, {z, 0.52, 0, 1}}];

Evaluating w[x] with a symbolic input doesn't generate a List because w[x] is only defined for a numeric input.

Evaluate[w[x]]

(* w[x] *)

Since neither Evaluate nor the option Evaluated -> True has any effect on a symbolic w[x], there is only one PlotStyle

You don't need to define separate functions, but you do need to explicitly list both parts of w for when it has numeric values.

Plot[{w[x][[1]], w[x][[2]]}, {x, 0, 1}, 
  PlotLegends -> Placed[{"y", "z"}, {.2, .8}]] // Quiet

enter image description here

$\endgroup$
1
$\begingroup$

There is an easy way to do this by making use of the function SetOptions. It allows one to modify the default options for any function. For the function Plot, its color is decided with the option PlotStyle. You can change this option to any other value globally using SetOptions.

SetOptions[Plot, PlotStyle :> RandomColor[]];

Note that I have used RuleDelayed instead of Rule. This is the key solution to your problem. When RuleDelayed is used, RandomColor[] is evaluated every time Plot is called, and thus the generated plot is of a different color. However, there are some rare cases when the subsequent output of RandomColor[] might be of a similar shade but I suppose you can figure out how to get around that.

So, after evaluating the above code, here is the output to your code:

Output of Plot with different colors

$\endgroup$
1
  • $\begingroup$ But the two curves are still the same colour in each of the 3 plots. $\endgroup$ Dec 25, 2020 at 7:33

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