0

I have a Random Variable X that has the following probability density function as follows:

X/25, 0 <= x <= 5

2/5-x/25, 5 <= x <= 10

0, otherwise

I am trying to input this into matlab but I can't seem to find documentation on how to do this. Any ideas?

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
OuOyeah
  • 73
  • 1
  • 5
  • 2
    What do you want to use it for? Do you want to plot it, or draw random numbers using the distribution or something else? Also, is the 0.5 in the second line of the equation a typo? – David Apr 14 '14 at 04:06

2 Answers2

1

Recognizing the probability density function (PDF) is from a Triangular distribution, another approach is to use the makedist() and pdf() functions found in MATLAB's Statistics toolbox which uses Probability Distribution Objects.

Sample code below.

% MATLAB R2022a
pd = makedist('Triangular',0,5,10);     % Define probability distribution object
X = 0:.1:12;
plot(X,pdf(pd,X),'r-','LineWidth',1.6)  % Use pdf() function 

PDF for Triangular Distribution with lowerbound 0, mode 5, and upperbound 10.

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
0

You can produce the probability density function you described like so:

function [y] = f( x )

if (x>=0 && x<=5)
    y = x/25;
elseif (x>=5 && x<=10)
    y =2/5-x/25;
else
    y=0;
end

end

If you would like to plot the function using a vector x, you can use:

function [y] = f( x )

n = numel(x);
y = zeros(1,n);

for k = 1:n
if (x(k)>=0 && x(k)<=5)
    y(k) = x(k)/25;
elseif (x(k)>=5 && x(k)<=10)
    y(k) =2/5-x(k)/25;
else
    y(k)=0;
end

end

You can actually just use this second function definition for single values of x and x as a vector of values. The vector x with the function above, like so:

x = 0:0.1:15;
y = f(x)

produces the figure:

image

Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
RDizzl3
  • 846
  • 8
  • 18