how to solve the following Probability density function in matlab?
Afficher commentaires plus anciens
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
- Plot the PDF in Matlab.
- Calculate and plot the CDF.
- 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
Plus de réponses (1)
Image Analyst
le 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 commentaires
Hydro
le 15 Sep 2014
Hydro
le 16 Sep 2014
Image Analyst
le 16 Sep 2014
Modifié(e) : Image Analyst
le 16 Sep 2014
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.
Hydro
le 16 Sep 2014
Image Analyst
le 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);

Catégories
En savoir plus sur Exploration and Visualization dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!