0% found this document useful (0 votes)
33 views

Lab 4 MATLAB

Uploaded by

mohammed rasheed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Lab 4 MATLAB

Uploaded by

mohammed rasheed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Islamic University ‫الجامعة اإلسالمية‬

Faculty of Engineering ‫كليه الهندسة‬

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

Section No: 2623

November 8, 2024

ENGR 3032, Experiment #4 Term 443 2023 Page 1 of 19


Objective:
The objective of this Lab session is to create two-dimensional plots, adjust the appearance of
generated plots and to divide the plotted window into subplots using Matlab/ Octave.

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)

✓ Titles, Labels, and Grids


The following commands add a title, x - and y -axis labels and a background grid:
Example :
plot(x,y)
title('Laboratory Experiment 1')
xlabel('Time, sec')
ylabel('Distance, ft')
grid on

✓ Creating Multiple Plots


The commands used to create a simple plot are summarized in Table 4.1

ENGR 3032, Experiment #4 Term 443 2023 Page 2 of 19


Table 4.1: Various plotting commands
Example :
x = 0:pi/100:2*pi;
y1 = cos(x*4);
plot(x,y1)
y2 = sin(x);
hold on; % to be able to plot again on the samefigure
plot(x, y2)

Another way to create a graph with multiple lines is to request both lines in a single plot command.
plot(x, Y1, x, Y2)

✓ Line, Color, and Mark Style


You can change the appearance of your plots by selecting user-defi ned line styles and line colors and by
choosing to show the data points on the graph with user specified mark styles as shown in table 4.2.

Table 4.2: Line properties withing the plot command

ENGR 3032, Experiment #4 Term 443 2023 Page 3 of 19


✓ Axis Scaling and Annotating Plots
MATLAB / OCTAVE / OCTAVEautomatically selects appropriate x-axis and y-axis scaling. Sometimes, it
is useful for the user to be able to control the scaling. Control is accomplished with the axis function, shown
in Table 4.3.

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))

3. Bar Graphs and Pie Charts


Bar graphs, histograms and pie charts are popular forms for reporting data. Some of the commonly used
MATLAB / OCTAVE / OCTAVE functions for creating bar graphs and pie charts are listed in Table 4.4.

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.

ENGR 3032, Experiment #4 Term 443 2023 Page 5 of 19


Task 2
Plot x versus y1 and y2, for y1 = sin(x2) and y2 = cos(2x+3). Let x vary from 0 to π in increments of
0.1π.
a. Add a title and labels to your plot.
b. Make the sin(x2) line dashed and red. Make the cos(2x+3) line blue and dotted.
c. Add a legend to the graph.

ENGR 3032, Experiment #4 Term 443 2023 Page 6 of 19


Task 3
Create plots of the following functions from x = 0 to 15. Let x vary from 0 to 15 in increments of
0.1. Each of your plots should include a title, an x-axis label, a y-axis label, and a grid.
a. y =5 - e3x
b. y = sin(0.4x)
c. y = ax2 + bx + c, where a=4, b=7, and c=9
d. y = √𝑥

ENGR 3032, Experiment #4 Term 443 2023 Page 7 of 19


ENGR 3032, Experiment #4 Term 443 2023 Page 8 of 19
ENGR 3032, Experiment #4 Term 443 2023 Page 9 of 19
ENGR 3032, Experiment #4 Term 443 2023 Page 10 of 19
ENGR 3032, Experiment #4 Term 443 2023 Page 11 of 19
Task 4
Create plots of the following functions from x = 0 to 10. Combine these into one figure with four
sub windows, using the subplot function of MATLAB / OCTAVE
a. y =2lnx
b. y = tan(x)+sin(x)
c. y = ax2 + bx + c, where a=0.5, b=0.2, and c=2.4
d. y = √𝑥

ENGR 3032, Experiment #4 Term 443 2023 Page 12 of 19


ENGR 3032, Experiment #4 Term 443 2023 Page 13 of 19
Task 5

The following scores were recorded for a given course:


G = [58, 93, 71, 60, 85, 72, 67, 35, 66, 95, 52, 81, 96, 68, 86, 58, 82, 65, 93, 83]
Write an m-file to represent the distribution of grades graphically (use comments and identify the
variables in the header of your script file).
a. Use MATLAB command sort ( ) to sort the data and create a bar graph of the
scores.
b. Create a histogram of the scores.
c. Find the minimum, maximum and mean of all scores.

ENGR 3032, Experiment #4 Term 443 2023 Page 14 of 19


ENGR 3032, Experiment #4 Term 443 2023 Page 15 of 19
ENGR 3032, Experiment #4 Term 443 2023 Page 16 of 19
Task 6
Create a vector x of values from 0 to 20𝜋, with a spacing of 𝜋 /100. Define vectors y and z as y= x
sin(x) and z= x cos(x).Use subplots to plot all the graphs.
a. Create an x–y plot of x and y.
b. Create a polar plot of x and y.
c. Create a three-dimensional line plot of x, y and z with title and labels.
d. Use comet 3 and create another graph.

ENGR 3032, Experiment #4 Term 443 2023 Page 17 of 19


ENGR 3032, Experiment #4 Term 443 2023 Page 18 of 19
Conclusion (Explain briefly the use of the subplot command?)

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.

ENGR 3032, Experiment #4 Term 443 2023 Page 19 of 19

You might also like