# Plotting a function by randomly choosing the values of two parameters

Given the function:

 fun[a_, b_, x_] =
Sqrt[1 + x^2 - 2 x Cos[a]] + Sqrt[2 - 2 Cos[b]] + Sqrt[
1 + x^2 + 2 x Cos[a + b]];


I want to Plot it with respect to $$x$$, by choosing randomly the values of $$a$$ and $$b$$ (need not be same) from the interval $$[0,2\pi]$$. I expect a two dimensional plot with "lots of dots", each dot corresponding to a value of the given function corresponding to $$x$$ for some value of parameters $$a$$ and $$b$$.

Clear["Global*"]

fun[a_, b_, x_] = Sqrt[1 + x^2 - 2 x Cos[a]] +
Sqrt[2 - 2 Cos[b]] + Sqrt[1 + x^2 + 2 x Cos[a + b]];

xmax = 2;

{fmin, fmax} = (#[{fun[a, b, x], 0 <= a <= 2 Pi,
0 <= b <= 2 Pi, -xmax <= x <= xmax}, {a, b,
x}] & /@
{NMinValue, NMaxValue})

(* {2., 8.} *)

Manipulate[
Plot[fun[a, b, x], {x, -xmax, xmax},
PlotRange -> {fmin, fmax},
Frame -> True,
FrameLabel -> (Style[#, 14, Bold] & /@ {x, fun})],
{{a, 3}, 0, 2 Pi, 0.01, Appearance -> "Labeled"},
{{b, 3}, 0, 2 Pi, 0.01, Appearance -> "Labeled"}]


Manipulate[
DensityPlot[fun[a2, b2, x],
{x, -xmax, xmax}, {a2, 0, 2 Pi},
FrameLabel -> (Style[#, 14, Bold] & /@ {"x", "a"}),
PlotLegends -> Automatic],
{{b2, 3, "b"}, 0, 2 Pi, 0.01, Appearance -> "Labeled"}]


Manipulate[
DensityPlot[fun[a3, b3, x],
{x, -xmax, xmax}, {b3, 0, 2 Pi},
FrameLabel -> (Style[#, 14, Bold] & /@ {"x", "b"}),
PlotLegends -> Automatic],
{{a3, 3, "a"}, 0, 2 Pi, 0.01, Appearance -> "Labeled"}]


DensityPlot3D[fun[a4, b4, x],
{x, -xmax, xmax}, {a4, 0, 2 Pi}, {b4, 0, 2 Pi},
AxesLabel -> (Style[#, 14, Bold] & /@ {"x", "a", "b"}),
PlotLegends -> Automatic,
PlotPoints -> 50]


If I understand your question right you want to plot {x,fun[a,b,x]}?

Try

fun[a_, b_, x_] :=Sqrt[1 + x^2 - 2 x Cos[a]] + Sqrt[2 - 2 Cos[b]] +Sqrt[1 + x^2 + 2 x Cos[a + b]];

Plot[Evaluate@
Table[fun[RandomReal[{0, 2 Pi}], RandomReal[{0, 2 Pi}], x], {i, 1,50}], {x, 0, 1}, PlotRange -> {0, Automatic} ,AxesLabel -> {"x", "fun[x]"}]
`

• Can one show the same using density plot? I think that would make more sense here.
– Rob
Jul 11, 2020 at 9:33