0% found this document useful (0 votes)
10 views25 pages

Matlab Lecture 3

This document provides an introduction to MATLAB script files, function files, and two-dimensional plots. It discusses that script files contain MATLAB commands saved in an M-file that are executed sequentially, while function files contain local variables and a function definition line. It also describes how the plot command is used to create two-dimensional plots from vector inputs, and how line style, color, and markers can be specified to customize plots.

Uploaded by

Aaditya Tyagi
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)
10 views25 pages

Matlab Lecture 3

This document provides an introduction to MATLAB script files, function files, and two-dimensional plots. It discusses that script files contain MATLAB commands saved in an M-file that are executed sequentially, while function files contain local variables and a function definition line. It also describes how the plot command is used to create two-dimensional plots from vector inputs, and how line style, color, and markers can be specified to customize plots.

Uploaded by

Aaditya Tyagi
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/ 25

Process Simulation Lab

(An Undergraduate Lab Course)

Lecture 3
Introduction to MATLAB

Course Instructor: Dr. Shabih Ul Hasan & Dr. Parul Katiyar


Motilal Nehru National Institute of Technology Allahabad
Prayagraj - 211004

1
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

SCRIPT FILES
AND
FUNCTION FILES
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Script Files
• A script file is a sequence of MATLAB commands, called a program, that is saved in a
file. When the script file is executed, MATLAB executes the commands in the order
they are listed.

• The script files are called M-files and are saved in the MATLAB in the current directory
pane with an extension .m after the file name.

• When a script file is executed, all the variables used in the script files are stored in the
matlab workspace. The variables that are shared with workspace are called global
variables in matlab.
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Working with Script Files


• Script files are created and modified in the editor window, to open the editor window,
Give the command edit at the command prompt or click on New icon in the Editor
tab in Home ribbon and select Script.

• To save a script file, click on Save icon in the Editor tab in Home ribbon.
• Note: the rules for naming the file to be saved are same as the rules for defining a
variable.

• To execute a script file either type its name at the command prompt or double click the
filename in the Current Directory Pane
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Click here Click here


to create to save

A Script file

Double click here to execute


Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Input to Script Files


1. The variable is defined and assigned a value in the script file.
2. The variable is defined and assigned a value in command window.
3. The variable is defined in the script file, but a specific value is entered in the command
window when the script file is executed.
– This is done by using input command for creating the variables.

Variable_name = input(‘String with a message that is displayed


in the command window’)
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Function Files
• A function file is also an M-file, like a script file, except that the variables in a function
file are all local variables. They do not appear in the matlab workspace and have their
own workspace.

• These files can be executed as a function on Matlab command window and are known as
User defined functions.
• .
• A function file begins with a function definition line, which has a well-defined list of
inputs and outputs. Without this line, the file becomes a script file.

• NOTE: the function name must be the same as the file name in which the function is
written. For example, if the name of the function is projectile it must be written and
saved in a file with the name projectile.m.
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Anatomy of a Function File

8
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

A Simple Function File

To Execute a Function File

9
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Function Files
• A function file is also an M-file, like a script file, except that the variables in a function
file are all local variables. They do not appear in the matlab workspace and have their
own workspace.

• These files can be executed as a function on Matlab command window and are known as
User defined functions.
• .
• A function file begins with a function definition line, which has a well-defined list of
inputs and outputs. Without this line, the file becomes a script file.

• NOTE: the function name must be the same as the file name in which the function is
written. For example, if the name of the function is projectile it must be written and
saved in a file with the name projectile.m.
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Output Commands
• Two commands that are frequently used to generate output are disp and fprintf.

• The disp command displays the displays the output on the screen.

• The fprintf command can be used to display the output on the screen. It also offers
several formatting options for output.
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

‘disp’ Command
• The disp command is used to display the elements of a variables without displaying the
name of the variable, and to display text.

disp(name of a variable) or disp(‘text as string’)


for ex.,
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

‘fprintf’ Command
• The fprintf command can be used to display output (text and data). With this
command (unlike with disp command) the output can be formatted.

• With many available options, the fprintf command can be long and complicated. To
avoid confusion, the command is presented gradually.

