Sie sind auf Seite 1von 15

Course material

Of

Simulation Laboratory

Department of Electronics and Telecommunication Yeshwantrao Chavan College of engineering, Nagpur

Contents
1: Interacting with MATLAB 1.1 Introduction 1.2 Starting & Quitting the MATLAB 1.3 Start Button 1.4 Command Window 1.5 Command History 1.6 Help Browser 1.7 Current Directory Browser & Search Path 1.8 Work Space 1.9 Array Editor 1.10 Variable 1.11 Basic Calculation operators 1.12 Relational Operators 1.13 Logical Operators 1.14 Vectors 1.15 Colon Notations or Range Operator 1.16 Functions 1.17 Graph Plotting 1.18 M-File 1.19 Polynomial

Interacting with MATLAB 1.1 Introduction


The purpose of this tutorial is to present basics of MATLAB. We do not assume any prior knowledge of this package; this tutorial is intended for users running a professional version of MATLAB 7.0.4 MATLAB and its toolboxes MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and visualization. It integrates numerical analysis, matrix computation, signal processing, and graphics in an easy-to-use environment. MATLAB also features a family of application-specific solutions called toolboxes. Toolboxes are comprehensive collections of MATLAB functions that extend its environment in order to solve particular classes of problems. The table below includes the toolboxes that are available in the last version of MATLAB:
Communications Control System Data Acquisition Database Datafeed Filter Design Financial Frequency Domain System Identification Fuzzy Logic Higher-Order Spectral Analysis Image Processing Instrument Control Mapping Neural Network Optimization Partial Differential Equation Robust Control Signal Processing Statistics Spline System Identification Wavelet MATLAB Compiler MATLAB C/C++ Graphics Library MATLAB C/C++ Math Library MATLAB Report Generator MATLAB Runtime Server MATLAB Web Server Simulink Symbolic/Extended Math

MATLAB is an interpreted language for numerical computation. It allows one to perform numerical calculations, and visualize the results without the need for complicated and time consuming programming. MATLAB allows its users to accurately solve problems, produce graphics easily and produce code efficiently. It provides a single platform for computation, visualization, programming and software development. All problems and solutions in MATLAB are expressed in notation used in linear algebra and essentially involve operations using matrices and vectors. MATLAB is widely used in all areas of applied mathematics in education and research at universities and in the industry.

Strengths of MATLAB
MATLAB is relatively easy to learn MATLAB code is optimized to be relatively quick when performing matrix operations MATLAB may behave like a calculator or as a programming language MATLAB is interpreted, errors are easier to fix Although primarily procedural, MATLAB does have some object-oriented elements

Weaknesses of MATLAB
MATLAB is NOT a general purpose programming language MATLAB is an interpreted language (making it for the most part slower than a compiled language such as C++) MATLAB is designed for scientific computation and is not suitable for some things (such as parsing text).

MATLAB can be used to solve problems in


o o o o o

Circuits Communication systems Digital signal processing Control systems Probability and statistics

In addition, you can use MATLAB to build Graphical User Interfaces (GUIs) so that you can develop user-friendly custom software.

1.2 Starting and Quitting the MATLAB


Starting MATLAB To start MATLAB on a Microsoft Windows platform, select the Start -> Programs -> MATLAB 7.0.4 -> MATLAB 7.0.4, or double-click the MATLAB shortcut icon on your Windows desktop. When you start MATLAB, the following MATLAB desktop appears containing tools for managing files, variables and applications associated with MATLAB.

You can change the way your desktop looks by opening, closing, moving, and resizing the tools in it. You can also move tools outside of the desktop or return them back inside the desktop.

Quitting MATLAB To end your MATLAB session, select File > Exit MATLAB in the desktop, or type quit OR exit in the Command Window.

1.3 Start Button


The MATLAB Start button provides easy access to tools, demos, shortcuts, and documentation. Click the Start button to see the options.

1.4 The Command Window


The Command Window is one of the main tools you use to enter data, variables, run MATLAB functions and other M-files, and display results.

Type functions and variables at the MATLAB prompt

MATLAB displays the result

Press the up arrow key to recall a statement you previously typed. Edit the statement as needed and then press Enter to run it.

1.5 MATLAB Help:

Help using Search:

Help through Function:

1.6 Command History


Statements you enter in the Command Window are logged in the Command History. From the Command History, you can view previously run statements, as well as copy and execute selected statements. You can also create an M-file from selected statements

Timestamp marks the start of each session

Select 1 or more lines and right click to copy, evaluate, or create an M-file from the selection

1.7 Help Browser


Use the Help browser to search for and view documentation and demos for all your MathWorks products. The Help browser is an HTML viewer integrated into the MATLAB desktop. To open the Help browser, click the help button in the desktop toolbar.

1.8 Current Directory Browser and Search Path


