Error when trying to plot 3D Joint PDF

4 views (last 30 days)
Kaleem Graham
Kaleem Graham on 11 Dec 2018
Answered: Parag on 27 Jan 2025
I'm suppose to plot the 3D Joint PDF of these two functions and compare it to the 3D Joint PDF of gaussian normal distribution, so I mulitplied them together but when I try to plot them I'm getting the (Z must be a matrix, not a scalar or vector error).
Question:
Question.png
Code:
>> mu = 0;sigma = 1; %mean = 0, variance = 1
>> L = 100000; %length of random vector
>> x1 = rand(L,1); %x1 uniformly distributed U(0,1)
>> x2 = rand(L,1); %x2 uniformly distributed U(0,1)
>> w1 = sqrt(-2*log(x1)).*cos(2*pi*x2); %w1 pdf declared
>> w2 = sqrt(-2*log(x1)).*sin(2*pi*x2); %w2 pdf declared
>> w = w1.*w2; %joint pdf declared
>> X = -10:0.1:10;
>> Y = X;
>> gaussNormal = pdf('Normal',X,mu,sigma); %declare gaussian normal pdf
>> [x,y] = meshgrid[X,Y];
>> mesh(x,y,w);
Error using mesh (line 71)
Z must be a matrix, not a scalar or vector.

Answers (1)

Parag
Parag on 27 Jan 2025
Hi, the problem involves transforming two independent and identically distributed random variables, (X_1) and (X_2), each uniformly distributed over the interval (0,1), into a new set of random variables, (W_1) and (W_2). The tasks are to find the joint probability density function (pdf) of (W_1) and (W_2), determine their marginal pdfs, prove that (W_1) and (W_2) are independent and identically distributed Gaussian variables with a mean of 0 and variance of 1
In the code there are 2 key issues,
1. The 'meshgrid' function is used to create a grid of coordinates for plotting 3D surfaces. In your code, you have:
[X, Y] = meshgrid(x, y);
mesh(X, Y, w);
Here, w should be a 2D matrix that corresponds to the grid defined by X and Y. However, w is currently a 1D vector, which leads to the error: Z must be a matrix, not a scalar or vector. To resolve this, ensure that w is calculated as a 2D matrix that matches the dimensions of X and Y.
2.The line w = w1 .* w2;attempts to define the joint PDF by simply multiplying two variables. However, this does not yield a valid joint PDF. A joint PDF should represent the probability density for each combination of the variables ( W_1 ) and ( W_2 ). You need to derive or estimate the joint distribution, which might involve using theoretical derivations or methods like histograms or kernel density estimation to properly capture the relationship between ( W_1 ) and ( W_2 ).
You can see the documentation for meshgrid function-
% Parameters
mu = 0; sigma = 1; % Mean and variance for Gaussian distribution
L = 100000; % Length of random vector
% Generate random samples
x1 = rand(L, 1); % X1 uniformly distributed U(0,1)
x2 = rand(L, 1); % X2 uniformly distributed U(0,1)
% Transform to Gaussian distributed variables using Box-Muller
w1 = sqrt(-2*log(x1)) .* cos(2*pi*x2);
w2 = sqrt(-2*log(x1)) .* sin(2*pi*x2);
% Define grid for plotting
x = -10:0.1:10; % Grid for W1
y = x; % Grid for W2
[X, Y] = meshgrid(x, y);
% Estimate joint PDF using 2D histogram (alternative: KDE)
joint_hist = histcounts2(w1, w2, x, y, 'Normalization', 'pdf');
Z_hist = reshape(joint_hist, length(y)-1, length(x)-1);
% Theoretical joint PDF of Gaussian (independent N(0,1))
Z_gaussian = (1/(2*pi*sigma^2)) * exp(-(X.^2 + Y.^2) / (2*sigma^2));
% Plot estimated joint PDF
figure;
mesh(x(1:end-1), y(1:end-1), Z_hist);
title('Estimated Joint PDF');
xlabel('W1'); ylabel('W2'); zlabel('PDF');
% Plot theoretical joint PDF
figure;
mesh(X, Y, Z_gaussian);
title('Theoretical Gaussian Joint PDF');
xlabel('W1'); ylabel('W2'); zlabel('PDF');

Community Treasure Hunt

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

Start Hunting!