Pharos University in Alexandria
Faculty of Engineering 2024-2025
Department of Basic Sciences
EB102 (Engineering Mathematics 2)
MATLAB Session 3_Plotting & Constructing M-Files
OBJECTIVE:
- Refreshment for the basic plotting commands
- Build up an M-file (script file) to run a set of MATLAB commands in a collective way.
Recall:
You can plot any desired function using fplot; which plots the function between specified limits.
Example (1):
Plot the curve sin x over the interval [–2, 2].
Solution:
MATALB commands Output
fplot(@(x)sin(x),[-2*pi,2*pi]) 1
grid
0.5
-0.5
-1
-5 0 5
grid off
fplot(@(x)sin(x),[-pi,pi]) 1
0.5
-0.5
-1
-2 0 2
Note:
Try to use grid on in the example above instead of grid, both gives the same result.
To remove the grid added we use the grid off command.
Page 1 of 5
Pharos University in Alexandria
Faculty of Engineering 2024-2025
Department of Basic Sciences
Constructing M-Files:
Example (2): [Write an M-file]
Write an M-file that plots the curves ex, e–x over the interval [–1, 1] on a single graph.
➱SOLUTION STEPS:
(1) Open the MATLAB editor to start writing a MATLAB program (M-file) from Home menu →
New Script or Home menu → New →Script or CTRL+ N.
(2) Write in the empty editor the following MATLAB commands to perform the required example:
fplot(@(x)exp(x),[-1,1])
hold on
fplot(@(x)exp(-x),[-1,1])
hold off
Note: Here in the editor the enter key is used to move to a new line and not to execute the
command like on the command window as the whole script is executed once.
➱Recall: The hold function determines whether new graphics objects are added to the graph or
replace objects in the graph. hold on retains the current plot so that subsequent graphing commands
add to the existing graph. hold off resets properties to their defaults before drawing new plots. hold
off is the default.
(3) Save your file and name it (for example "plotExp.m").
Note: It’s not allowed to start this name with numbers or to include spaces and some symbols are
forbidden too. Also, this name should not be matched with any MATLAB built in function.
(4) Run your program from this icon; Hint: you can press F5.
Page 2 of 5
Pharos University in Alexandria
Faculty of Engineering 2024-2025
Department of Basic Sciences
Then the solution will be as shown below in a separate figure window;
Example (3): [Modify an existing M-file]
Modify the previous M-file to plot the curves ex, e–x over the interval [–2, 2] on a single graph.
➱SOLUTION STEPS:
(1) Return to the previous program "plotExp.m" and modify the first and third line to be [-2,2]
instead of [-1,1].
(2) Save your work by using the icon or just go to step 3, so MATLAB will update the
already saved file with your modifications.
(3) Run your program again and see the new result
Page 3 of 5
Pharos University in Alexandria
Faculty of Engineering 2024-2025
Department of Basic Sciences
0
-2 -1 0 1 2
(4) close the editor
Example (4): [Use an existing M-file to build a new one]
Write an M-file that plots the curves sin x, cos x over the interval [0, 2] on a single graph.
➱SOLUTION STEPS:
(1) Open your program "plotExp.m" from Home menu → Open and save as it
with a new name (for example "plotTrig.m").
(2) Modify the program to be as follows;
fplot(@(x)sin(x),[0,2*pi])
hold on
fplot(@(x)cos(x),[0,2*pi])
hold off
(3) Save your work.
(4) Run the new program.
Page 4 of 5
Pharos University in Alexandria
Faculty of Engineering 2024-2025
Department of Basic Sciences
Then the solution will be as shown below;
Classwork Activity:
Plot the function f(x)= ln(x) , for 0 ≤ x ≤ 3.
Solution:
© By Engineering Mathematics Group
Page 5 of 5