Plotting Gaussian Curves with Matlab

Click For Summary
SUMMARY

This discussion focuses on plotting Gaussian curves using MATLAB, specifically through the function gauss(c,n) defined in the script plot_gaussian.m. The user aims to plot 10 Gaussian curves for values of n within the interval [-3,3]. Key insights include the necessity of defining the variable y before plotting and the use of color specifications to differentiate the curves. The discussion also highlights the option to automatically assign colors to each Gaussian curve by plotting them collectively.

PREREQUISITES
  • Familiarity with MATLAB programming and syntax
  • Understanding of Gaussian functions and their mathematical representation
  • Knowledge of MATLAB's plotting functions, particularly the plot function
  • Awareness of line specifications (linespecs) in MATLAB for customizing plot appearances
NEXT STEPS
  • Research MATLAB's plot function and its parameters for advanced plotting techniques
  • Explore the concept of Gaussian distributions and their applications in data analysis
  • Learn about MATLAB cell arrays for managing multiple color specifications in plots
  • Investigate how to automate plot customization in MATLAB for enhanced visualizations
USEFUL FOR

This discussion is beneficial for MATLAB users, data analysts, and researchers interested in visualizing Gaussian distributions and enhancing their plotting skills in MATLAB.

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 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K