Professional Documents
Culture Documents
DOSS - I - Assignment No. 7 (MATLAB)
DOSS - I - Assignment No. 7 (MATLAB)
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.
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.
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.
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.
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.
• Every time the disp command is executed, the display it generates appears in a new line. One example is:
When this file (saved as Chapter4Example5) is executed, the display in the Command Window is:
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.
Programming :
Conditional statement :
if-end structure:
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).
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.
2.
1. Solution :
2. Solution :