MATLAB file operations use the current directory and the search path as reference points. Any file you want to run must either be in the current directory or on the search path. Current Directory A quick way to view or change the current directory is by using the current directory field in the desktop toolbar, shown here. To search for, view, open, and make changes to MATLAB related directories and files use the MATLAB Current Directory browser.

1.9 Workspace
The MATLAB workspace consists of the set of variables (named arrays) built up during a MATLAB session and stored in memory. You add variables to the workspace by using functions, running M-files, and loading saved workspaces To view the workspace and information about each variable, use the Workspace browser, or use the functions who and whos.

To delete variables from the workspace, select the variables and select Edit -> Delete. Alternatively, use the clear function. The workspace is not maintained after you end the MATLAB session. To save the workspace to a file that can be read during a later MATLAB session, select File -> Save, or use the save function. This saves the workspace to a binary file called a MAT-file, which has a .mat extension. You can use options to save to different formats. To read in a MAT-file, select File -> Import Data, or use the load function.

1.10 Array Editor


Double-click a variable in the Workspace browser, or use openvar variablename, to see it in the Array Editor. Use the Array Editor to view and edit a visual representation of variables in the workspace.

Change values of array elements

Variable management
Who --- List variables Whos --- List variables with sizes

Workspace management
clear x close(x) clc why --- Clear the value of variable x (e.g. clear all clears all variables) --- Close open windows (e.g. close all closes all figures, etc) --- Clear workspace --- Try it and see type why with no arguments

MATLAB is case sensitive: - a and A are different. If this proves to be an annoyance, the command casesen will toggle the case sensitivity off and on. Use a ; at the end of the line to stop commands from echoing to the screen Use keys to scroll through previously entered commands and rerun. continues a command to the next line. Help will tell you how to use a function and what it does. (e.g. >> help plot) % :-Comment operator Use in front (e.g. % Comment this line)

1.11 Variables
Variable = Expression Or Expression Variable Names are Case Sensitive! Expressions: Variables Variable names consist of a letter, followed by any number of letters, digits, or underscores. Only the first 31 characters of a variable name are used. To view the matrix assigned to any variable, simply enter the variable name. To view all variables currently assigned, type who. To view with sizes, type whos. Variables in MATLAB are just like variables in any other programming language (C, C++ etc.); only difference is that you do not have to define them by indicating the type etc. Also, variable names (case sensitive) can be used to refer to a single number (a scalar), a set of numbers (a vector) or an array of numbers (a matrix). Solving equations using variables MATLAB is an expression language Expressions typed by the user are interpreted and evaluated by the MATLAB system Variables are names used to store values Variable names allow stored values to be retrieved for calculations or permanently saved

1.12 Basic Calculation (Arithmetic) Operators:


+ Addition - Subtraction * Multiplication / Division ^ Exponentiation Ex:>> x=2 >>x+1

>> y=4 >>y=2*x; >> x+y >> x*y >> x/y >> x^y

Example: Compute 5 sin (2.53-pi) +1/75. In MATLAB this is done by simply typing 5*sin (2.5^ (3-pi)) +1/75 at the prompt. Be careful with parenthesis and don't forget to type * whenever you multiply! Try 1) a=5*(2/3) +3^2 result is shown 2) a=2/4 + 4\2 ; result is not shown 3) a value of a is shown 4) x=3.5*sin (2.9) 5) (1+4)*3 6) t = 1 + 2 + 3 + ... 4 + 5 + 6 % ... continues a line 7) sin (pi/2) 8) 1/0 9) 3*z ??? Undefined function or variable z. Observe that variables must have values before they can be used. When an expression returns a single result that is not assigned to a variable, this result is assigned to ans, which can then be used like any other variable. >> 2^4 ans = 16 >> pi/ans ans = 0.1963 10) x^4 + 2*x^3 + 2*x^2 + 4*x + 1 11) f(x) = x2 + (1 - exp(x))2-4 12) f = 1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 ... + 1/7 + 1/8 13) y=[0 1/4 1/2 3/4 1] 14) y=pi*y; sin(y)

1.13 Relational operators


Relational operators are used to compare two scalar values or matrices of equal dimensions < Less than <= Less than or equal to > Greater than >= Greater than or equal to == Equal ~= Not equal A 1 or 0 is returned for each comparison indicating TRUE or FALSE >> 5 == 5 Ans 1 >> 20 <= 15 Ans 0 Try this Example: 1)3<4 2)3.5 == 3.5 3)10<= 7

1.14 Logical Operators


In this section you should think of 1 as "true" and 0 as "false." The notations &, |, ~ stand for "and,""or," and "not," respectively. ~a a&b a & ~a a|b a | ~a

