1
$\begingroup$

I have a function f(x) which is dependent on a random parameter a.

I can define a matrix Y containing {x,f(x),P}, where P is the probability that the curve appears. I would like to plot these curves with a gradient on each curve representing its own probability.

The code :

f[x_, a_] := Piecewise[{{a*Sin[x]/x, 2*Pi > x > 0}}];
n = 100;
a = RandomVariate[NormalDistribution[1, 0.1], n];
P = Table[N@Probability[x <= a[[i]], x \[Distributed] a], {i, 1, n}];
Y = Table[Table[{i, f[i, a[[j]]], P[[j]]}, {i, 0, 2*Pi, 0.01}], {j, 1, n}];
ListPlot[Y[[All, All, {1, 2}]],Joined->True]

It may be like

ListPlot[Y[[All, All, {1, 2}]], ColorFunction->...]

or

ListPlot[Y[[All, All, {1, 2}]], PlotStyle->Table[ColorData[...]]]

Thank you in advance

$\endgroup$

1 Answer 1

3
$\begingroup$

Edit: My first approach didn't work as intended, it only took the last opacity value in the table. Here is a version with colors, albeit based on a rather than P:

ClearAll[f, n, a, P, Y];
f[x_, a_] := Piecewise[{{a*Sin[x]/x, 2*Pi > x > 0}}];
n = 100;
a = RandomVariate[NormalDistribution[1, 0.1], n];
P = ParallelTable[
   N@Probability[x <= a[[i]], x \[Distributed] a], {i, 1, n}];
Y = ParallelTable[
   Table[{i, f[i, a[[j]]], P[[j]]}, {i, 0, 2*Pi, 0.01}], {j, 1, n}];
ListPlot[Y[[All, All, {1, 2}]], Joined -> True, 
 PlotStyle -> Hue /@ (Abs[a - 1])]

Returns:

enter image description here

Old approach: I believe different colors would be too confusing for displaying the probability with this many curves. Heres a version with different opacitylevels:

ClearAll[f, n, a, P, Y];
f[x_, a_] := Piecewise[{{a*Sin[x]/x, 2*Pi > x > 0}}];
n = 100;
a = RandomVariate[NormalDistribution[1, 0.1], n];
P = Table[N@Probability[x <= a[[i]], x \[Distributed] a], {i, 1, n}];
Y = Table[
   Table[{i, f[i, a[[j]]], P[[j]]}, {i, 0, 2*Pi, 0.01}], {j, 1, n}];
ListPlot[Y[[All, All, {1, 2}]], Joined -> True, 
 PlotStyle -> Directive[Red, Opacity /@ (P*0.2)]]

Returns:

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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