MATLAB Plotting Gaussian Curves with Matlab

AI Thread Summary
To plot Gaussian curves in Matlab for 10 different values of n, define the function gauss(c,n) and use the formula gauss(x,j)=sqrt(j./pi)*exp(-1*j.*x.^2) within a loop. The correct syntax for plotting is plot(x,gauss) instead of plot(x,y), as y is not defined. To display the curves in different colors, you can use a cell array of color specifications and call plot with the color argument. Alternatively, building the Gaussian curves in the loop and plotting them together will automatically assign different colors to each curve. This method simplifies the process and avoids errors related to color specifications.
Dr_Pill
Messages
41
Reaction score
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
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.
 

Similar threads

Replies
10
Views
3K
Replies
1
Views
2K
Replies
7
Views
2K
Replies
2
Views
2K
Replies
1
Views
3K
Replies
1
Views
4K
Replies
2
Views
1K
Back
Top