Mathematica Asked on April 13, 2021
Plot[x2 = x /. FindRoot[Tan[x] - x, {x, x1}]; {x2 + x1, 2 x2 + x1}, {x1, 1, 15},
PlotStyle -> {Red, Blue}]
It is a simple example of performing operation as well as plotting. In my case i want findroot operation to be done inside the plot command. How can i manage plotstyle then?
Clear["Global`*"]
Plot[
Evaluate[
x2 = x /.
FindRoot[Tan[x] - x, {x, x1}];
{2 x2 + x1, x2 + x1}],
{x1, 1, 15},
PlotPoints -> 100,
PlotStyle -> {Red, Blue},
PlotLegends ->
Placed[{"2[ThinSpace]x2[ThinSpace]+[ThinSpace]x1",
"x2[ThinSpace]+[ThinSpace]x1"},
{0.25, 0.75}]] //
Quiet
Using Evaluate
enables plotting; however, numerous error messages and plotting artifacts are produced. The plotting artifacts are due to the equation having multiple solutions. The first several positive solutions are
x /. NSolve[{Tan[x] - x == 0, 1 <= x <= 20}, x]
(* {4.49341, 7.72525, 10.9041, 14.0662, 17.2208} *)
EDIT: In general, trying to put everything inside the Plot
statement may not be the best approach. Using an external function and NSolve
instead of FindRoot
solns = x /. NSolve[{Tan[x] - x == 0, 1 <= x <= 30}, x];
sol[x1_?NumericQ] := Module[{min, max},
{min, max} = {0.95, 1}*Nearest[solns, x1][[1]];
x /.
NSolve[{Tan[x] - x == 0, min <= x <= max}, x][[1]]]
Plot[
Evaluate[
x2 = sol[x1];
{2 x2 + x1, x2 + x1}],
{x1, 1, 15},
PlotPoints -> 100,
PlotRange -> All,
Exclusions -> None,
PlotStyle -> {Red, Blue},
PlotLegends ->
Placed[{"2[ThinSpace]x2[ThinSpace]+[ThinSpace]x1",
"x2[ThinSpace]+[ThinSpace]x1"},
{0.25, 0.75}]] //
Quiet
Correct answer by Bob Hanlon on April 13, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP