Plotting Gaussian Curves with Matlab

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 4K views
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:
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.