• Using the fprintf command to display text:


fprintf(‘text typed in as a string’)

• e.g.,
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

‘fprintf’ Command
• With fprintf command it is possible to start a new line in the middle of the string. This
is done by inserting \n before the character that will start the new line.

• When a program has more than one fprintf command, the display generated is
continuous (the fprintf command does not automatically start a new line). This is true
even if there are other commands between fprintf commands.

• Problem: Write the following sentence in Matlab command window using fprintf
command.
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

‘fprintf’ Command
• Using fprintf command to display a mix of text and numeric data:

fprintf(‘text as string %5.2f additional text’, variable_name)

Example:
fprintf(‘An average of %f points was scored in three games.’, avg_point )

• fprintf can also take multiple variables in a single text.


Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Additional Remarks
• To place a single quotation mark in the displayed text, type two single quotation marks
in the string inside the command.

• The fprintf command is vectorized. This means that when a variable that is a vector or
a matrix is included in the command, the command repeats itself until all the elements
are displayed. If the variable is matrix, the data is used column by column.
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

TWO
DIMENSIONAL
PLOTS
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

‘plot’ Command
• The basic 2-D plot command is: plot(x,y)
where x is a vector (one dimensional array), and y is a vector. Both vectors must have
same length.

• The plot command creates a single curve with x values on the abscissa (horizontal axis)
and the y values on the ordinate (vertical axis).

• The curve is made from segments of lines that connect the points that are defined by x
and y coordinates of the elements in the two vectors.

Example:.
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

‘plot’ Command with Specifier


plot(x, y, ’linespecifiers’)

Note about using the specifiers


– The specifiers are typed inside the plot command as strings.
– Within the strings specifiers can be typed in any order.
– The specifiers are optional. This means that none, one, two, or all three types can
be included in one command.
Some Examples:
– plot(x,y) : A blue solid line connects the points with no marker (default).
– plot(x,y, ‘r’) : A red solid line connects the points.
– 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 that are marked with
diamond markers.
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Lines and Markers in Plot


Line color Specifier
Line Style Specifier
Red r
Solid(default) - Green g
Blue b
Dashed -- Magenta m
Dotted : Yellow y
Black k
dash-dot -. White w

Marker Type Specifier Marker Type Specifier


Plus sign + Square s
Circle o Diamond d
Asterisk * Five-pointed star p
Point . Six-pointed star h
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Formatting a Plot
• xlabel and ylabel : xlabel(‘string’) & ylabel(‘string’)

• Title : title(‘text as string’)

• Legend : legend(‘string1’, ‘string2’, ‘string3’,……..)

• Axis : axis([xmin, xmax, ymin, ymax])

• Grid : grid on or grid off


Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Plotting Multiple Graphs in the Same


Plot
• If the entire set of data is available, plot command with multiple arguments may be used
to generate an overlay plot.

• For example, if we have three sets of data-(xl, yl) , (x2, y2) , and (x3, y3).
the command plot (x1 , y1, x2 , y2 , ' :', x3 , y3, 'o') plots (xl, yl) with a solid line,
(x2, y2) with a dotted line, and (x3, y3) as unconnected points marked by small circles
(o) , all on the same graph.

• There is another way to plot multiple graph in the same plot. After plotting the first
graph, use the command hold on. Then, plot another graph, the graph will be plotted on
same plot. To end plotting on the same plot use the command hold off.
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Plotting Multiple Graphs in the Same


Plot
• Example script file using hold command
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

‘fplot’ Command
• The fplot command plots a function with the form y = f(x) between specified limit.
fplot(‘function’, limits, ‘line specifiers’)

Where,
-‘function’ : The function can be typed directly as a string inside the command.
-‘limits’ : The limits argument is a vector with two elements that specify the domain
of x [xmin, xmax], or a vector with four elements that specify the domain of x and the
limits of y-axis [xmin, xmax, ymin, ymax].
-‘line specifiers’ : The line specifiers are the same as in the plot command.
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

‘fplot’ Command
Example : fplot(‘x^2+4*sin(2*x)-1’, [-3 3])
After using the above stated command, the following plot is generated on matlab figure
window

You might also like