Matlab Lecture 3
Matlab Lecture 3
Lecture 3
Introduction to MATLAB
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
• 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
A Script file
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
8
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj
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.
‘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.
• 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:
Example:
fprintf(‘An average of %f points was scored in three games.’, avg_point )
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
Formatting a Plot
• xlabel and ylabel : xlabel(‘string’) & ylabel(‘string’)
• 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
‘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