You are on page 1of 18

MAHARASHTRA INSTITUTE OF TECHNOLOGY, LABORATORY

AURANGABAD (An Autonomous Institute) MANUAL


PRACTICAL EXPERIMENT INSTRUCTION SHEET
DEPARTMENT: MECHANICAL ENGINEERING
LABORATORY: MIT-MED- Lab - Development of Software skills – I YEAR: 2021-22
Class: Third Year PART: I SUBJECT: Development of Software skills – I (Course Code – MED327)

Experiment No:- 07
Title:- MATLAB Programming for problem solving.
Script Files :

So far all the commands were typed in the Command Window and were executed when the Enter
key was pressed. Although every MATLAB command can be executed in this way, using the Command
Window to execute a series of commands— especially if they are related to each other (a program)—is
not convenient and may be difficult or even impossible. The commands in the Command Window cannot
be saved and executed again. In addition, the Command Window is not interactive. This means that every
time the Enter key is pressed only the last command is executed, and everything executed before is
unchanged. If a change or a correction is needed in a command that was previously executed and the
results of this command are used in commands that follow, all the commands have to be entered and
executed again.
A different (better) way of executing commands with MATLAB is first to create a file with a list
of commands (program), save it, and then run (execute) the file. When the file runs, the commands it
contains are executed in the order that they are listed. If needed, the commands in the file can be corrected
or changed and the file can be saved and run again. Files that are used for this purpose are called script
files.

Notes About Script Files :


• A script file is a sequence of MATLAB commands, also called a program.
• When a script file runs (is executed), MATLAB executes the commands in the order they are written just as if
they were typed in the Command Window.
• When a script file has a command that generates an output (e.g. assignment of a value to a variable without
semicolon at the end), the output is displayed in the Command Window.
• Using a script file is convenient because it can be edited (corrected and/or changed) and executed many times.
• Script files can be typed and edited in any text editor and then pasted into the MATLAB editor.
• Script files are also called M-files because the extension .m is used when they are saved.

PREPARED BY: Sameer M. Shaikh APPROVED BY: HMED


MAHARASHTRA INSTITUTE OF TECHNOLOGY, LABORATORY
AURANGABAD (An Autonomous Institute) MANUAL
PRACTICAL EXPERIMENT INSTRUCTION SHEET
DEPARTMENT: MECHANICAL ENGINEERING
LABORATORY: MIT-MED- Lab - Development of Software skills – I YEAR: 2021-22
Class: Third Year PART: I SUBJECT: Development of Software skills – I (Course Code – MED327)

Creating and Saving a Script File

In MATLAB script files are created and edited in the Editor/Debugger Window. This window is opened from the
Command Window. In the File menu, select New, and then select script/M-file. An open Editor/Debugger Window
is shown in Figure.

PREPARED BY: Sameer M. Shaikh APPROVED BY: HMED


MAHARASHTRA INSTITUTE OF TECHNOLOGY, LABORATORY
AURANGABAD (An Autonomous Institute) MANUAL
PRACTICAL EXPERIMENT INSTRUCTION SHEET
DEPARTMENT: MECHANICAL ENGINEERING
LABORATORY: MIT-MED- Lab - Development of Software skills – I YEAR: 2021-22
Class: Third Year PART: I SUBJECT: Development of Software skills – I (Course Code – MED327)

Once the window is open, the commands of the script file are typed line by line. MATLAB automatically
numbers a new line every time the Enter key is pressed. The commands can also be typed in any text
editor or word processor program and then copied and pasted in the Editor/Debugger Window. An
example of a short program typed in the Editor/Debugger Window is shown in Figure. The first few lines
in a script file are typically comments (which are not executed since the first character in the line is %)
that describe the program written in the script file.

Before a script file can be executed it has to be saved. This is done by choosing Save As... from the File
menu, selecting a location, and entering a name for the file. When saved, MATLAB adds the extension .m
to the mane. The rules for naming a script file follow the rules of naming a variable (must begin with a
letter, can include digits and underscore, no spaces, and be up to 63 characters long). The names of user-
defined variables, predefined variables, and MATLAB commands or functions should not be used as
names of script files.

Running (Executing) a Script File

A script file can be executed either directly from the Editor Window by clicking on the Run icon or by
typing the file name in the Command Window and then pressing the Enter key.

PREPARED BY: Sameer M. Shaikh APPROVED BY: HMED


MAHARASHTRA INSTITUTE OF TECHNOLOGY, LABORATORY
AURANGABAD (An Autonomous Institute) MANUAL
PRACTICAL EXPERIMENT INSTRUCTION SHEET
DEPARTMENT: MECHANICAL ENGINEERING
LABORATORY: MIT-MED- Lab - Development of Software skills – I YEAR: 2021-22
Class: Third Year PART: I SUBJECT: Development of Software skills – I (Course Code – MED327)

input command :
In this case the variable is defined in the script file and when the file is executed, the user is prompted to assign a
value to the variable in the Command Window. This is done by using the input command to create the variable.
The form of the input command is:

