how to solve the following Probability density function in matlab?

1 Ansicht (letzte 30 Tage)
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.

Akzeptierte Antwort

Roger Stafford
Roger Stafford am 11 Sep. 2014
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 Kommentare
Hydro
Hydro am 16 Sep. 2014
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
Roger Stafford
Roger Stafford am 17 Sep. 2014
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 11 Sep. 2014
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 Kommentare
Hydro
Hydro am 16 Sep. 2014
Many thanks, got it. have understood the entire question+procedure now.
Image Analyst
Image Analyst am 16 Sep. 2014
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);

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by