how can I investigate the effect of uncertain parameters on a bi-variate function by using color map?can I use below code?and also how can I solve the error in code?

1 visualizzazione (ultimi 30 giorni)
%monte carlo simulation
sigma=[2 1;1 3]
mu=[1,2]
%initialization
N=100 %number of trail
counter=0
%run the simulation
for i=1:N
%function %generate random numbers
x=2*randn(1,N);
y=randn(1,N);
z=randn(1,N).*sigma+mu; %repmat(mu,10,1) + randn(10,2) %Z = sqrt(1 - X.^2 - Y.^2); %in z ,y and x should be included
if w == x(x.^2 + y.^2 + z.^2 <= 1) %check if occur
counter=counter+1 %find the number of occurance
plot3(x(i), y(i), z(i), '.r')
else
plot3(x(i), y(i), z(i), '.b')
end
end
%%%%%%%%%%%%%%%%%%the error is:Matrix dimensions must agree.%%%%
histogram(w)
histfit(w)
mu=mean(x)
sigma=std(x)
PDFNormal=normpdf(x,mu,sigma) %return as a vector %x is function of system
plot(w,PDFNormal)
gm = gmdistribution(mu,sigma)
pdf(gm,x)
fsurf(@(x,y)reshape(pdf(gm,[x(:),y(:)]),size(x)),[-10 10])
Probability=counter/N
disp(['The estimated value is ' num2str(Probability)])

Risposte (1)

prabhat kumar sharma
prabhat kumar sharma il 17 Gen 2024
Hi Sogol,
I understand that you want to investigate the effect of uncertain parameters on a bivariate function using a colormap, you would typically visualize the function's output over a grid of parameter values, assigning colours based on the function's output at each grid point.
The code you provided contains several errors and seems to be mixing different concepts.
  1. Matrix dimensions must agree: This error occurs because you're trying to perform element-wise operations on matrices that don't have the same dimensions. In the line “z=randn(1,N).*sigma+mu;, randn(1,N)” generates a 1-by-N matrix, while sigma is a 2-by-2 matrix, and mu is a 1-by-2 vector. They cannot be directly multiplied or added due to mismatched dimensions.
  2. Undefined variable w: The variable w is used without being defined. It seems like you want to use it to count the occurrences of a certain condition, but it's not clear what w is supposed to represent.
  3. Bivariate normal distribution: You're trying to generate random numbers from a bivariate normal distribution, but the method you're using is incorrect. You should use mvnrnd to generate random numbers from a multivariate normal distribution with the given mean vector mu and covariance matrix sigma.
  4. Plotting and Histogram: The plotting and histogram parts of the code are not integrated with the simulation loop correctly.
sigma = [2 1; 1 3];
mu = [1, 2];
% Initialization
N = 1000; % Number of trials
counter = 0;
points = zeros(N, 3); % Store x, y, z points
% Run the simulation
for i = 1:N
% Generate random numbers from a bivariate normal distribution
z = mvnrnd(mu, sigma, 1);
x = z(1);
y = z(2);
% Check if the point is within the unit sphere
if x^2 + y^2 <= 1
counter = counter + 1;
points(i, :) = [x, y, 0]; % z-coordinate is 0 for points inside the unit circle
else
points(i, :) = [x, y, 1]; % z-coordinate is 1 for points outside the unit circle
end
end
% Plotting the points
figure;
scatter(points(:, 1), points(:, 2), 10, points(:, 3), 'filled');
colormap([1 0 0; 0 0 1]); % Red for inside, blue for outside
colorbar;
title('Bivariate Normal Distribution Points');
xlabel('X');
ylabel('Y');
% Probability estimation
Probability = counter / N;
disp(['The estimated value is ', num2str(Probability)]);
This code generates points from a bivariate normal distribution and checks if they fall within the unit circle (not sphere, since it's a 2D problem). The points are plotted using a scatter plot where the color indicates whether they are inside (red) or outside (blue) the unit circle. The colormap is set accordingly.
I hope it helps!

Community Treasure Hunt

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

Start Hunting!

Translated by