Plotting Gaussian Curves with Matlab

In summary, the conversation discussed plotting a Gaussian for 10 different values of n using the function plot_gaussian.m. The code includes a loop to plot the Gaussian curves and can also plot them in different colors by passing a third argument to the plot function. Another option is to build up the Gaussian in the loop and then plot it, which will automatically assign different colors to each curve.
  • #1
Dr_Pill
41
0
Matlab rook here,

Suppose I'd want to plot the Gaussian for 10 different values for n:
Code:
function gauss(c,n) = plot_gaussian.m

x=-1*c:0.1:c;
gauss(x,j)=sqrt(j./pi)*exp(-1*j.*x.^2);
for j=1:n
    plot(x,y)
end
And then I'll type gauss(3,10) to plot 10 curves in the interval [-3,3]

Also, if I'd want to plot in 10 different colors, how can I manage that?

Thanks in advance.
 
Last edited:
Physics news on Phys.org
  • #2
You're plotting x,y but you haven't defined a y. I assume you mean to plot(x,gauss). You also use j before you define it (so I think you want the gauss(x,j)= line to be inside a loop

You can pass a third argument to plot to represent the color and shape of the line. For instance:

plot(x,gauss,'g*') would plot a green star. Look up linespecs MATLAB on google.

since it's a loop, you'll have to define a cell of color specs:
C = {'g*','bd','r^'}

and then call plot like
plot(x,y,C{j})

I've only put three elements in C though, so it would error if j=4

Alternatively, you could just build up gauss in the loop, then plot it:

plot(x,gauss)

and it will automatically give each gaussian its own color.
 

1. How do I plot a Gaussian curve with Matlab?

To plot a Gaussian curve in Matlab, you can use the built-in function plot() and specify the equation of the Gaussian curve using the normpdf() function. For example, if you want to plot a Gaussian curve with mean mu and standard deviation sigma, you can use the following code:

mu = 0;sigma = 1;x = linspace(-5,5,100);y = normpdf(x,mu,sigma);plot(x,y);

2. Can I customize the appearance of the Gaussian curve plot?

Yes, you can customize the appearance of the Gaussian curve plot by specifying additional arguments in the plot() function. For example, you can change the color of the curve by adding the argument 'color' followed by the desired color name or RGB vector. You can also add a legend, change the line style, and add labels to the axes.

3. How do I plot multiple Gaussian curves on the same plot?

You can plot multiple Gaussian curves on the same plot by using the hold on command. This allows you to add multiple curves to the same plot without overwriting the previous one. For example, you can plot two Gaussian curves with different mean values on the same plot using the following code:

mu1 = 0;mu2 = 2;sigma = 1;x = linspace(-5,5,100);y1 = normpdf(x,mu1,sigma);y2 = normpdf(x,mu2,sigma);plot(x,y1);hold on;plot(x,y2);

4. Can I add a Gaussian curve to an existing plot?

Yes, you can add a Gaussian curve to an existing plot by using the hold on command, as mentioned in the previous question. Additionally, you can use the plot() function to add the Gaussian curve to the existing plot, specifying the desired axes as an argument. For example, if you have an existing plot with the handle ax, you can add a Gaussian curve to it using the following code:

plot(ax,x,y);

5. Are there other ways to plot Gaussian curves in Matlab?

Yes, there are other ways to plot Gaussian curves in Matlab. For instance, you can use the gaussmf() function from the Fuzzy Logic Toolbox, which plots a Gaussian membership function. You can also use the surf() function to plot a 3D surface of a Gaussian distribution. Additionally, you can use the histogram() function to plot a histogram of a Gaussian distribution, which can also be used to estimate the probability density function.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
888
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
750
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
971
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
741
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top