output commands :
MATLAB automatically generates a display when some commands are executed. For example, when a variable is
assigned a value, or the name of a previously assigned variable is typed and the Enter key is pressed, MATLAB
displays the variable and its value. This type of output is not displayed if a semicolon is typed at the end of the
command. In addition to this automatic display, MATLAB has several commands that can be used to generate
displays. The displays can be messages that provide information, numerical data, and plots.

The disp Command :


The disp command is us ed to display the elements of a variable without displaying the name of the variable, and
to display text. The format of the disp command is:

• Every time the disp command is executed, the display it generates appears in a new line. One example is:

PREPARED BY: Sameer M. Shaikh APPROVED BY: HMED


MAHARASHTRA INSTITUTE OF TECHNOLOGY, LABORATORY
AURANGABAD (An Autonomous Institute) MANUAL
PRACTICAL EXPERIMENT INSTRUCTION SHEET
DEPARTMENT: MECHANICAL ENGINEERING
LABORATORY: MIT-MED- Lab - Development of Software skills – I YEAR: 2021-22
Class: Third Year PART: I SUBJECT: Development of Software skills – I (Course Code – MED327)
The next example shows the use of the disp command in the script file that calculates the average points scored
in three games.

When this file (saved as Chapter4Example5) is executed, the display in the Command Window is:

PREPARED BY: Sameer M. Shaikh APPROVED BY: HMED


MAHARASHTRA INSTITUTE OF TECHNOLOGY, LABORATORY
AURANGABAD (An Autonomous Institute) MANUAL
PRACTICAL EXPERIMENT INSTRUCTION SHEET
DEPARTMENT: MECHANICAL ENGINEERING
LABORATORY: MIT-MED- Lab - Development of Software skills – I YEAR: 2021-22
Class: Third Year PART: I SUBJECT: Development of Software skills – I (Course Code – MED327)

The fprintf Command

The fprintf command can be used to display output (text and data) on the screen or to save it to a file. With this
command (unlike with the disp command) the output can be formatted. For example, text and numerical values
of variables can be intermixed and displayed in the same line. In addition, the format of the numbers can be
controlled.

PREPARED BY: Sameer M. Shaikh APPROVED BY: HMED


MAHARASHTRA INSTITUTE OF TECHNOLOGY, LABORATORY
AURANGABAD (An Autonomous Institute) MANUAL
PRACTICAL EXPERIMENT INSTRUCTION SHEET
DEPARTMENT: MECHANICAL ENGINEERING
LABORATORY: MIT-MED- Lab - Development of Software skills – I YEAR: 2021-22
Class: Third Year PART: I SUBJECT: Development of Software skills – I (Course Code – MED327)

PREPARED BY: Sameer M. Shaikh APPROVED BY: HMED


MAHARASHTRA INSTITUTE OF TECHNOLOGY, LABORATORY
AURANGABAD (An Autonomous Institute) MANUAL
PRACTICAL EXPERIMENT INSTRUCTION SHEET
DEPARTMENT: MECHANICAL ENGINEERING
LABORATORY: MIT-MED- Lab - Development of Software skills – I YEAR: 2021-22
Class: Third Year PART: I SUBJECT: Development of Software skills – I (Course Code – MED327)

PREPARED BY: Sameer M. Shaikh APPROVED BY: HMED


MAHARASHTRA INSTITUTE OF TECHNOLOGY, LABORATORY
AURANGABAD (An Autonomous Institute) MANUAL
PRACTICAL EXPERIMENT INSTRUCTION SHEET
DEPARTMENT: MECHANICAL ENGINEERING
LABORATORY: MIT-MED- Lab - Development of Software skills – I YEAR: 2021-22
Class: Third Year PART: I SUBJECT: Development of Software skills – I (Course Code – MED327)

Programming :

Conditional statement :

PREPARED BY: Sameer M. Shaikh APPROVED BY: HMED


MAHARASHTRA INSTITUTE OF TECHNOLOGY, LABORATORY
AURANGABAD (An Autonomous Institute) MANUAL
PRACTICAL EXPERIMENT INSTRUCTION SHEET
DEPARTMENT: MECHANICAL ENGINEERING
LABORATORY: MIT-MED- Lab - Development of Software skills – I YEAR: 2021-22
Class: Third Year PART: I SUBJECT: Development of Software skills – I (Course Code – MED327)

if-end structure:

PREPARED BY: Sameer M. Shaikh APPROVED BY: HMED


MAHARASHTRA INSTITUTE OF TECHNOLOGY, LABORATORY
AURANGABAD (An Autonomous Institute) MANUAL
PRACTICAL EXPERIMENT INSTRUCTION SHEET
DEPARTMENT: MECHANICAL ENGINEERING
LABORATORY: MIT-MED- Lab - Development of Software skills – I YEAR: 2021-22
Class: Third Year PART: I SUBJECT: Development of Software skills – I (Course Code – MED327)

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. In for-end loops the number of passes is specified when
the loop starts. In while-end loops the number of passes is not known ahead of time, and the looping process
continues until a specified condition is satisfied. Both kinds of loops can be terminated at any time with the break
command.

