Introduction to Matlab

10
INTRODUCTION TO MATLAB MATLAB stands for MATrix LABoratory because its basic data element is a matrix (array). It is widely used for mathematical computation, modeling, simulations, visualization and graphics, and algorithm development. The MATLAB has optional toolboxes that are specially designed to solve specific problems. i.e toolboxes for signal processing, robotics and control systems. Literature that has been written about MATLAB assumes that the reader has knowledge of computer programming. 1.1 MATLAB WINDOWS When the program starts, the MATLAB desktop window opens, the window contain four smaller windows: the command window, the current folder Window, the Workspace Window and the Command History Window known as default view of tool. The list of different windows their purposes are given in Table 1-1. Window Purpose Command Window Main window, enters variable, runs programs. Figure Window Contain Output from graphic command. Editor Window Create s and debug script and Function files. Help Window Provides help related to MATLAB tool. Workspace Window Provides information about the variables that are used. Current Folder Window Shows the file in the current folder. Command History Window Logs commands entered in the command window. Table 1-1: MATLAB windows 1.2 WORKING IN THE COMMAND WINDOW 1

description

Matlab is a universal tool for all technical personals especially engineers. This document explains the basic

Transcript of Introduction to Matlab

INTRODUCTION TO MATLABMATLAB stands for MATrix LABoratory because its basic data element is a matrix (array). It is widely used for mathematical computation, modeling, simulations, visualization and graphics, and algorithm development. The MATLAB has optional toolboxes that are specially designed to solve specific problems. i.e toolboxes for signal processing, robotics and control systems. Literature that has been written about MATLAB assumes that the reader has knowledge of computer programming.1.1 MATLAB WINDOWS When the program starts, the MATLAB desktop window opens, the window contain four smaller windows: the command window, the current folder Window, the Workspace Window and the Command History Window known as default view of tool. The list of different windows their purposes are given in Table 1-1.WindowPurpose

Command WindowMain window, enters variable, runs programs.

Figure WindowContain Output from graphic command.

Editor Window Create s and debug script and Function files.

Help WindowProvides help related to MATLAB tool.

Workspace WindowProvides information about the variables that are used.

Current Folder WindowShows the file in the current folder.

Command History Window Logs commands entered in the command window.

Table 1-1: MATLAB windows1.2 WORKING IN THE COMMAND WINDOWIt is the main window and it can be used for executing commands, opening other windows, running programs written by the user, and managing the software. An example of command window with several simple commands that will be explained later can be shown in the figure 1-1. 1.3 NOTES FOR WORKING IN THE COMMAND WINDOWa) To type a command the cursor must be placed next to the command prompt (>>).b) After entering the command, the Enter key is pressed, the command is executed.c) Several commands can be typed in same line by simply placing comma in between them. When Enter key is pressed the commands are executed in order from left to right.d) It is not possible to go back to a previous line that is displayed in command window, make a correction and re-execute it.e) A previously typed command can be recalld to the command promt with the up arrow () and the down arrow () can be used to move down the list of previously typed commands.f) If a command is too long to fit in one line , it can be continued to the next line by typing three periods .(called an ellipsis) and pressing the Enter Key.

Figure1-1: The command WindowThe semicolon ( ; ):When a command is typed in command window and Enter key is pressed , the command is executed and output can be seen in command window. If a semicolon is inserted at the end of the command, the command will be executed but the output will not be displayed on screen.Typing ( % ) :When % symbol is entered in the beginning of the command, It will be considered as a comment and will not be executed.The ( clc ) Command:The clc command clears the command window. The Assignment operator ( = ) :the left hand side of the operator can be include only one variable name and the right hand side of the operator can be a number, a variable and a complete expression which can compute the result and store in it. When Enter key is pressed the right hand value is stored in the left hand side variable.1.4 ARITHMETIC OPERATIONS WITH SCALARSNumbers can be used in arithmetic calculations directly or they can be assigned to variables which can subsequently be sued in calculation. The symbols of arithmetic operations are: 1.5 ORDER OF PRECEDENCEMATLAB executes the calculations according to the order of precedence displayed below. The higher precedence operations are executed before the lower. If two or more have same precedence, the expression is executed from left to right. 1.6 ELEMENTARY MATH BUILT-IN FUNCTIONSMATLAB has a very large library of built-in functions. A function has a name and an argument in parentheses. Example sqrt(x) its name is sqrt and argument is x.

