Lab 4 MATLAB
Lab 4 MATLAB
ENGR 3032
SCIENTIFIC COMPUTING LANGUAGES
Fall 2024 (Term 461)
LAB 4
Plotting and Subplots
[CO_4, PI_1_5, SO_1]
Submitted To
Eng. Osamah Alkhalifah
Submitted By
Mohammed Rasheed
431014033
November 8, 2024
Theory:
1. Two Dimensional Plots
A set of ordered pairs is used to identify points on a two-dimensional graph; straight lines then connect the
points. The values of x and y may be measured or calculated. Generally, the independent variable is given the
name x and is plotted on the x -axis, and the dependent variable is given the name y and is plotted on the y -
axis.
✓ Simple x–y Plots
Once the vectors of x -values and y -values have been defined, MATLAB / OCTAVE makes it easy to
create plots
Example :
>> x = [0:2:18];
>> y = [0, 0.33, 4.13, 6.29, 6.85, 11.19, 13.19,
13.96, 16.33,18.17]
>> plot(x,y)
Another way to create a graph with multiple lines is to request both lines in a single plot command.
plot(x, Y1, x, Y2)
Table 4.3: Axis scaling, legend and adding text to a plot window
2. Sub Plots
The subplot command allows you to subdivide the graphing window into a grid of m rows and n columns.
The function splits the figure into an m x n matrix:
subplot(m, n, p)
The variable p identifies the portion of te window where the next plot will be drawn.
The following commands split the graph window into a top plot and a bottom plot:
Example :
x = 0:pi/20:2*pi
subplot(2, 1, 1)
plot(x, sin(x))
subplot(2, 1, 2)
plot(x, sin(2*x))
Table 4.4
ENGR 3032, Experiment #4 Term 443 2023 Page 4 of 19
Example :
clear, clc
x = [1, 2, 5, 4, 8];
y = [x;1:5];
subplot(2, 2, 1)
bar(x),title('A bar graph of vector x')
subplot(2, 2, 2)
bar(y),title('A bar graph of matrix y')
subplot(2, 2, 3)
bar3(y),title('A three-dimensional bar graph')
subplot(2, 2, 4)
pie(x),title('A pie chart of x')
Histogram Example:
x = [100, 95, 74, 87, 22, 78, 34, 35, 93, 88, 86, 42, 55, 48];
hist(x)
Lab Work
Task 1
Plot x versus y for y = 1 + sin(x/5). Let x vary from 0 to 2.5π in increments of 0.12π. Add title and
labels to your plot.
The `subplot` command in MATLAB arranges multiple plots within a single figure by
dividing it into a grid. This allows you to compare different graphs side by side,
making it easier to analyze and present related data clearly in one view.