Plot a scalar functions range of possible values for a range of different coefficients.

3 views (last 30 days)
Hi,
I am trying to plot a 2D plot of all possible functions, let's say f(x)=1+a*x +b*x^2 + c*x^3, in a f-x axes
such that a,b,c are gaussian distributions and the alpha value of the outpot curve is a function of probability.
Something along the lines of:
But with alpha weighted by the likelihood.
I'm looking for a smart way to do it.
Thx
  1 Comment
Rik
Rik on 21 Sep 2022
I'm not sure there is an actually good way to do this (as it will spawn lots of line objects), but what would be wrong with generating a grid of values for a b and c with ndgrid? You can then loop through all combinations and assign the alpha based on the probability?

Sign in to comment.

Answers (1)

Saarthak Gupta
Saarthak Gupta on 11 Sep 2023
Hi Ira,
I understand you are trying to plot a family of functions, coefficients being normally distributed. The transparency or alpha value of each plot is a function of the PDF (probability density function) values of the coefficients.
Under the assumption that the coefficients are independent normal random variables, the following code should help you achieve the desired effect:
[as,bs,cs]=ndgrid(-1:3,-1:3,-1:3);
x=linspace(-100,100);
adjustment_param=7;
mu=0;
sig=2;
hold on
for i=1:5
for j=1:5
for k=1:5
a=as(i,j,k);
b=bs(i,j,k);
c=cs(i,j,k);
alpha = mvnpdf(a,mu,sig)*mvnpdf(b,mu,sig)*mvnpdf(c,mu,sig);
alpha_adjusted = (adjustment_param+1)*alpha/(adjustment_param*alpha+1);
plot(x,1+a.*x+b.*(x.^2)+c.*(x.^3),'Color',[1 0 0 alpha_adjusted]);
end
end
end
hold off
Which results in the following output:
Here, you can use the ‘ndgrid’ function and 'for' loops to iterate through all possible combinations of the coefficients (‘a’, ‘b’, ‘c’). For each tuple (a,b,c), we plot the corresponding function, and the alpha value is calculated using the ‘mvnpdf’ function. Adjust the alpha values so that extremely small values (of the order of 1e-9) can be made visible. To achieve this, you can use the function (n+1)x/nx+1 which boosts very small values of alpha to a reasonable range. ‘n’ is the adjustment parameter, and ‘x’ is the calculated alpha.
Note: Since ‘a’, ‘b’ and ‘c’ are assumed to be independent, you can calculate their combined probability by simply multiplying their individual PDF values. In case of joint distributions, you can refer to the documentation of the ‘mvnpdf’ function.
Please refer to the following MATLAB documentation for more details:

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!