FunctionDescriptionExample

sqrt ( x )Square root.>>sqrt( 81 ) ans=9

nthroot (x,n)Real nth root of a real number x>>nthroot (80,5) ans= 2.4022

exp(x)Exponential >>exp(5) ans=148.4132

abs(x)Absolute value>>abs(-24) ans=24

log(x)Natural logarithm (ln)>>log(1000) ans=6.9078

log10(x)Base 10 logarithm>>log10(1000) ans=3.0000

factorial(x)X must be a positive number>>factorial(5) ans=120

sin(x)Sine of angle x in radians>>sin(pi/6) ans=0.5

sind(x)Sine of angle x in degrees

cos(x)cos of angle x in radians

cosd(x)cos of angle x in degrees>>cosd(30) ans=0.86

tan(x)tan of angle x in radians>>tan(pi/6) ans=0.5774

tand(x)tan of angle x in degrees

Table 1-2: Built-in Function of MATLABThe inverse trigonometric functions are asin(x), acos(x), atan(x), acot(x) for the angle in radians; asind(x), acosd(x), atand(x), for angles in degrees the hyperbolic trigonometric functions are sinh(x), cosh(x), tanh(x) and coth(x).Function DescriptionExample

round(x)Round to the nearest integer>>round(17/5) ans= 3

fix(x)Round toward 0>>fix(13/5) ans=2

ceil(x)Round toward infinity>>ceil(13/5) ans=3

floor(x)Round toward minus infinity>>floor(-9/4) ans=-3

rem(x,y)Remainder when x divided by y>>rem(13,5) ans=3

Table 1-3: Rounding functions1.7 DEFINING A SCALAR VARIABLEIt is a name of a memory location, when a new variable is defined; MATLAB allocates an appropriate memory space where the data is stored. When a variable is assigned a new value the content of the location is replaced.Variable-name = A numerical value, or a computable expression

1.7.1 RULES ABOUT VARIABLE NAMES A variable name must begin with a letter.it can contain letters, digits and underscores. It cannot contain punctuations letter i.e ( , . : ;) . No spaces are allowed between characters. Avoid using name of built in function i.e ( void , sprt, exp, pi). Once a function name is used as a variable, the function can be executed.1.7.2 FREQUENTLY USED VARIABLESansIt has the value of last expression.

piIt contains the number .

epsIt contains a value 2^(-52).

infIt contains infinity value.

iDefine as a

jDefine as a

1.7.3 USEFUL COMMANDS FOR MANAGING VARIABLESClear Remove all the variables from memory

Clear a b cRemove only a b c from the memory

whoDisplays the current variable list defined in memory

whosDisplays the current variable with complete detail

1.8 SCRIPT FILESThe commands in command window cannot be executed and saved again. Every time the Enter key is pressed only the last command is executed and everything executed before is unchanged.A different way of executing the commands is first to create a file with a list of commands, save it and then run the file. The commands will be executed in order from top to bottom. If needed command can be corrected or changed.

1.8.1 NOTES ABOUT SCRIPT FILES a. When a script file has a command which generates an output, the output is displayed in command window.b. It is sequences of commands thats why also known as program.c. It can be edited and executed many times.d. It is also known as M-files and the extension .m is used to save files.e. It can be typed and edited in any text editor and then pasted into MATLAB editor.

1.8.2 CREATING AND SAVING A SCRIPT FILEThis window is opened from command window. In the file menu, select New and then the Script. An open editor window is shown in the figure below.

The command can also be typed in word and then copy paste in Editor. MATLAB automatically number a new line when Enter key is pressed while writing command in Editor. A simple program entered in Editor. The first few lines in editor are typically used for comments by placing first character % in the start of it.

Before a script file can be executed it has to be saved. This is done by choosing Save As from the file menu, selecting the location and entering the name of the file. While saving add the extension .m . The rules for naming a script file are same as it is for naming a variable.LAB ASSIGNEMENTS1.

2.

3.

4.

5.

6

7.

1