Survey

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Transcript

Numerical Analysis Lab Introduction To Matlab LAB 1 INTRODUCTION TO MATLAB-1 1.0 OBJECTIVE 2.0 To understand basic function of MATLAB, To write inline functions INTRODUCTION MATLAB is a high-performance language for technical computing. It integrates computation, visualization, and programming in an easy-to-use environment where problems and solutions are expressed in familiar mathematical notation. Typical uses include Math and computation Algorithm development Data acquisition Modeling, simulation, and prototyping Data analysis, exploration, and visualization Scientific and engineering graphics Application development, including graphical user interface building MATLAB is an interactive system whose basic data element is an array that does not require dimensioning. This allows you to solve many technical computing problems, especially those with matrix and vector formulations, in a fraction of the time it would take to write a program in a scalar noninteractive language such as C or Fortran. The name MATLAB stands for matrix laboratory. MATLAB was originally written to provide easy access to matrix software developed by the LINPACK and EISPACK projects. Today, MATLAB engines incorporate the LAPACK and BLAS libraries, embedding the state of the art in software for matrix computation. MATLAB features a family of add-on application-specific solutions called toolboxes. Very important to most users of MATLAB, toolboxes allow you to learn and apply specialized technology. Toolboxes are comprehensive collections of MATLAB functions (M-files) that extend the MATLAB environment to solve particular classes of problems. Areas in which toolboxes are available include signal processing, control systems, neural networks, fuzzy logic, wavelets, simulation, and many others. 1 Numerical Analysis Lab Introduction To Matlab a) Getting Started With MATLAB Starting MATLAB On Windows platforms, start MATLAB by double-clicking the MATLAB shortcut icon on your Windows desktop. If you have worked in MATLAB before and have an M-file that was written by MATLAB, you can also double-click on the file to lunch MATLAB MATLAB windows On almost all systems, MATLAB works through three basic windows. Command window This is the main window. It is characterized by the MATLAB command prompt ‘>>’. When you launch the application program, MATLAB puts you in this window. All commands, including those for running user-written program, are typed in this window at the MATLAB prompt. 2 Numerical Analysis Lab Introduction To Matlab Lunch Pad This subwindow lists all MATLAB related applications and toolboxes that are installed on your machine. You can launch any of the listed applications by double clicking on them. Workspace This subwindow lists all variables that you have generated so far and shows their type and size. You can do various things with these variables, such as plotting, by clicking on a variable and then using the right button on the mouse to select your option. Command History All commands typed on the MATLAB prompt in the command window get recorded, even across multiple sessions. You can select a command from this window with the mouse and execute it in the command window by double clicking on it. You can also select a set of commands from this window and create an M-files with right click of the mouse. Current History This is where all your files from the current directory are listed. You can do file navigation here. You also have several options. To see the options, click the right button of the mouse after selecting a file. You can run M-files, rename them, delete them, etc. Graphics window The outputs of all graphics command typed in command window are flushed to the graphics or Figure window. The user can create as many figure windows as the system memory will allow. 3 Numerical Analysis Lab Introduction To Matlab Edit window This is where you write, edit, create, and save your own programs in files ‘M-files’. 4 Numerical Analysis Lab Introduction To Matlab b) Basic Functions of MATLAB General function you should remember who whos save load clear pack size length disp clc home format echo more Managing Variables and the Workspace list current variables list current variables, long form save workspace variables to disk retrieve variables from disk clear variables and functions from memory consolidate workspace memory size of matrix length of vector display matrix or text Controlling the Command Window clear command window send cursor home|to top of screen set output format echo commands inside script commands control paged output in command window Matrix Operators + − * ^ / \ ‘ .' Array Operators addition subtraction multiplication power right division left division conjugate transpose transpose 5 + − .* .^ ./ .\ addition subtraction multiplication power right division left division Numerical Analysis Lab Introduction To Matlab Relational and Logical Operators < less than <= less than or equal > greater than >= greater than or equal == ~= equal not equal Special Characters = [] () . .. ... , ; % : assignment statement used to form vectors and matrices; enclose multiple function output variables arithmetic expression precedence; enclose function input variables decimal point parent directory continue statement to next line separate subscripts, function arguments, statements end rows, suppress printing comments subscripting, vector generation Example 1 Compute the following quantities 1 25 and (1 5 ) 1 5 2 2 1 >> 2^5/(2^5-1) ans = 1.0323 3 5 1 ( 5 1) 2 1 >> 3*(sqrt(5)-1)/(sqrt(5)+1)^2-1 ans = -0.6459 6 Numerical Analysis Lab Introduction To Matlab Example 2 Create a vector A containing the integers 1 through 5 >> A=1:5 Create a vector A [1.0 1.1 1.2 1.3 1.4] >> A=1:0.1:1.4 Example 3 Colon operator >> c=[1 2 3; 4 5 6; 7 8 9]; >> y=c(:,3) y= >> z=c(2,:) >> x=c(:,2:3) z= x= 4 3 6 9 5 2 5 8 6 3 6 9 Example 4 Element by element operation matrices >> a=[1 2 3 4]; >> b=[4 3 2 1]; >> c=a+b >> e=a.*b e= c= 5 4 5 5 6 >> f=a./b d= f= -1 1 4 5 >> d=a-b -3 6 3 0.2500 7 0.6667 1.5000 4.0000 Numerical Analysis Lab Introduction To Matlab c) Creating and Using Inline Function A mathematical function, such as F(x) or F(x,y), usually requires just values of the independent variables for computing the value of the function. We frequently need to evaluate such functions in scientific calculations. However, there is a quicker way of programming function is they are not too complicated. This is done by defining inline functions – functions that are created on the command line. We define these functions using the built-in function inline. The syntax for creating an inline-function is particularly simple: F=inline (‘function formula’) Thus, a function such as F(x) = x2sin(x) can be coded as F = inline(‘x^2*sin(x)’) For more examples and usage of inline functions we can as below: Example 1 Compute the following quantities f ( x) x 2 sin x where x = π 8 Numerical Analysis Lab Introduction To Matlab where x = 0, π/4, π/2, 3π/4, π 9