1.15 Vectors
Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between brackets, and set it equal to a variable. Vectors are nothing but matrices having a single row (a row vector), or a single column (a column vector). For example, to create the vector a, enter into the MATLAB command window To create a row vector in MATLAB, do: >> r = [1 2 3 4] r= 1 2 3 4 A column vector can be created by >> c = [1; 2; 3; 4] c= 1 2 3 4 Manipulating vectors is almost as easy as creating them. First, suppose you would like to add 2 to each of the elements in vector 'r'. The equation for that looks like: >>b = r + 2

b= 3 4 5 6 Now suppose, you would like to add two vectors together. If the two vectors are the same length, it is easy. Simply add the two as shown below: >>c = r + b c= 4 6 8 10 Subtraction of vectors of the same length works exactly the same way. Try it.

1.16 Colon Notation OR Range operator ":"


The range operator allows one to set up a vector of equally spaced entries. Defining vectors with the range operator >> x = 1:4 x= 1 2 3 4 Vectors can also be created by incrementing a starting value with a constant quantity. For example, >> x = 1:2:6 x= 1 3 5 >> r = [0:2:10] r= 0 2 4 6 8 10 Creates a row vector, with the first element = 0; each element incremented by 2; until the final value of 10. You can index specific parts of a vector. For example, to get the third element in the vector r, you can do >>r(3) Let's say you want to create a vector with elements between 0 and 20 evenly spaced in increments of 2 (this method is frequently used to create a time vector): >>t = 0:2:20 t= 0 2 4 6 8 10 12 14 16 18 20

1.17 Functions
MATLAB contains all of the standard functions such as abs, sin, cos, log, exp, sqrt, as well as many others. Commonly used constants such as pi, and i or j for the square root of -1, are also incorporated into MATLAB. Ex.>> exp(i*pi) >>sin(pi/4) >>B=sqrt(x) >>z=x+i*y >> log(-1) To determine the usage of any function, type help [function name] at the MATLAB command window. MATLAB even allows you to write your own functions with the function command.

1.18 Graph plotting


It is also easy to create plots in MATLAB. Suppose you wanted to plot a sine wave as a function of time. First make a time vector (the semicolon after each statement tells MATLAB we don't want to see all the values) and then compute the sin value at each time. Examples 1) Graph the sine function over the domain x x = -pi:0.01:pi; plot(x,sin(x)), grid on 2) t = 0:pi/100:2*pi; y1=sin(t); y2=sin(t+pi/2); plot(t,y1,t,y2) 3) x = -pi:0.01:pi; y1 = sin(x); y2 = cos(x); plot(x,y1,x,y2)

1.19 M-file
What is an m-file? An m-file is a simple text file where you can place MATLAB commands. When the file is run, MATLAB reads the commands and executes them exactly as it would if you had typed each command sequentially at the MATLAB prompt. All m-file names must end with the extension '.m' (e.g. plot.m). If you create a new m-file with the same name as an existing m-file, MATLAB will choose the one which appears first in the path order (help path for more information). To make life easier, choose a name for your m-file which doesn't already exist. To see if a filename.m exists, type help filename at the MATLAB prompt. Why use m-files? For simple problems, entering your requests at the MATLAB prompt is fast and efficient. However, as the number of commands increases or trial and error is done by changing certain variables or values, typing the commands over and over at the MATLAB prompt becomes tedious. M-files will be helpful and almost necessary in these cases. How to create, save or open an m-file? To create an m-file, choose New from the File menu and select m-file. This procedure brings up a text editor window in which you can enter MATLAB commands. To save the m-file, simply go to the File menu and choose Save (remember to save it with the '.m' extension). To open an existing m-file, go to the File menu and choose Open . How to run the m-file? After the m-file is saved with the name filename.m in the MATLAB folder or directory, you can execute the commands in the m-file by simply typing filename at the MATLAB prompt. If you don't want to run the whole m-file, you can just copy the part of m-file that you want to run and paste it at the MATLAB prompt.

1.20 Polynomials
In MATLAB, a polynomial is represented by a vector. To create a polynomial in MATLAB, simply enter each coefficient of the polynomial into the vector in descending order. For instance, let's say you have the following polynomial:

To enter this into MATLAB, just enter it as a vector in the following manner x = [1 3 -15 -2 9] MATLAB can interpret a vector of length n+1 as an nth order polynomial. Thus, if your polynomial is missing any coefficients, you must enter zeros in the appropriate place in the vector. For example,

would be represented in MATLAB as: y = [1 0 0 0 1] You can find the value of a polynomial using the polyval function. For example, to find the value of the above polynomial at s=2, z = polyval([1 0 0 0 1],2) You can also extract the roots of a polynomial. This is useful when you have a high-order polynomial such as

Finding the roots would be as easy as entering the following command; roots([1 3 -15 -2 9]) ans = -5.5745 2.5836 -0.7951 0.7860

Das könnte Ihnen auch gefallen