Matlab Introduction by FFB6Vf


									                                   Matlab Introduction

This lecture notes deal with Matlab version 6.5 on windows environment. It should be backward
compatible with older versions. Matlab is a registered trademark of MathWorks, Inc.
Let us introduce Matlab in windows environment. Its front-end (Graphical user interface) is very
easy to use and is user friendly. Further it is compatible with standard windows applications and
1) File operations
2) Multiple windows view.
3) help, demos and example (Check whether this component is installed)
4) Wizard to create a GUI (graphical user interface)
5) Wizard to profile code
6) Toolboxes for different components like communication, control systems, data acquisition,
    curve fitting, fuzzy logic, neural network etc. (These components may or may not be
    available, depending upon the installation)
7) simulator libraries (Not to be discussed now)
When matlab application is started, it looks like figure 1.
Figure 1: Matlab command window

                                              CURRENT DIRECTORY

                                               COMMAND               COMMAND
    TOOLBOX                                  1 HISTORY               WINDOW
    MENU                WINDOW &               WINDOW
                        CURRENT DIR
                        WINDOW WITH 2
                        TAB VIEWS (current
WINDOW                                               PURPOSE
Command Window                                       To enter commands
Command History Window                               To see previous commands
Workspace window                                     Provide information about the variables that
                                                     are used
Current Directory Window                             Shows the files in current directory
Launch pad window (Toolbox Menu)                     Provide access to tools and demos
Table 1: Description of common windows

There are other windows of matlab, which are equally important. Table 2 describes them in brief
and will be introduced later.
WINDOW                                               PURPOSE
Help window                                          Provides Help information
Editor window                                        Text editor to write programs
Figure window                                        Output of Graphical commands
Table 2: Description of other windows

Exercise 1
To use Matlab as a calculator
Matlab can be used as a calculator. In the command window start typing simple examples
The following can be observed -:
1) To type a command cursor must be placed next to command prompt, (>>).
2) Several commands can be typed in the same line with comma separating the commands. If
comma is missed then only the last command is executed
3) Semicolon (;) is used to suppress the output (No output is echoed on the screen)
4) Percentage (%) is used as comments
5) clc is used for clearing screen (It is same as cls in windows command prompt or clear in unix
6) Addition (+), Subtraction (-), multiplication (*), exponent (^), Division --- right & left is
supported. Normal division is known as right division (/) ex 4/2 =2. Left division (\) ex 4\2= 0.5
7) If a command is too long to fit in one line, it can be continued by typing three periods (…)
called ellipsis and pressing the enter key
8) Numerous display formats are supported. They are invoked by typing format xxxx, on the
    command prompt, where xxxx can stand for numerous things. More information can be
    obtained by typing help format on the command prompt. The various display formats are
    shown in table 3. Please fill in the description column of Table 3 by practically carrying
    out the examples.

COMMAND                           DESCRIPTION                       EXAMPLE
format short                                                        >>290/7
                                                                    ans = 41.4286
format long                                                         >>290/7
                                                                    ans = 41.42857142857143
format short e                                                      >>290/7
                                                                    ans = 4.1429e+001
format long e                                                       >>290/7
                                                                    ans =
format short g                                                      >>290/7
                                                                    ans = 41.429
format long g                                                       >>290/7
                                                                    ans =41.4285714285714
format bank                                                         >>290/7
                                                                    ans = 41.43
format compact
format loose
Table 3 : Output format options

9) Carry out the exercise in table 4 to use matlab as a calculator, use format long -:
COMMAND                                           ANSWER
>>7 + 8/2
>>4 + 5/3 +2
>>27^(1/3) + 32^0.2
>>27^1/3 + 32^0.2
>>0.7854 -(0.7854)^3/(1*2*3) + ...
0.785^5/(1*2*3*4*5) - …
Table 4: Matlab as calculator
10) Has predefined Mathematical symbols. pi, inf, i; defined for ,  , square root of (-1).
11) Elementary Math Build in functions, table 5 . Carry out the examples and fill in the answers
use format short -:
FUNCTION            DESCRIPTION                    EXAMPLE
sqrt(x)             Square root                    >>sqrt(82)
                                                   ans =
exp(x)              Exponential (e )               >>exp(5)
                                                   ans =
abs(x)              Absolute Value                 >>abs(-24)
                                                     ans =
log(x)             Natural Logarithm. Base e         >>log(1000)
                   logarithm (ln)                    ans =
log10(x)           Base 10 logarithm                 >>log10(1000)
                                                     ans =
factorial(x)       The Factorial function, x must    >>factorial(5)
                   be positive                       ans =
sin(x)             Sine of an angle x, x in          >>sin(pi/6)
                   radians                           ans =
cos(x)             Cosine of an angle x, x in        >>cos(pi/6)
                   radians                           ans =
tan(x)             Tangent of an angle x in          >>tan(pi/6)
                   radians                           ans =
cot(x)             Cotangent of an angle x in        >>cot(pi/6)
                   radians                           ans =
round(x)           Round to nearest integer          >>round(17/5)
                                                     ans =
fix(x)             Round towards zero                >>fix(13/5)
                                                     ans =
ceil(x)            Round towards infinity            >>ceil(11/5)
                                                     ans =
floor(x)           Round towards minus infinity      >>floor(-9/4)
                                                     ans =
rem(x,y)           Returns the remainder after x     >>rem(13,5)
                   is divided by y                   ans =
sign(x)            Signum function. Returns 1 if     >>sign(5)
                   x>0, -1 if x <0 and 0 if x =0.    ans =
Table 5 : Elemenatry Mathematical functions
In addition to the above mentioned functions matlab supports inverse trigonometric functions
[asin(x), acos(x), atan(x) and acot(x)] and hyperbolic trigonometric functions [sinh(x), cosh(x),
tanh(x), coth(x)].

Defining Scalar Variables
In Matlab, a variable can be declared using a variable name. The syntax for declaring a scalar
variable is

variable_name = A numerical value, or a computable expression, or a previously defined
                                                                        variable name.
>>x =15

Rules about Variable Names -:
1) Can be 63 characters long (matlab version 6.5, version 6.0 allows for 31 characters)
2) Can contain letters, digits and underscore characters.
3) Must begin with a letter.
4) Should not be defined to predefined mathematical symbols (pi, inf, i, j , Nan, ans ,eps). One
   should avoid names of built-in functions.

Useful commands for managing variables.
Some useful commands for managing variables are mentioned in table 6
clear             Remove all variables from the memory.
clear x y z       Remove only variables x, y, z from the memory
who               Display a list of the variables currently in the memory
whos              Display a list of the variables currently in the memory and their size
                  together with information about bytes and class.


To top