for-end loops:
In for-end loops the execution of a command, or a group of commands, is repeated a predetermined number of
times. The form of the loop is shown in Figure.
• The loop index variable can have any variable name (usually i, j, k, m, and n are used. 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 the 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 the 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 value of k in each loop is 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).
• 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 specific value (typed in as a vector).

PREPARED BY: Sameer M. Shaikh APPROVED BY: HMED


MAHARASHTRA INSTITUTE OF TECHNOLOGY, LABORATORY
AURANGABAD (An Autonomous Institute) MANUAL
PRACTICAL EXPERIMENT INSTRUCTION SHEET
DEPARTMENT: MECHANICAL ENGINEERING
LABORATORY: MIT-MED- Lab - Development of Software skills – I YEAR: 2021-22
Class: Third Year PART: I SUBJECT: Development of Software skills – I (Course Code – MED327)

For 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 (sometimes useful for debugging) by typing k as one of the commands in the loop.
• When the loop ends, the loop index variable (k) has the value that was last assigned to it.

PREPARED BY: Sameer M. Shaikh APPROVED BY: HMED


MAHARASHTRA INSTITUTE OF TECHNOLOGY, LABORATORY
AURANGABAD (An Autonomous Institute) MANUAL
PRACTICAL EXPERIMENT INSTRUCTION SHEET
DEPARTMENT: MECHANICAL ENGINEERING
LABORATORY: MIT-MED- Lab - Development of Software skills – I YEAR: 2021-22
Class: Third Year PART: I SUBJECT: Development of Software skills – I (Course Code – MED327)

while-end loops:
while-end loops are used in situations when looping is needed but the number of passes is not known ahead of
time. 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. The structure of a whileend loop is shown in
Figure.

PREPARED BY: Sameer M. Shaikh APPROVED BY: HMED


MAHARASHTRA INSTITUTE OF TECHNOLOGY, LABORATORY
AURANGABAD (An Autonomous Institute) MANUAL
PRACTICAL EXPERIMENT INSTRUCTION SHEET
DEPARTMENT: MECHANICAL ENGINEERING
LABORATORY: MIT-MED- Lab - Development of Software skills – I YEAR: 2021-22
Class: Third Year PART: I SUBJECT: Development of Software skills – I (Course Code – MED327)

PREPARED BY: Sameer M. Shaikh APPROVED BY: HMED


MAHARASHTRA INSTITUTE OF TECHNOLOGY, LABORATORY
AURANGABAD (An Autonomous Institute) MANUAL
PRACTICAL EXPERIMENT INSTRUCTION SHEET
DEPARTMENT: MECHANICAL ENGINEERING
LABORATORY: MIT-MED- Lab - Development of Software skills – I YEAR: 2021-22
Class: Third Year PART: I SUBJECT: Development of Software skills – I (Course Code – MED327)

PREPARED BY: Sameer M. Shaikh APPROVED BY: HMED


MAHARASHTRA INSTITUTE OF TECHNOLOGY, LABORATORY
AURANGABAD (An Autonomous Institute) MANUAL
PRACTICAL EXPERIMENT INSTRUCTION SHEET
DEPARTMENT: MECHANICAL ENGINEERING
LABORATORY: MIT-MED- Lab - Development of Software skills – I YEAR: 2021-22
Class: Third Year PART: I SUBJECT: Development of Software skills – I (Course Code – MED327)

Questions for Assignment/Experiment No. 7 –


1.

2.

PREPARED BY: Sameer M. Shaikh APPROVED BY: HMED


MAHARASHTRA INSTITUTE OF TECHNOLOGY, LABORATORY
AURANGABAD (An Autonomous Institute) MANUAL
PRACTICAL EXPERIMENT INSTRUCTION SHEET
DEPARTMENT: MECHANICAL ENGINEERING
LABORATORY: MIT-MED- Lab - Development of Software skills – I YEAR: 2021-22
Class: Third Year PART: I SUBJECT: Development of Software skills – I (Course Code – MED327)

1. Solution :

PREPARED BY: Sameer M. Shaikh APPROVED BY: HMED


MAHARASHTRA INSTITUTE OF TECHNOLOGY, LABORATORY
AURANGABAD (An Autonomous Institute) MANUAL
PRACTICAL EXPERIMENT INSTRUCTION SHEET
DEPARTMENT: MECHANICAL ENGINEERING
LABORATORY: MIT-MED- Lab - Development of Software skills – I YEAR: 2021-22
Class: Third Year PART: I SUBJECT: Development of Software skills – I (Course Code – MED327)

2. Solution :

PREPARED BY: Sameer M. Shaikh APPROVED BY: HMED

You might also like