LAB ACTIVITY 2 - Introduction To MATLAB PART2
LAB ACTIVITY 2 - Introduction To MATLAB PART2
College of Engineering
Computer Engineering Department
Experiment No.2
Introduction to MATLAB (Part 2)
Score
Submitted by
Last Name, First Name, MI.
Submitted to
Professor’s Name
Instructor
Date Submitted
Month Date, Year
Experiment No. 2
Introduction to MATLAB (Part 2)
I. OBJECTIVES
The objectives of this experiment, using the MATLAB software, are for the students to:
1. learn how to represent polynomials in MATLAB
2. find roots of polynomials
3. create polynomials when roots are known
4. obtain partial fractions
5. learn how to write M-file scripts and MATLAB Functions
6. acquire knowledge on MATLAB flow control like ‘if-elseif-end’, ‘for loops’ and
‘while loops’
Symbolic Math Toolbox contains additional specialized support for polynomial operations.
Scripts, which do not accept input arguments or return output arguments. They operate on
data in the workspace. MATLAB provides a full programming language that enables you to
write a series of MATLAB statements into a file and then execute them with a single
command. You write your program in an ordinary text file, giving the file a name of
‘filename.m’. The term you use for ‘filename’ becomes the new command that MATLAB
associates with the program. The file extension of .m makes this a MATLAB M-file.
Functions, which can accept input arguments and return output arguments. Internal variables
are local to the function.
If you're a new MATLAB programmer, just create the M-files that you want to try out in the
current directory. As you develop more of your own M-files, you will want to organize them into
other directories and personal toolboxes that you can add to your MATLAB search path. If you
duplicate function names, MATLAB executes the one that occurs first in the search path.
Representing Polynomials
MATLAB represents polynomials as row vectors containing coefficients ordered by descending
powers. For example, consider the equation
This is the celebrated example Wallis used when he first represented Newton's method to the
French Academy.
Polynomial Roots
The roots function calculates the roots of a polynomial:
Output Screenshot:
By convention, MATLAB stores roots in column vectors. The function poly returns to the
polynomial coefficients:
Output Screenshot:
Output Screenshot:
It is also possible to evaluate a polynomial in a matrix sense. In this case the equation
becomes , where X is a square matrix and I is the
identity matrix.
Output Screenshot:
Output Screenshot:
Output Screenshot:
Polynomial Derivatives
The polyder function computes the derivative of any polynomial. To obtain the derivative of the
polynomial
Output Screenshot:
polyder also computes the derivative of the product or quotient of two polynomials. For example,
create two polynomials a and b:
Output Screenshot:
Calculate the derivative of the product a*b by calling polyder with a single output argument:
Output Screenshot:
Calculate the derivative of the quotient a/b by calling polyder with two output arguments:
Output Screenshot:
‘residue’ finds the partial fraction expansion of the ratio of two polynomials. This is particularly
useful for applications that represent systems in transfer function form. For polynomials b and a,
if there are no multiple roots, where r is a column vector of residues, p is a column vector of pole
locations, and k is a row vector of direct terms.
Output Screenshot:
Given three input arguments (r, p, and k), residue converts back to polynomial form:
Output Screenshot:
Scripts
When you invoke a script, MATLAB simply executes the commands found in the file. Scripts
can operate on existing data in the workspace, or they can create new data on which to operate.
Although scripts do not return output arguments, any variables that they create remain in the
workspace, to be used in subsequent computations. In addition, scripts can produce graphical
output using functions like plot.
For example, create a file called ‘myprogram.m’ that contains these MATLAB commands:
Type the statement ‘myprogram’ in MATLAB’s command window. After execution of the file is
complete, the variable ‘r’ remains in the workspace.
Output Screenshot:
Functions
Functions are M-files that can accept input arguments and return output arguments. The names
of the M-file and of the function should be the same. Functions operate on variables within their
own workspace, separate from the workspace you access at the MATLAB command prompt. An
example is provided below:
The first line of a function M-file starts with the keyword ‘function’. It gives the function name
and order of arguments. In this case, there is one input arguments and one output argument. The
next several lines, up to the first blank or executable line, are comment lines that provide the help
text. These lines are printed when you type ‘help fact’. The first line of the help text is the H1
line, which MATLAB displays when you use the ‘lookfor’ command or request help on a
directory. The rest of the file is the executable MATLAB code defining the function.
The variable n & f introduced in the body of the function as well as the variables on the first line
are all local to the function; they are separate from any variables in the MATLAB workspace.
This example illustrates one aspect of MATLAB functions that is not ordinarily found in other
programming languages—a variable number of arguments. Many M-files work this way. If no
output argument is supplied, the result is stored in ans. If the second input argument is not
supplied, the function computes a default value.
Flow Control:
Conditional Control – if, else, switch
This section covers those MATLAB functions that provide conditional program control. if, else,
and elseif. The if statement evaluates a logical expression and executes a group of statements
when the expression is true. The optional elseif and else keywords provide for the execution of
alternate groups of statements. An end keyword, which matches the if, terminates the last group
of statements. The groups of statements are delineated by the four keywords—no braces or
brackets are involved as given below.
if <condition>
<statements>;
elseif <condition>
<statements>;
else
<statements>;
end
It is important to understand how relational operators and if statements work with matrices.
When you want to check for equality between two variables, you might use
if A == B, ...
This is valid MATLAB code, and does what you expect when A and B are scalars. But when A
and B are matrices, A == B does not test if they are equal, it tests where they are equal; the result
is another matrix of 0's and 1's showing element-by-element equality. (In fact, if A and B are not
the same size, then A == B is an error.)
Output Screenshot:
The proper way to check for equality between two variables is to use the isequal function:
if isequal(A, B), ...
isequal returns a scalar logical value of 1 (representing true) or 0 (false), instead of a matrix, as
the expression to be evaluated by the if function. Using the A and B matrices from above, you
get
Output Screenshot:
Here is another example to emphasize this point. If A and B are scalars, the following program
will never reach the "unexpected situation". But for most pairs of matrices, including
if A > B
'greater'
elseif A < B
'less'
elseif A == B
'equal'
else
error('Unexpected situation')
end
our magic squares with interchanged columns, none of the matrix conditions A > B, A < B, or A
== B is true for all elements and so the else clause is executed:
Several functions are helpful for reducing the results of matrix comparisons to scalar conditions
for use with if, including ‘isequal’, ‘isempty’, ‘all’, ‘any’.
There must always be an end to match the switch. An example is shown below.
Unlike the C language switch statement, MATLAB switch does not fall through. If the first case
statement is true, the other case statements do not execute. So, break statements are not required.
for, while, break and continue
This section covers those MATLAB functions that provide control over program loops.
for
The ‘for’ loop, is used to repeat a group of statements for a fixed, predetermined number of
times. A matching ‘end’ delineates the statements. The syntax is as follows:
The semicolon terminating the inner statement suppresses repeated printing, and the r after the
loop displays the final result.
It is a good idea to indent the loops for readability, especially when they are nested:
while
The ‘while’ loop, repeats a group of statements indefinite number of times under control of a
logical condition. So a while loop executes atleast once before it checks the condition to stop the
execution of statements. A matching ‘end’ delineates the statements. The syntax of the ‘while’
loop is as follows:
while <condition>
<statements>;
end
Here is a complete program, illustrating while, if, else, and end, that uses interval bisection to
find a zero of a polynomial:
Output Screenshot:
break
The break statement lets you exit early from a ‘for’ loop or ‘while’ loop. In nested loops, break
exits from the innermost loop only.
continue
The continue statement passes control to the next iteration of the for loop or while loop in which
it appears, skipping any remaining statements in the body of the loop. The same holds true for
continue statements in nested loops. That is, execution continues at the beginning of the loop in
which the continue statement was encountered.
V. ANALYSIS / OBSERVATION
VI. CONCLUSION