how to solve the following Probability density function in matlab?

Hello, i am very new to Matlab and got this question in assignment which i need to submit soon. Any help with coding and a bit explanation would be appreciated.
A probability density function (PDF) is given as follows:
1/4 0<=x<3
f(x) =
5/8-(1/8)x 0<=x<=5
  1. Plot the PDF in Matlab.
  2. Calculate and plot the CDF.
  3. Demonstrate analytically that the mean value associated with the PDF is 49/24.
The answer to the question should be the plots of the PDF and the CDF, as well as the analytical calculation of the mean value.

 Réponse acceptée

There is an obvious misprint in the stated interval. It should be:
1/4 0<=x<3
f(x) =
5/8-(1/8)x 3<=x<=5
As to answering (3.), it all depends on your remembering your calculus enough to be able to integrate a constant times x and a constant times x^2. I think they want this one done by hand. Also you need to know what the definition of "mean value" is.

5 commentaires

Thanks for the note Roger,
I actually solved the third part however, i can't do the first two parts as i dont know how to feed this into Matlab. Am very new to matlab.
As to the first two parts. do this (I'm assuming you made the correction I mentioned):
x = linspace(0,5);
t = (x<3);
PDF = t/4+(~t).*(5/8-(1/8)*x); % <-- CORRECTED
CDF = t.*x/4+(~t).*(-9/16+5/8*x-x.^2/16); % <-- Derived analytically
plot(x,PDF,'y-',x,CDF,'r-')
Many thanks Roger. Solved the problem
Roger, your coding works super. here is mine one and i couldn't really figure out where i am wrong with regards to plotting CDF.
*
function [ Y ] = CDF ()
X=linspace(0,5,1000);
Y=[];
for x=X;
if x<3
Y=[Y .25*x];
else
Y=[Y 5/8*x-1/18*x^2];*
end
end plot(X,Y,'-'); xlabel('X'); ylabel('f(x)'); title('CDF Plot') end
Your Y is correct up to x = 3. But at that point you have introduced a discontinuity into it. Notice that just before that point your Y was very nearly equal to 3/4, which is correct. Just after it, the value suddenly jumps up to 21/16 (I have corrected your erroneous 1/18*x^2 to 1/16*x^2.) That is because you haven't computed the integration of the PDF properly. First of all, there is a residual 3/4 that is the cumulative probability built up by the prior interval. That can't be suddenly ignored. You need to include it in your Y values. Next, your integration going past there is in error. What you want is the definite integral of 5/8-1/8*t with respect to t from 3 to x, so that is the difference between the two indefinite integrals at x and at 3: (5/8*x-1/16*x^2)-(5/8*3-1/16*3^2). You forgot to include the "-(5/8*3-1/16*3^2)". This is equal to
-21/16+5/8*x-1/16*x^2
This would of course be zero at x = 3, so we have to add in the 3/4 to make it a truly cumulative distribution. The final result would be
-9/16+5/8*x-1/16*x^2
which is what I gave you. Notice that when x = 5, the value of CDF becomes exactly one, which is what it should be.

Connectez-vous pour commenter.

Plus de réponses (1)

Try this hint:
x = linspace(0, 5, 800); % Divide section from 0 - 5 up into 800 elements.
fx = 0.25 * ones(1, length(x)); % Initialize fx as all 1/4
% Now need to set the latter part equal to that equation.
latterIndexes = x >= 3;
fx(latterIndexes) = your equation using x(latterIndexes) instead of x.
See how far you get with your homework with this hint.

5 commentaires

Hello,
Thanks for the hint however, didnt really understand. Tried to digest though. The (1/4* X shouldn't really go till end which in your case is set to 800. I need to submit this tomorrow and i dont know how to proceed in matlab??:))
my mistake while commenting earlier regarding 800 set of observations.
Of course I know that. It only goes from 0 to 3, which is 3/5 of the way, or the first (3/5) * 800 = 480 elements. Elements 481 - 800 (which are called "latterIndexes") you were supposed to set to your equation 5/8-(1/8)*x so all you had to do was put in that equation and call plot().
x = linspace(0, 5, 800); % Divide section from 0 - 5 up into 800 elements.
fx = 0.25 * ones(1, length(x)); % Initialize fx as all 1/4
% Now need to set the latter part equal to that equation.
latterIndexes = x >= 3;
fx(latterIndexes) = 5/8 - (1/8)*x(latterIndexes);
% Now we're done creating the function.
% All that's left to do is plot it.
plot(x, fx, 'b-', 'LineWidth', 3);
ylim([0,0.3]);
grid on;
xlabel('x', 'FontSize', 25);
ylabel('fx', 'FontSize', 25);
I gave you virtually the whole thing already except simple commands to plot it. All you had to do was plug in your equation in the right hand side of that equation like I told you to.
Many thanks, got it. have understood the entire question+procedure now.
To also plot CDF, use cumsum:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 22;
x = linspace(0, 5, 800); % Divide section from 0 - 5 up into 800 elements.
fx = 0.25 * ones(1, length(x)); % Initialize fx as all 1/4
% Now need to set the latter part equal to that equation.
latterIndexes = x >= 3;
fx(latterIndexes) = 5/8 - (1/8)*x(latterIndexes);
% Now we're done creating the function.
% All that's left to do is plot it.
subplot(1,2,1);
plot(x, fx, 'b-', 'LineWidth', 3);
ylim([0,0.3]);
grid on;
xlabel('x', 'FontSize', 25);
ylabel('fx', 'FontSize', 25);
title('PDF', 'FontSize', fontSize);
% Compute CDF from the PDF, fx.
cdf = cumsum(fx);
% Convert to percentage
cdf = 100 * cdf / cdf(end);
subplot(1,2,2);
plot(x, cdf, 'b-', 'LineWidth', 3);
ylim([0, 100]);
grid on;
xlabel('x', 'FontSize', 25);
ylabel('Percentage', 'FontSize', 25);
title('CDF', 'FontSize', fontSize);

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by