112CCE Graphical Programming Lab Manual
112CCE Graphical Programming Lab Manual
LABORATORY MANUAL
112CCE - 1
Graphical Programming Lab
Student Information
Name: ___________________________________________________________________________________
1442-1443
112CCE: Graphical Programming KKU-CS-CE
Grades:
Lab Activity 15 marks
Home Work 15 marks
Mid-1 Exam 10 marks
Mid-2 Exam 10 marks
Final Exam 50 marks
Lab Policy:
Attendance is Mandatory.
Copies in labs will get a grade of ZERO.
Lab reports/activities are due on the same lab session. LATE lab reports will have a 20% penalty, if they are
given within one week of the due date.
CONTENTS
S. No. Chapter Name Page No
1. Starting with MATLAB 8
Graphical Programming
112-CPE-1
Starting with MATLAB
Student Information
Name: ____________________________________________________________________________________
ID Number: ________________ Serial Number: ______________ Group Number: ___________
Prepared by
PAGES TO STUDY:
PAGE:3 TO PAGE:35
PAGES TO STUDY:
PAGE:5 TO PAGE:33
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Introduction
MATLAB is a powerful language for technical computing.
The name MATLAB stands for MATrix LABoratory, because its basic
data element is a matrix (array).
MATLAB can be used for math computations, modeling and simulations,
data analysis and processing, visualization and graphics, and algorithm
development.
MATLAB is widely used in universities and colleges in introductory and
advanced courses in mathematics, science, and especially engineering.
MATLAB: An Introduction with Applications is intended for students who are using MATLAB for the first time and
have little or no experience in computer programming.
History of MATLAB:
Moler, the chairman of the computer science department at the University of New Mexico, started developing MATLAB
in the late 1970s.
In the 1980’s, MATLAB was rewritten in C with more functionality (such as plotting routines)
The Mathworks, Inc. was created in 1984.
The Mathworks is now responsible for development, sale, and support for MATLAB.
R2017a
Strengths of MATLAB
MATLAB is relatively easy to learn.
MATLAB code is optimized to be relatively quick when performing matrix operations.
MATLAB may behave like a calculator or as a programming language.
MATLAB is interpreted, errors are easier to fix.
Weaknesses of MATLAB
MATLAB is NOT a general purpose programming language
MATLAB is an interpreted language (making it for the most part slower than a compiled language
such as C++)
MATLAB is designed for scientific computation and is not suitable for some things (such as parsing
text- to analyze (a string of characters) in order to associate groups of characters with the syntactic units
of the underlying grammar)
Graphical Programming-CPE112-1 Lecture 1
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Workspace
Window
Current Folder Window
Store all
Variables
Command
History
Window
Command Window
Graphical Programming-CPE112-1 Lecture 1
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Workspace
Window
Current Folder Window
Store all
Variables
Command
History
Window
Command Window
Graphical Programming-CPE112-1 Lecture 1
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Command Window: The Command Window is MATLAB 's main window and opens when
MATLAB is started.
Figure Window opens automatically after any command that draws a graph.
Editor Window: Use Editor Window to write and debug MATLAB scripts. Open with edit
command.
Help Window: Get Help Window by clicking on Help icon (question mark) in tool strip
Basic procedure:
1. At prompt (>>), type in MATLAB
command
2. Press ENTER key
3. MATLAB displays result in Command
Window, followed by a prompt
4. Repeat from step 1
Semicolon (;)
When typed at end of command, suppresses output. (Only prompt displayed at next
line)
• Useful for preventing display of large outputs
Percent sign(%)
When typed at beginning of line, MATLAB treats line as a comment and doesn’t
execute line.
clc command
• Clears Command Window display
Up and down arrows still bring back previous commands
In this chapter will only discuss arithmetic with scalars (single numbers)
• Can do arithmetic directly on numbers (like a calculator)
• Can store numbers in variables
Multiplication * 5*3
Right division / 5/3
Left division \ 5\3=3/5
Exponentiation ^ 5 ^ 3 (means 53 = 125)
Precedence order
• Same as most calculators
• Same as doing arithmetic by hand
• For multiple operations of same precedence,
MATLAB goes left to right
• Can change order by using parentheses
Lecture 2
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 2
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 2
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 2
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 2
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 2
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 2
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 2
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 2
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 2
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 2
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 2
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 2
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 2
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 2
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 2
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 2
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 2
112CPE– Graphical Programming– Lab Manual -Experiment #1 KKU-CSC-CE
Activity
1
King Khalid University
College of Computer Science
Department of Computer Engineering
Graphical Programming
112-CPE-1
Student Information
Name: ____________________________________________________________________________________
ID Number: ________________ Serial Number: ______________ Group Number: ___________
Data Submitted: _______________ Marks: …../2mark Instructor Signature: _________
Prepared by
Solution
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………….……………………………………………………………………………………………
Activity No.2:- Calculate Using MATLAB
Solution
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
Solution
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………….……………………………………………………………………………………………
………………………………………………………………………………………………………………
……………………………………………………………………………………………………………….
………………………………………………………………………………………………………
Solution
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………….……………………………………………………………………………………………
………………………………………………………………………………………………………………
……………………………………………………………………………………………………………….
………………………………………………………………………………………………………………
………………………………………………………………………………………………………………
………………………………………………………………………………………………………………
………………………………………………………………………………………………………………
…………………………………………………………………………………………………………….…
………………………………………………………………………………………………………………
……………………………………………………………………………………………………………….
………………………………………………………………………………………………………………
………………………………………………………………………………………………………………
Student Information
Name: ____________________________________________________________________________________
ID Number: ________________ Serial Number: ______________ Group Number: ___________
Prepared by
Chapter2
Creating Arrays
The array is a fundamental form that MATLAB uses to store data.
1D array: Vector
2D array: Matrix 3D array
Graphical Programming Lecture 3
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 3
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 3
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 3
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 3
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 3
King Khalid University
Faculty of Computer Science
Computer Engineering Department
ARRAY ADDRESSING
Lecture 3
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 3
King Khalid University
Faculty of Computer Science
Computer Engineering Department
ARRAY ADDRESSING
Lecture 4
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 4
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Matrix
Lecture 4
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 4
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 4
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 4
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 4
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 4
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 4
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 4
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 4
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 4
King Khalid University
Faculty of Computer Science
Computer Engineering Department
DELETING ELEMENTS
Lecture 4
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 4
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 4
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 4
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 4
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 4
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 4
112CPE– Graphical Programming– Lab Manual -Experiment #2 KKU-CSC-CE
Activity
2
King Khalid University
College of Computer Science
Department of Computer Engineering
Graphical Programming
112-CPE-1
Student Information
Name: ____________________________________________________________________________________
ID Number: ________________ Serial Number: ______________ Group Number: ___________
Data Submitted: _______________ Marks: …../2mark Instructor Signature: _________
Prepared by
Solution
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………….……………………………………………………………………………………………
Activity No.2:-
Create a column vector with 15 equally spaced elements in which the first element is –21 and
the last element is 12.
Solution
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………….……………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
Activity No.3:-
Use a single command to create a row vector (assign it to a variable named b) with 19 elements such that
b = 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 Do not type the vector explicitly.
Solution
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………….……………………………………………………………………………………………
………………………………………………………………………………………………………………
……………………………………………………………………………………………………………….
………………………………………………………………………………………………………
Activity No.4:- Create the following matrixes by typing one command. Do not type individual elements
explicitly.
Solution
………………………………………………………………………………………………………………
……………………………………………………………………………………………………………….
………………………………………………………………………………………………………………
………………………………………………………………………………………………………………
………………………………………………………………………………………………………………
………………………………………………………………………………………………………………
…………………………………………………………………………………………………………….…
………………………………………………………………………………………………………………
……………………………………………………………………………………………………………….
Graphical Programming
112-CCE-1
Student Information
Name: ____________________________________________________________________________________
ID Number: ________________ Serial Number: ______________ Group Number: ___________
Data Submitted: _______________ Marks: …../2mark Instructor Signature: _________
Prepared by
Lecture 6
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 6
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 6
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 6
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 6
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 6
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 6
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 6
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 6
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 6
King Khalid University
Faculty of Computer Science
Computer Engineering Department
ARRAY DIVISION
Identity matrix
Lecture 7
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 7
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Inverse of a matrix:
The matrix B is the inverse of the matrix A if, when the two
matrices are multiplied, the product is the identity matrix.
Both matrices must be square.
Lecture 7
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 7
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 7
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Determinants:
Lecture 7
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Array division:
MATLAB has two types of array division, right division and left division
Left division, \ :
Left division is used to solve the matrix equation AX = B.
Lecture 7
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Right division, I :
Lecture 7
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 7
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 7
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 7
King Khalid University
Faculty of Computer Science
Computer Engineering Department
ELEMENT-BY-ELEMENT OPERATIONS
Lecture 7
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 7
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 7
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 7
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 7
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 7
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 7
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 7
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 8
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 8
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 8
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 8
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 8
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 8
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 8
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 8
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 8
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 8
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 8
King Khalid University
Faculty of Computer Science
Computer Engineering Department
GENERATION OF
RANDOM NUMBERS
Lecture 8
King Khalid University
Faculty of Computer Science
Computer Engineering Department
Lecture 8
Activity
Graphical Programming
112-CCE-1
Student Information
Name: ____________________________________________________________________________________
ID Number: ________________ Serial Number: ______________ Group Number: ___________
Data Submitted: _______________ Marks: …../2mark Instructor Signature: _________
Prepared by
Solution
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………….……………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………….……………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………….……………………………………………………………………………………………
Activity No.2:-
Solution
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………….……………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
Graphical Programming
112-CCE-1
Student Information
Name: ____________________________________________________________________________________
ID Number: ________________ Serial Number: ______________ Group Number: ___________
Data Submitted: _______________ Marks: …../2mark Instructor Signature: _________
Prepared by
label 1000
Text
Tick-mark
800
INTENSITY (lux)
600
400
Data symbol
200
0
8 10 12 14 16 18 20 22 24
DISTANCE (cm)
x axis Tick-mark label
label
106-
TWO-DIMENSIONAL plot() COMMAND
110
plot(x,y)
Given data:
x 1 2 3 5 7 7.5 8 10
y 2 6.5 7 7 5.5 4 6 8
Once the plot command is executed, the Figure Window opens with
the following plot.
106-
PLOT OF GIVEN DATA 107
106-
LINE SPECIFIERS IN THE plot() COMMAND 107
plot(x,y,’line specifiers’)
106-
LINE SPECIFIERS IN THE plot() COMMAND 107
plot(x,y,‘line specifiers’)
The specifiers are optional. This means that none, one, two, or
all the three can be included in a command.
EXAMPLES:
plot(x,y) A solid blue line connects the points with no markers.
plot(x,y,’r’) A solid red line connects the points with no markers.
plot(x,y,’--y’) A yellow dashed line connects the points.
plot(x,y,’*’) The points are marked with * (no line between the
points.)
plot(x,y,’g:d’) A green dotted line connects the points which are
marked with diamond markers.
110-
PLOT OF GIVEN DATA USING LINE 111
SPECIFIERS IN THE plot() COMMAND
Line Specifiers:
dashed red line and
asterisk markers.
110-
PLOT OF GIVEN DATA USING LINE 111
SPECIFIERS IN THE plot() COMMAND
−0.5 x
Consider: y = 3.5 cos(6 x) for − 2 ≤ x ≤ 4
A script file for plotting the function is:
Once the plot command is executed, the Figure Window opens with
the following plot.
111-
A PLOT OF A FUNCTION 112
−0.5 x
y = 3.5 cos(6 x) for − 2 ≤ x ≤ 4
111-
CREATING A PLOT OF A FUNCTION 112
If the vector x is created with large spacing, the graph is not accurate.
Below is the previous plot with spacing of 0.3.
x = [-2:0.3:4];
y = 3.5.^(-0.5*x).*cos(6*x);
plot(x,y)
112-
THE fplot COMMAND 113
fplot(‘function’,limits)
The limits is a vector with the domain of x, and optionally with limits
of the y axis:
[xmin,xmax] or [xmin,xmax,ymin,ymax]
Line specifiers can be added.
112-
113
PLOT OF A FUNCTION WITH THE fplot() COMMAND
plot(x,y,u,v,t,h)
plot(x,y,’-b’,u,v,’—r’,t,h,’g:’)
114-
USING THE plot() COMMAND TO PLOT 115
MULTIPLE GRAPHS IN THE SAME PLOT
plot(x,y,'-b',x,yd,'--r',x,ydd,':k')
120
100
80
60
40
20
-20
-40
-2 -1 0 1 2 3 4
115-
USING THE hold on, hold off, COMMANDS 116
TO PLOT MULTIPLE GRAPHS IN THE SAME PLOT
hold on Holds the current plot and all axis properties so that
subsequent plot commands add to the existing plot.
This method is useful when all the information (vectors) used for
the plotting is not available a the same time.
115-
USING THE hold on, hold off, COMMANDS 116
TO PLOT MULTIPLE GRAPHS IN THE SAME PLOT
Plot of the function, y = 3 x 3 − 26 x + 10 and its first and second
derivatives, for − 2 ≤ x ≤ 4 all in the same plot.
x = [-2:0.01:4];
y = 3*x.^3-26*x+6;
yd = 9*x.^2-26;
ydd = 18*x;
plot(x,y,'-b') First graph is created.
hold on
plot(x,yd,'--r')
Two more graphs are created.
plot(x,ydd,':k')
hold off
EXAMPLE OF A FORMATTED 2-D PLOT 106
Plot title
Light Intensity as a Function of Distance
1200 Legend
Theory
y axis Experiment
label 1000
Text
800
Tick-mark
INTENSITY (lux)
600
400
Data symbol
200
0
8 10 12 14 16 18 20 22 24
DISTANCE (cm)
x axis Tick-mark label
label
116-
122
FORMATTING PLOTS
1. Formatting commands.
In this method commands, that make changes or additions to
the plot, are entered after the plot() command. This can be
done in the Command Window, or as part of a program in a
script file.
title(‘string’)
Adds the string as a title at the top of the plot.
xlabel(‘string’)
Adds the string as a label to the x-axis.
ylabel(‘string’)
Adds the string as a label to the y-axis.
legend(‘string1’,’string2’,’string3’)
Creates a legend using the strings to label various curves (when
several curves are in one plot). The location of the legend is
specified by the mouse.
text(x,y,’string’)
Places the string (text) on the plot at coordinate x,y relative to
the plot axes.
gtext(‘string’)
Places the string (text) on the plot. When the command
executes the figure window pops and the text location is clicked
with the mouse.
120-
EXAMPLE OF A FORMATTED PLOT 121
Below is a script file of the formatted light intensity plot (2nd slide).
(Some of the formatting options were not covered in the lectures,
but are described in the book)
xlabel('DISTANCE (cm)')
Labels for the axes.
ylabel('INTENSITY (lux)')
Title for the plot.
title('\fontname{Arial}Light Intensity as a Function of
Distance','FontSize',14)
axis([8 24 0 1200]) Setting limits of the axes.
text(14,700,'Comparison between theory and
Creating text.
experiment.','EdgeColor','r','LineWidth',2)
legend('Theory','Experiment',0) Creating a legend.
Graphical Programming
112-CCE-1
Student Information
Name: ____________________________________________________________________________________
ID Number: ________________ Serial Number: ______________ Group Number: ___________
Data Submitted: _______________ Marks: …../2mark Instructor Signature: _________
Prepared by
Solution
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………….……………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………….……………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………….……………………………………………………………………………………………
Activity No.2:-
1)
2)
3)
Solution
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………….……………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
Programming in MATLAB
Student Information
Name: ____________________________________________________________________________________
ID Number: ________________ Serial Number: ______________ Group Number: ___________
Prepared by
2
5.1 RELATIONAL AND LOGICAL OPERATORS
Relational operators:
3
5.1 RELATIONAL AND LOGICAL OPERATORS
Relational operators:
When two numbers are compared, the result is 1 (logical true) if the comparison,
according to the relational operator, is true, and 0 (logical false) if the comparison is false.
If two scalars are compared, the result is a scalar 1 or 0. If two arrays are compared (only
arrays of the same size can be compared), the comparison is done element-by-element, and
the result is a logical array of the same size with 1s and 0s according to the outcome of the
comparison at each address.
If a scalar is compared with an array, the scalar is compared with every element of the
array, and the result is a logical array with 1s and 0s according to the outcome of the
comparison of each element.
4
5.1 RELATIONAL AND LOGICAL OPERATORS
5
5.1 RELATIONAL AND LOGICAL OPERATORS
6
5.1 RELATIONAL AND LOGICAL OPERATORS
7
5.1 RELATIONAL AND LOGICAL OPERATORS
When a logical vector is used for addressing another vector, it extracts from that vector the
elements in the positions where the logical vector has 1s. For example:
8
5.1 RELATIONAL AND LOGICAL OPERATORS
Order of precedence:
In a mathematical expression that includes relational and arithmetic operations, the arithmetic
operations (+, –, *, /, \) have precedence over relational operations.
9
5.1 RELATIONAL AND LOGICAL OPERATORS
10
5.1 RELATIONAL AND LOGICAL OPERATORS
Logical operators have numbers as operands. A nonzero number is true, and a zero number is false.
11
5.1 RELATIONAL AND LOGICAL OPERATORS
12
5.1 RELATIONAL AND LOGICAL OPERATORS
Order of precedence:
13
5.1 RELATIONAL AND LOGICAL OPERATORS
14
5.1 RELATIONAL AND LOGICAL OPERATORS
15
5.1 RELATIONAL AND LOGICAL OPERATORS
16
5.1 RELATIONAL AND LOGICAL OPERATORS
17
5.1 RELATIONAL AND LOGICAL OPERATORS
18
5.2 CONDITIONAL STATEMENTS
19
5.2 CONDITIONAL STATEMENTS
20
5.2 CONDITIONAL STATEMENTS
5.2.1 The if-end Structure
21
5.2 CONDITIONAL STATEMENTS
Sample Problem 5-2: Calculating worker’s pay
A worker is paid according to his hourly wage up to 40 hours, and 50% more for overtime.
Write a program in a script file that calculates the pay to a worker. The program asks the user
to enter the number of hours and the hourly wage. The program then displays the pay.
22
5.2 CONDITIONAL STATEMENTS
Sample Problem 5-2: Calculating worker’s pay
Application of the program (in the Command Window) for two cases is shown below (the
file was saved as Workerpay):
23
5.2 CONDITIONAL STATEMENTS
5.2.2 The if-else-end Structure:
provides a means for
choosing one group of
commands, out of a
possible two groups,
for execution.
24
5.2 CONDITIONAL STATEMENTS
5.2.3 The if-elseif-else-end Structure
This structure includes two
conditional statements (if
and elseif) that make it
possible to select one out
of three groups of
commands for execution.
Several elseif statements
and associated groups of
commands can be added.
In this way more
conditions can be included.
25
5.2 CONDITIONAL STATEMENTS
Sample Problem 5-3: Water level in water tower
The tank in a water tower has the geometry
shown in the figure (the lower part is a cylinder
and the upper part is an inverted frustum of a
cone). Inside the tank there is a float that indicates
the level of the water. Write a MATLAB program
that determines the volume of the water in the tank
from the position (height h) of the float. The
program asks the user to enter a value of h in m,
and as output displays the volume of the water in
m3.
26
5.2 CONDITIONAL STATEMENTS
Sample Problem 5-3: Water level in water tower
27
Dr. Talal Skaik 2018
5.2 CONDITIONAL STATEMENTS
28
Dr. Talal Skaik 2018
5.2 CONDITIONAL STATEMENTS
The following is the display in the Command Window when the program is used with three
different values of water height.
29
5.3 THE switch-case STATEMENT
The switch-case statement is another method that can be used to direct the flow of a program.
It provides a means for choosing one group of commands for execution out of several possible
groups.
The first line is the switch command, which has the form:
Following the switch command are one or several case commands. Each has a value
(can be a scalar or a string) next to it (value1, value2, etc.) and an associated group of
commands below it.
After the last case command there is an optional otherwise command followed by a
group of commands.
The last line must be an end statement.
30
5.3 THE switch-case STATEMENT
31
5.3 THE switch-case STATEMENT
If there is more than one match, only the first matching case is executed.
If no match is found and the otherwise statement (which is optional) is present, the group
of commands between otherwise and end is executed.
If no match is found and the otherwise statement is not present, none of the command
groups is executed.
Note: In MATLAB only the first matching case is executed. After the group of commands
associated with the first matching case are executed, the program skips to the end statement.
This is different from the C language, where break statements are required.
32
5.3 THE switch-case STATEMENT
Sample Problem 5-4: Converting units of energy
Write a program in a script file that converts a quantity of energy (work) given in units of
either joule, ft-lb, cal, or eV to the equivalent quantity in different units specified by the
user. The program asks the user to enter the quantity of energy, its current units, and the
desired new units. The output is the quantity of energy in the new units.
33
5.3 THE switch-case STATEMENT
Converting
units of
energy
34
5.3 THE switch-case STATEMENT
Converting
units of
energy
35
5.3 THE switch-case STATEMENT
36
5.4 LOOPS
A loop is another method to alter the flow of a computer program.
In a loop, the execution of a command, or a group of commands, is repeated several times
consecutively.
Each round of execution is called a pass. In each pass at least one variable, but usually
more than one, or even all the variables that are defined within the loop, are assigned new
values.
MATLAB has two kinds of loops, for-end loops and while-end loops.
37
5.4 LOOPS
5.4.1 for-end Loops
In for-end loops the execution of a command, or a group of commands, is repeated a
predetermined number of times.
38
5.4 LOOPS 5.4.1 for-end Loops for k = f:s:t
The loop index variable can have any variable name (usually i, j, k, m, and n are used,
however, i and j should not be used if MATLAB is used with complex numbers).
In the first pass k = f and the computer executes the commands between the for and end
commands. Then, the program goes back to the for command for the second pass. k obtains a
new value equal to k = f + s, and the commands between the for and end commands are
executed with the new value of k. The process repeats itself until the last pass, where k = t.
Then the program does not go back to the for, but continues with the commands that follow
the end command. For example, if k = 1:2:9, there are five loops, and the corresponding
values of k are 1, 3, 5, 7, and 9.
The increment s can be negative (i.e.; k = 25:–5:10 produces four passes with k = 25, 20, 15,
10).
If the increment value s is omitted, the value is 1 (default) (i.e.; k = 3:7 produces five passes
with k = 3, 4, 5, 6, 7).
39
5.4 LOOPS 5.4.1 for-end Loops for k = f:s:t
The If f = t, the loop is executed once.
If f > t and s > 0, or if f < t and s < 0, the loop is not executed.
If the values of k, s, and t are such that k cannot be equal to t, then if s is positive, the last pass is
the one where k has the largest value that is smaller than t (i.e., k = 8:10:50 produces five passes
with k = 8, 18, 28, 38, 48). If s is negative, the last pass is the one where k has the smallest value
that is larger than t.
In the for command k can also be assigned a specific value (typed as a vector). Example: for k =
[7 9 –1 3 3 5].
The value of k should not be redefined within the loop.
Each for command in a program must have an end command.
The value of the loop index variable (k) is not displayed automatically. It is possible to display
the value in each pass (which is sometimes useful for debugging) by typing k as one of the
commands in the loop.
40
5.4 LOOPS 5.4.1 for-end Loops for k = f:s:t
When the loop ends, the loop index variable (k) has the value that was last assigned to it.
A simple example of a for-end loop (in a script file) is:
41
5.4 LOOPS 5.4.1 for-end Loops
Sample Problem 5-5: Sum of a series
42
5.4 LOOPS 5.4.1 for-end Loops Sample Problem 5-5: Sum of a series
(a)
43
5.4 LOOPS 5.4.1 for-end Loops Sample Problem 5-5: Sum of a series
(b)
44
5.4 LOOPS 5.4.1 for-end Loops Sample Problem 5-5: Sum of a series
(b)
45
5.4.1 for-end Loops Sample Problem 5-6: Modify vector elements
A vector is given by V = [5, 17, –3, 8, 0, –7, 12, 15, 20, –6, 6, 4, –7, 16]. Write a program as a
script file that doubles the elements that are positive and are divisible by 3 or 5, and, raises to the
power of 3 the elements that are negative but greater than –5.
46
5.4.2 while-end Loops
while-end loops are used in situations when looping is needed but the number of passes is
not known in advance.
In while-end loops the number of passes is not specified when the looping process starts.
Instead, the looping process continues until a stated condition is satisfied.
47
5.4.2 while-end Loops
48
5.4.2 while-end Loops
49
5.4.2 while-end Loops
50
5.4.2 while-end Loops Sample Problem 5-7: Taylor series representation of a function
51
5.4.2 while-end Loops Sample Problem 5-7: Taylor series representation of a function
52
5.5 NESTED LOOPS AND NESTED CONDITIONAL STATEMENTS
Loops and conditional statements can be nested within other loops or conditional statements.
This means that a loop and/or a conditional statement can start (and end) within another loop
or conditional statement.
53
5.5 NESTED LOOPS AND NESTED CONDITIONAL STATEMENTS
Write a program in a script file that creates an n x m matrix with elements that have the
following values:
The value of each element in the first row is the number of the column.
The value of each element in the first column is the number of the row.
The rest of the elements each has a value equal to the sum of the element above it and the
element to the left.
When executed, the program asks the user to enter values for n and m.
Solution: The program, shown next, has two loops (one nested) and a nested if-elseif-else-
end structure. The elements in the matrix are assigned values row by row. The loop index
variable of the first loop, k, is the address of the row, and the loop index variable of the
second loop, h, is the address of the column.
54
5.5 NESTED LOOPS AND NESTED CONDITIONAL STATEMENTS
55
5.5 NESTED LOOPS AND NESTED CONDITIONAL STATEMENTS
56
5.6 THE break AND continue COMMANDS
The continue command can be used inside a loop (for or while) to stop the present pass
and start the next pass in the looping process.
The continue command is usually a part of a conditional statement. When MATLAB
reaches the continue command, it does not execute the remaining commands in the loop, but
skips to the end command of the loop and then starts a new pass.
58
Experiment
King Khalid University
College of Computer Science
Department of Computer Engineering
5
Graphical Programming
112-CCE-1
Programming in MATLAB
Student Information
Name: ____________________________________________________________________________________
ID Number: ________________ Serial Number: ______________ Group Number: ___________
Prepared by
The sample maximum daily temperatures (in F) for Jeddah and Riyadh during the month of August 2021
TJ = [75 79 86 86 79 81 73 89 91 86 81 82 86 88 89 90 82 84 81 79 73 69 73 79 82 72 66 71 69 66 66]
TR = [69 68 70 73 72 71 69 76 85 87 74 84 76 68 79 75 68 68 73 72 79 68 68 69 71 70 89 95 90 66 69]
Write a program in a script file to answer the following:
(a) Calculate the average temperature for the month in each city.
(b) How many days was the temperature above the average in each city?
(c) How many days, and on which dates in the month, was the temperature in Jeddah lower than the
temperature in Riyadh?
(d) How many days, and on which dates in the month, was the temperature the same in both cities?
Solution
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………….……………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………….……………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………….……………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
Activity No.2:-
Write a program in a script file that determines the real roots of a quadratic equation .
Name the file quadroots. When the file runs, it asks the user to enter the values of the constants a, b,
and c. To calculate the roots of the equation the program calculates the discriminant D, given by:
.
If D > 0, the program displays message “The equation has two roots,” and the roots are displayed
in the next line.
If D = 0, the program displays message “The equation has one root,” and the root is displayed in
the next line.
If D < 0, the program displays message “The equation has no real roots.”
Run the script file in the Command Window three times to obtain solutions to the following three
equations:
(a)
(b)
(c)
Solution
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………….……………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………….……………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
Run the program with m = 5, m = 10, and m = 20. Compare the result with π. (Use format long.)
Solution
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………….……………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
………………….…………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…………………….………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………...
……………………………………………………………………………………………………………
Student Information
Name: ____________________________________________________________________________________
ID Number: ________________ Serial Number: ______________ Group Number: ___________
Prepared by
2
10.0
3
10.1 Line Plots
5
10.1 Line Plots
EXAMPLE
Suppose the spatial coordinates
vary with time as
𝑥𝑥 = 𝑡𝑡 sin(2𝑡𝑡)
𝑦𝑦 = 𝑡𝑡 cos(2𝑡𝑡)
𝑧𝑧 = 0.5𝑡𝑡
Make a line plot for 0 ≤ t ≤ 6π
6
10.1 Line Plots
7
10.2 Mesh and Surface Plots
8
10.2 Mesh and Surface Plots
9
10.2 Mesh and Surface Plots
10
10.2 Mesh and Surface Plots
4 4 4 4 4
3 3 3 3 3
Y =
2 2 2 2 2
1 1 1 1 1
11
10.2 Mesh and Surface Plots
Note that
▪X is made of identical rows because each row
of grid has the same x-coordinates
▪Y is made of identical columns because each
column of grid has same y-coordinates
To make matrices, use MATLAB
command
12
10.2 Mesh and Surface Plots
13
10.2 Mesh and Surface Plots
14
10.2 Mesh and Surface Plots
EXAMPLE
𝑥𝑥𝑦𝑦 2
Make mesh and surface plots of 𝑧𝑧 = 𝑥𝑥 2 +𝑦𝑦 2
over domain -1≤ x ≤ 3 and 1 ≤ y ≤ 4
15
10.2 Mesh and Surface Plots
16
10.2 Mesh and Surface Plots
17
10.2 Mesh and Surface Plots
19
10.3 Plots With Special Graphics
20
10.4 The view Command
22
10.4 The view Command
23
10.4 The view Command
24
10.4 The view Command
25
Experiment
King Khalid University
College of Computer Science
Department of Computer Engineering
6
Graphical Programming
112-CCE-1
3-D Plots
Student Information
Name: ____________________________________________________________________________________
ID Number: ________________ Serial Number: ______________ Group Number: ___________
Prepared by
Solution
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………….……………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………….……………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………….……………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
Activity No.2:-
Make a 3-D mesh plot of the below function in the domain
Solution
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………….……………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………….……………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………….……………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………….……………………………………………………………………………………………
……………………………………………………………………………………………………………
Activity No.3:-
Make a 3-D surface plot of the function given below, in the domain .
Solution
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…….………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………….……………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
………………….…………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
…………………….………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………...
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………...
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………
……………………………………………………………………………………………………………...
Student Information
Name: ____________________________________________________________________________________
ID Number: ________________ Serial Number: ______________ Group Number: ___________
Prepared by
Student Information
Name: ____________________________________________________________________________________
ID Number: ________________ Serial Number: ______________ Group Number: ___________
Prepared by