Plotting a 3D Gaussian in MATLAB

In summary, you would need to use the plot3 command to plot a three dimensional Gaussian distribution. To do this, you would need to choose appropriate values for the A0,w, and range of coordinates.
  • #1
debwaldy
38
0

Homework Statement


Hi all! I'm trying to solve the following problem but I'm not too sure of a few things.
Write Matlab instructions to plot a three dimensional Gaussian:

A = A0exp((x^2 + y^2 + z^2)/w^2)

choosing appropriate values for A0,w and range of coordinates.


Homework Equations


I'm guessing i have to use the plot3 command but after that I'm lost


The Attempt at a Solution


I don't know how to choose appropriate values for all of the variables.And what is w?
I'm a bit lost so I'd really appreciate any hints or tips to point me in the right direction thanks debs:smile:
 
Physics news on Phys.org
  • #2
debwaldy said:

Homework Statement


Hi all! I'm trying to solve the following problem but I'm not too sure of a few things.
Write Matlab instructions to plot a three dimensional Gaussian:

A = A0exp((x^2 + y^2 + z^2)/w^2)

choosing appropriate values for A0,w and range of coordinates.


Homework Equations


I'm guessing i have to use the plot3 command but after that I'm lost


The Attempt at a Solution


I don't know how to choose appropriate values for all of the variables.And what is w?
I'm a bit lost so I'd really appreciate any hints or tips to point me in the right direction thanks debs:smile:

w is a parameter of your Gaussian, make it bigger and the distribution should flatten out.

What exactly do you want to plot? You can't plot A(x,y,z) on a 3D plot because A depends on 3 variables so you would need a 4D plot.

If you want to plot a Gaussian distribution of 2 variables, run the following code in Matlab:
Code:
clear;
pts = -5:.1:5;
N = length(pts);
X = reshape(repmat(pts,1,N),N,N);
Y = reshape(repmat(pts,N,1),N,N);
Z = exp(-X.^2-Y.^2);
figure;surf(X,Y,Z)
Is that the type of plot you are trying to produce?
 
  • #3


I would suggest breaking down the problem into smaller steps and understanding each step before proceeding. First, let's define the variables:

- A0: This is the amplitude of the Gaussian, which determines the peak height. You can choose any value for this, but a common choice is 1.
- w: This is the width of the Gaussian, also known as the standard deviation. It controls the spread of the Gaussian. Again, you can choose any value for this, but a common choice is 1.
- x, y, z: These are the coordinates in 3D space where you want to evaluate the Gaussian function.

Next, we can use the plot3 command to plot the Gaussian. The syntax for plot3 is `plot3(x,y,z)`, where x, y, and z are vectors of the same length. In this case, we want to plot the Gaussian function at multiple points in 3D space, so we need to create a grid of x, y, and z values. We can use the `meshgrid` command to create this grid:

```
% Define the range of coordinates
x = linspace(-5,5,100); % Choose a range that covers the peak of the Gaussian
y = linspace(-5,5,100);
z = linspace(-5,5,100);

% Create a grid of x, y, and z values
[X,Y,Z] = meshgrid(x,y,z);

% Calculate the Gaussian function at each point in the grid
A = A0*exp((X.^2 + Y.^2 + Z.^2)/w^2);

% Plot the Gaussian function using plot3
plot3(X,Y,Z,A);
```

You can play around with the values of A0 and w to see how they affect the shape and spread of the Gaussian. Additionally, you can use the `surf` command to plot a surface instead of just the points. I hope this helps guide you in the right direction.
 

1. How do I plot a 3D Gaussian in MATLAB?

To plot a 3D Gaussian in MATLAB, you can use the "surf" or "mesh" function. First, define the parameters of the Gaussian function (mean, standard deviation, and amplitude) and generate a grid of x, y, and z values. Then, use the "surf" or "mesh" function to plot the Gaussian on the grid.

2. Can I change the color or style of the 3D Gaussian plot?

Yes, you can change the color and style of the 3D Gaussian plot by using the "surf" or "mesh" function with additional input arguments for color and style. You can also use the "colormap" function to change the color scheme of the plot.

3. How can I add labels and a legend to my 3D Gaussian plot?

To add labels and a legend to your 3D Gaussian plot, use the "xlabel", "ylabel", "zlabel", and "legend" functions. These functions allow you to specify the labels and legend for each axis of the plot.

4. Is it possible to plot multiple 3D Gaussians on the same figure?

Yes, you can plot multiple 3D Gaussians on the same figure by using the "hold on" and "hold off" commands. These commands allow you to plot multiple figures on the same axis without overwriting the previous plot.

5. How can I save my 3D Gaussian plot as an image file?

To save your 3D Gaussian plot as an image file, you can use the "saveas" function. This function allows you to specify the file format (e.g. PNG, JPEG) and file name for your plot. Alternatively, you can use the "print" function to save your plot as an image file.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top