I want to create a simple code to solve an algebraic equation named eq[x]
, and plot x
as a function of three parameters y
, z
, and k
(not with Plot3D
). To do this, I fix y
and z
each time and plot x
as a function of k
: y
will take the values 2,5,20
, while z
will take 0.1,1,10,100,200
, with k
varying between 0
and 50
.
I'd like to achieve all this in a single subroutine without having to systematically use the Clear["Global"]
instruction to reset the values of y z,k,root[k]
.
What's the best numerical scheme for this problem?
Here is the code that I usually use to solve and plot this equation:
eq[x_?NumericQ, k_?NumericQ] = 1 - k^2/x^2 - y^2/(BesselI[3, z] x^2 (1 - 1/x)) (BesselI[2, z]/3 + (k^2)/((BesselI[3, z] + z) 15 (1 - 1/x)^2 x^2) y);
Solution for y=2 and z = 0.1
Clear["Global`*"] (*============== Solution for y=2 ==============*) y = 2; (*== z=0.1 ==*) z = 0.1; root[k_] :=SolveValues[1 - k^2/x^2 - y^2/(BesselI[3, z] x^2 (1 - 1/x)) (BesselI[2, z]/3 + (k^2)/((BesselI[3, z] + z) 15 (1 - 1/x)^2 x^2) y) == 0, x] (*Tables of the 3 roots with k between 0 and 50 *) x1 = Table[Evaluate[{k, root[k][[1]]}], {k, 0.1, 50, 0.1}]; x2 = Table[Evaluate[{k, root[k][[2]]}], {k, 0.1, 50, 0.1}]; x3 = Table[Evaluate[{k, root[k][[3]]}], {k, 0.1, 50, 0.1}]; (*Plots of the 3 roots as a function of k*) ListLinePlot[{x1, x2, x3}, PlotRange -> All, Frame -> True, FrameLabel -> {k, x}, PlotLegends -> {"x1,", "x2", "x3"}]
Solution for y=2 and z = 1
Clear["Global`*"] (*============== Solution for y=2 ==============*) y = 2; (*== z=1 ==*) z = 1; root[k_] :=SolveValues[1 - k^2/x^2 - y^2/(BesselI[3, z] x^2 (1 - 1/x)) (BesselI[2, z]/3 + (k^2)/((BesselI[3, z] + z) 15 (1 - 1/x)^2 x^2) y) == 0, x] (*Tables of the 3 roots with k between 0 and 50 *) x1 = Table[Evaluate[{k, root[k][[1]]}], {k, 0.1, 50, 0.1}]; x2 = Table[Evaluate[{k, root[k][[2]]}], {k, 0.1, 50, 0.1}]; x3 = Table[Evaluate[{k, root[k][[3]]}], {k, 0.1, 50, 0.1}]; (*Plots of the 3 roots as a function of k*) ListLinePlot[{x1, x2, x3}, PlotRange -> All, Frame -> True, FrameLabel -> {k, x}, PlotLegends -> {"x1,", "x2", "x3"}]
Solution for y=2 and z = 10
Clear["Global`*"] (*============== Solution for y=2 ==============*) y = 2; (*== z=10 ==*) z = 10; root[k_] :=SolveValues[1 - k^2/x^2 - y^2/(BesselI[3, z] x^2 (1 - 1/x)) (BesselI[2, z]/3 + (k^2)/((BesselI[3, z] + z) 15 (1 - 1/x)^2 x^2) y) == 0, x] (*Tables of the 3 roots with k between 0 and 50 *) x1 = Table[Evaluate[{k, root[k][[1]]}], {k, 0.1, 50, 0.1}]; x2 = Table[Evaluate[{k, root[k][[2]]}], {k, 0.1, 50, 0.1}]; x3 = Table[Evaluate[{k, root[k][[3]]}], {k, 0.1, 50, 0.1}]; (*Plots of the 3 roots as a function of k*) ListLinePlot[{x1, x2, x3}, PlotRange -> All, Frame -> True, FrameLabel -> {k, x}, PlotLegends -> {"x1,", "x2", "x3"}]
Solution for y=5 and z = 0.1
Clear["Global`*"] (*============== Solution for y=5 ==============*) y = 5; (*== z=0.1 ==*) z = 0.1; root[k_] :=SolveValues[1 - k^2/x^2 - y^2/(BesselI[3, z] x^2 (1 - 1/x)) (BesselI[2, z]/3 + (k^2)/((BesselI[3, z] + z) 15 (1 - 1/x)^2 x^2) y) == 0, x] (*Tables of the 3 roots with k between 0 and 50 *) x1 = Table[Evaluate[{k, root[k][[1]]}], {k, 0.1, 50, 0.1}]; x2 = Table[Evaluate[{k, root[k][[2]]}], {k, 0.1, 50, 0.1}]; x3 = Table[Evaluate[{k, root[k][[3]]}], {k, 0.1, 50, 0.1}]; (*Plots of the 3 roots as a function of k*) ListLinePlot[{x1, x2, x3}, PlotRange -> All, Frame -> True, FrameLabel -> {k, x}, PlotLegends -> {"x1,", "x2", "x3"}]
Solution for y=5 and z = 1
Clear["Global`*"] (*============== Solution for y=5 ==============*) y = 5; (*== z=1 ==*) z =1; root[k_] :=SolveValues[1 - k^2/x^2 - y^2/(BesselI[3, z] x^2 (1 - 1/x)) (BesselI[2, z]/3 + (k^2)/((BesselI[3, z] + z) 15 (1 - 1/x)^2 x^2) y) == 0, x] (*Tables of the 3 roots with k between 0 and 50 *) x1 = Table[Evaluate[{k, root[k][[1]]}], {k, 0.1, 50, 0.1}]; x2 = Table[Evaluate[{k, root[k][[2]]}], {k, 0.1, 50, 0.1}]; x3 = Table[Evaluate[{k, root[k][[3]]}], {k, 0.1, 50, 0.1}]; (*Plots of the 3 roots as a function of k*) ListLinePlot[{x1, x2, x3}, PlotRange -> All, Frame -> True, FrameLabel -> {k, x}, PlotLegends -> {"x1,", "x2", "x3"}]
.... and so on.