You are on page 1of 7

Chapter 1

Introduction to MATLAB

1.1 Command Window 1-1


1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.1 Command Window

Various font style used in MATLAB -

Courier font Screen Display


Helvetica User inputs at MATLAB’s
command window prompt
Helvetica bold MATLAB Function
Bold Italic Important terms and facts,
notes, filenames

 When we first start MATLAB, we see the toolbar on top of


the command screen and the prompt >>
 We can use the Editor/Debugger to write our program, save
it, and return to the command screen to execute the
program.
To use editor/Debugger:

 From the File menu > New > M−File.


 This takes us to the Editor Window where we can type our
script (list of statements) for a new file, or open a
previously saved file.
Points to remember for giving file-name for M-file:

 File name must start with letter.


 MATLAB is case sensitive thus t and T are different
characters in MATLAB language.
 Extension for M-file is .m so create file with filename
like myfile01.m
To execute a program, we type the file name without the .m
extension at the >> prompt; then, we press <enter> and observe
the execution and the values obtained from it. If we have
saved our file in drive a or any other drive, we must make
sure that it is added it to the desired directory in MATLAB’s
search path.

‘help’ command:
 The command help matlab iofun will display input/output
information. To get help with other MATLAB topics, we
can type help followed by any topic from the displayed
menu. For example, to get information on graphics, we
type help matlab graphics.

Exiting MATLAB:
 When we are done and want to leave MATLAB, we type quit
or exit.

‘clear’ command:
 If we want to clear all previous values, variables, and
equations without exiting, we should use the clear
command. This command erases everything; it is like
exiting MATLAB and starting it again.

‘clc’ command:
 The clc command clears the screen but MATLAB still
remembers all values, variables and equations which we
have already used.

MATLAB involving complex function:


 One of the most powerful features of MATLAB is the
ability to do computations involving complex numbers. We
can use either i or j to denote the imaginary part of a
complex number, such as 3-2j or 3-2i. Multiplication sign
is not necessary.
 If the imaginary part is a function or variable such as,
we must use the multiplication sign, that is, we must
type cos(x)*j or j*cos(x).

1.2 Roots of Polynomials

 In MATLAB, a polynomial is expressed as a row vector of


the form
[an an-1 a2 a1 a0].
 The elements of this vector are the coefficients of the
polynomial in descending order. We must include terms
whose coefficients are zero.
‘roots’ function:
We can find the roots of any polynomial with the
roots(p) function where p is a row vector
containing
the polynomial coefficients in descending order.
Example:
>> a=[1 2 1]; % a(x) = x2+2x+1
>> b=roots(a)
b =
-1
-1
1.3 Polynomial Construction from known roots
 We can compute the coefficients of a polynomial from a
given set of roots with the poly(r) function, where r is
a row vector containing the roots.
Example:
>> c=[-1 -1]; % roots are -1 and -1
>> d=poly(c)
d =
1 2 1

1.4 Evaluation of a Polynomial at Specified Values


 The polyval(p,x) function evaluates a polynomial at some
specified value of the independent variable.

Example:
Evaluate polynomial x2+2x+1 at x = 2.
>> e=[1 2 1]; % defining polynomial
>> f=polyval(e,2) % define polynomial and evaluation value
f =
9

1.5 Multiplication of Polynomials


 The conv(p1,p2) function evaluates the multiplication of
two polynomials p1 and p2.
Example:
Evaluate multiplication of polynomials (x-1) and (x+1).
>> p1= [1 -1];
>> p2= [1 1];
>> m= conv(p1,p2)
m =
1 0 -1

1.6 Compute the Quotient and reminder of two


Polynomials
 The deconv(p1,p2) function evaluates the division of two
polynomials p1 and p2 and finds the quotient and reminder
of two polynomials.

Example: Compute the division of two polynomials p1=x2+2x+1


and p2=x+1.
>> p1= [1 2 1];
>> p2= [1 1];
>> [q,r]=deconv(p1,p2)
q =
1 1
r =
0 0 0

1.7 Compute the derivative of Polynomial


 The polyder(p1) function evaluates the derivative of
polynomials p1.
Example: Find the derivative of polynomial x2+2x+1.
>> p1=[1 2 1];
>> d= polyder(p1)
d =
2 2

1.8 Express numerator and denominator polynomials in


factored form

 The roots(p1) function evaluates the roots of polynomials


p1.

Example:
>> p1=[1 -3 0 5 7 9];
>> p2=[2 0 -8 0 4 10 12];
>> roots_num= roots(p1)
roots_num =
2.4186 + 1.0712i
2.4186 - 1.0712i
-1.1633 + 0.0000i
-0.3370 + 0.9961i
-0.3370 - 0.9961i

>> roots_den= roots(p2)


roots_den =
1.6760 + 0.4922i
1.6760 - 0.4922i
-1.9304 + 0.0000i
-0.2108 + 0.9870i
-0.2108 - 0.9870i
-1.0000 + 0.0000i

1.9 Using MATLAB to Make Plots


 The function plot(X,Y) creates a 2-D line plot of the
data in Y versus the corresponding values in X.
 Commands related to plot function are listed below -

Command Description
xlabel(txt) It labels the x-axis of the
current axes.

 Create Multiline x-axis Label


Create a multiline label using a cell array of character
vectors.
Example:

Plot: See - Fig.A

 Include Greek Letters in x-Axis Label

You might also like