Electrical Engineering Department قسم الهندسة الكهربائية
Course Programming Applications for Engineers (ECE 201)
Name
Lab 9 Class
sheet no.
Common MATLAB commands for plotting and figure control
with brief descriptions:
• plot (x, y): Creates a 2D line plot of y vs. x.
• hold on: Retains the current plot, allowing multiple plots to be overlaid on the same
figure.
• hold off: Stops retaining the current plot, so the next plot command will clear the
figure.
• title('Title'): Adds a title to the plot.
• xlabel('label'): Labels the x-axis.
• ylabel('label'): Labels the y-axis.
• legend('label1', 'label2'): Adds a legend with specified labels for each plot.
• grid on / grid off: Turns the grid on or off in the plot for easier reading.
• xlim([xmin xmax]): Sets the limits of the x-axis to the specified range.
• ylim([ymin ymax]): Sets the limits of the y-axis to the specified range.
• figure: Opens a new figure window, allowing multiple figures to be created and
plotted independently.
• linspace(start, end, numberOfPoints): generates linearly spaced values
between two specified endpoints.
Style:
Line Styles Markers Colors
• '-' : Solid line • 'o' : Circle • 'r' : Red
• '--' : Dashed line • '+' : Plus sign • 'g' : Green
• ':' : Dotted line • '*' : Asterisk • 'b' : Blue
• '-.' : Dash-dotted line • '.' : Point • 'c' : Cyan
• 'x' : Cross • 'm' : Magenta
• 's' : Square • 'y' : Yellow
• '^' : Upward-pointing • 'k' : Black
triangle • 'w' : White
• 'v' : Downward-
pointing triangle
• '>' : Right-pointing
triangle
• '<' : Left-pointing
triangle
Examples of Usage
Combine these elements to customize the plot style:
• plot(x, y, 'r--') : Red dashed line
Electrical Engineering Department قسم الهندسة الكهربائية
1.Plot the function y=x+2x2+3x3+4x4+5x5+6x6, between -5≤x≤5 with 100
samples. (Plot should include Title, xlabel, and ylabel).
Code
Electrical Engineering Department قسم الهندسة الكهربائية
Output
2. Plot the function y=3x2 sin(x), between 0≤x≤2π with 50 samples. (Plot should
include Title, xlabel, and ylabel).
Code
Electrical Engineering Department قسم الهندسة الكهربائية
Output
Electrical Engineering Department قسم الهندسة الكهربائية
3. Plot the following functions between 0≤x≤6π with increments of π/1000.
(Plot should include Title, xlabel, ylabel, and legends):
• y=0.5sin(x), in (in a red dotted line).
• y=sin(x), in (in a green dash-dotted line).
• y=5sin(x), in (in black dashed line).
Code
Output
Electrical Engineering Department قسم الهندسة الكهربائية