3-d plot generated from simulations
Show older comments
Hi all,
Could someone please tell me how to generate a graph like the one in the figure below?

My idea is that I have a variable X that I simulate 1000 times with a normal distribution to which I want to vary the mean (from 75 to 125) and the standard deviation (from 1 to 50). From the different values of X obtained, I would like to calculate the Y variable. My idea is to make a graph where the x-axis is the different mean values, the y-axis is the different standard deviation values from X, and the Z-axis is the expected value (Z) obtained for the Y function.
n=1000; %number of sample
X = normrnd(100,40,n,1); %Demand Simulation,here I want to change the mean and the std. deviation
Y=0.3*((X/10).^(2));
Z=mean(Y);
Thank you very much for your help!
21 Comments
Scott MacKenzie
on 2 Jun 2021
I see a couple of issues in your question. First, it seems Z is only a functon of X. Second, X, in your code, is 1000 values from a normal distribution, but, X, in your question varies from 75 to 125. Can you clarify?
Angelavtc
on 2 Jun 2021
Scott MacKenzie
on 2 Jun 2021
I'm still scratching my head over your question. BTW, your code reduces to...
n=1000; %number of sample
X = normrnd(100,40,n,1); %Demand Simulation,here I want to change the mean and the std. deviation
Z=mean(0.3*((X/10).^(2)));
I don't see the connection between this code (with two variables) and the 3D plot you are trying to create (with three variables).
The plot you are trying to create shows z as function of x (which varies from 75 to 125 -- evidently a mean) and y (which varies from 1 to 50 -- evidently a standard deviation). It seems z is a function of two values, one a mean and the other a standard deviation. If that's the case, why are you using normrnd and generate 1000 sample points from the distribution. It's only the mean and standard deviation that you need, and these are known.
Angelavtc
on 2 Jun 2021
Scott MacKenzie
on 2 Jun 2021
Let's try this. In your example plot, one of the x-axis values in 99 and one of the y-axis values is 28. It's hard to tell, but z looks to be about -1.5 or so. Can you give the formula for z in terms of x and y?
Angelavtc
on 2 Jun 2021
Scott MacKenzie
on 2 Jun 2021
OK, we're getting there. I think you forgot n=1000; at the beginning of your code. If you add that and the following lines at the end...
[X, Y] = meshgrid(1:iMax, 1:jMax);
surf(X, Y, myArray');
xlabel('X'); ylabel('Y'); zlabel('Z');
here's the resulting plot:

This gets you closer to your goal. The Z values are no where near those in your example plot and the surface is quite rough. The roughness is perhaps an expected side effect of using normrnd.
Angelavtc
on 2 Jun 2021
Angelavtc
on 2 Jun 2021
Scott MacKenzie
on 2 Jun 2021
OK, sure. I don't this is the exactly what you were looking for, but hopefully it helps. Good luck.
Angelavtc
on 2 Jun 2021
Scott MacKenzie
on 2 Jun 2021
Edited: Scott MacKenzie
on 2 Jun 2021
Yea, I was fiddling around with that also. Here's what I put together (with the variable names changed to reflect the x, y, and z axes):
n = 1000;
x = 75:125;
y = 1:50;
for i = 1:length(x) % mean
for j = 1:length(y) % sd
Z(i,j) = mean(0.3 * ((normrnd(x(i),y(j),n,1) / 10).^(2)));
end
end
[X, Y] = ndgrid(x, y);
h = surf(X, flip(Y), Z);
xlabel('X'); ylabel('Y'); zlabel('Z');

I still haven't figure out how to reverse the order of the y axis values.
Angelavtc
on 2 Jun 2021
Scott MacKenzie
on 3 Jun 2021
You're welcome. But, please be aware that by flipping Y (or using the transpose of myArray in my original answer), the relationship between the data and the changed variable is also flipped. Really, I was just trying to create a reasonable visual graph.
BTW, to smooth the surface, you can add this after the outer for-loop:
Z = smoothdata(Z);
Angelavtc
on 3 Jun 2021
Scott MacKenzie
on 3 Jun 2021
Hmm, not sure. Are you using a different formula? I think there is something fundamentally wrong in the calculations, as opposed an issue in plotting the results. Even the data in my solution (created from the nested for-loop you provided) don't come close to matching the data in the original gray scale graph. So, if you can't recreate the data, there's no change of recreating the plot.
Angelavtc
on 3 Jun 2021
Scott MacKenzie
on 3 Jun 2021
@Angelavtc I just dug a little deeper and figure this out. There is an axis property that allows you to reverse the direction of the data along an axis. After the surf function, put...
set(gca, 'YDir', 'reverse');
Angelavtc
on 3 Jun 2021
Scott MacKenzie
on 3 Jun 2021
With using flip on a matrix, you need to be aware of whether you are flipping along the rows or column. flip(Y) or flip(Y,1) flips the rows, but flip(Y,2) flips the columns. Try the latter and you'll see a difference.
Angelavtc
on 3 Jun 2021
Accepted Answer
More Answers (0)
Categories
Find more on Polygons in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

