Borrar filtros
Borrar filtros

Trying to graph a cumulative distributive function with erf function, lost.

1 visualización (últimos 30 días)
%Sn = X1 + X2 ... + Xn
%Xi's are independent random variables
%uniform on the interval [-a,a]
a1 = 1;
a2 = 5;
a3 = 10;
%σ^2 = o^2 and is the variance of Xi
%μ = u which is expectation
n = 4;
x = -a1:0.1:a1;
for i = -a1:a1
o = (i - mean(x)) / (n-1);
i = i + 1;
y = (1/2)*(1+erf((x-n*mean(x))/sqrt(n*o^2)));
grid on
title('CDF of normal distribution with \mu = 0 and \sigma = 1')
Trying to make a graph that looks something like the one below, although I am lost. Solving for the expectation and variance is difficult since it is supposed to be graphed and it isn't a simple math problem. I tried using the mean function and using a for loop to calculate variance with each x value. However I keep running into roadblocks.

Respuestas (1)

Sudarsanan A K
Sudarsanan A K el 10 de Nov. de 2023
Hello Kylenino,
I understand that you are looking for plotting the Cumulative Distribution Function (CDF) of a random variable , that is formed by taking the sum of n Independent and Identically Distributed (IID) random variables which are uniformly distributed in the interval .
I also note that you want to consider different values of n (say n = [4, 20, 50]), and plot the resulting CDF in the same figure.
Here is an example how you could achieve this:
% Parameters
mu = 0;
sigma = 1;
a = 2;
% Values of n
n_values = [4, 20, 50];
% Generate x values for plotting
x = linspace(-a, a, 5000);
% Plotting
figure('Position', [100, 100, 1000, 400]); % Adjust the figure size here
hold on;
% Define line styles, colors, and marker styles
line_styles = {'--', ':', '-'};
colors = {'b', 'r', 'g'};
%marker_styles = {'o', 's', '*'};
for idx = 1:numel(n_values)
n = n_values(idx);
% Generate n uniform random variables
X = 2 * a * rand(n, 1) - a;
% Calculate the sum of the uniform random variables
S_n = cumsum(X);
% Calculate the mean of the uniform random variables
mean_X = mean(X);
% Calculate the random variable Z_n
Z_n = (S_n - n * mean_X) / (sigma * sqrt(n));
% Calculate the CDF for Z_n
cdf = zeros(size(x));
for i = 1:length(x)
cdf(i) = sum(Z_n <= x(i)) / length(Z_n);
% Plot the CDF with customized line styles, colors, and marker styles
plot(x, cdf, line_styles{idx}, 'Color', colors{idx}, 'LineWidth', 1.5);
% Add labels and title
xlabel('$x$', 'Interpreter', 'latex');
ylabel('$F_{Z_n}(z)$', 'Interpreter', 'latex');
title('CDF of $Z_n$', 'Interpreter', 'latex');
% Add a legend
legend('n = 4', 'n = 20', 'n = 50');
% Adjust the axis limits
xlim([-3, 3]); % Modify the x-axis limits here
ylim([0, 1]);
% Set grid lines
grid on;
% Set background color
set(gca, 'Color', [0.95, 0.95, 0.95]);
% Set font size
set(gca, 'FontSize', 12);
% Set line width of legend
set(findobj(gca, 'Type', 'Line'), 'LineWidth', 2);
% Hold off to end plotting
hold off;
Note that you can use the "erf()" function to get a continuous CDF plot (removing the staircase nature) by replacing the code in the calculation of CDF of "Z_n" with the following command:
% Calculate the CDF for Z_n
cdf = 0.5 * (1 + erf((x - mean(Z_n)) / std(Z_n)));
To know the "erf()" function better, you can additionally refer to the MathWorks documentation in the following link:
I hope this helps!


Más información sobre Graph and Network Algorithms en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by