Beruflich Dokumente
Kultur Dokumente
Of
Simulation Laboratory
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
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).
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.
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.
Press the up arrow key to recall a statement you previously typed. Edit the statement as needed and then press Enter to run it.
Select 1 or more lines and right click to copy, evaluate, or create an M-file from the selection
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.
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
>> 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.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.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.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