CE890 / ENE801 Lecture 1 Introduction to MATLAB
CE890: Course Objectives Become familiar with a powerful tool for computations and visualization (MATLAB) Promote problem-solving skills using computers Learn programming skills Learn to do numerical analysis using MATLAB
Textbook Essential MATLAB for Engineers & Scientists By Brian Hahn & Daniel Valentine 3 rd Edition (2007) Elsevier
Grading Homework assignments (4): 60% of final grade Final Project: 40% of final grade There will be one homework assignment every week DECS computer labs are reserved for hands-on sessions
Syllabus Introduction to MATLAB: Variables, constants, m-files (scripts and functions), operators, control flow, strings, cell arrays, y, built-in functions, help and matlab documentation MATLAB Graphics: XY plots (line, scatter, bar, probability and polar plots), two-dimensional graphs including contour plots, filled contour plots, quiver or vector plots, stream function plots; working with images; three-dimensional plots:iso- surfaces, adjusting light, shading and camera positions, creating movies / animations, reading data into MATLAB, exporting plots and animations in various formats Numerical Analysis: Root finding, Newton - Raphson method; Interpolation (one- and multidimensional interpolation; splines); Numerical integration: Simpson's s rule, Gaussian quadrature; Numerical Linear Algebra: Vectors and matrices in MATLAB; determinants and inverse of a matrix; Solving systems of linear equations; eienvalues, LU decomposition; singular value decomposition; sparse matrices; connection between graphics and linear algebra; Solution of ordinary differential equations: Runge - Kutta method; Two-point boundary value problems; Systems of Differential Equations; Algorithms for Stiff Differential Equations Toolboxes and Advanced Programming Techniques in MATLAB: Vector versus scalar computation; Memory management; MEX-files and Dynamic Link Libraries (DLLs); The MATLAB compiler; Toolboxes; How to create your own toolbox?
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
MATLAB: Matrix Lab Images, movies, sound and variables all are matrices in MATLAB Sound: load chirp sound(y) Image: load clown image(x), colormap(map) Animations (movies): z = peaks; surf(z); axis tight set(gca,'nextplot','replacechildren'); for j = 1:20 end movie(f,50) surf(sin(2*pi*j/20)*z,z) f(j) = getframe;
MATLAB is both a Software tool and a computer language
Matlab Desktop Launch Pad Command Window History
Matlab Help
Command Window The Command Window on the right is the main panel where you interact t with MATLAB. You key (or type) and <Enter> commands after the prompt >>; MATLAB executes the commands and displays results (if requested). Some commonly used tools and commands: (up arrow) returns last command input, can be repeated clc clears the screen whos shows list of variables clear clears variables
Evaluation of MATLAB HANDS ON with MATLAB Type >> 2+3 <Enter> into the Command Window >> clc <Enter> >> whos <Enter> Throughout h the lecture, red text indicates what you should type into MATLAB.
Command History Window The Command History Window logs all of the commands you enter in MATLAB. It should have logged 2+3. Use the Command History Window to reenter 2+3 in the command window (use copy-and- ) paste or double click on 2+3). This is useful to retrieve past commands. Use Shift key to select multiple lines.
Arithmetic with MATLAB Let us explore by doing exercises: >> 3 2 <Enter> >> 3*2 <Enter> >> 3/2 <Enter> >> 3\2 <Enter> >> 3^2 <Enter> >> 2/0 <Enter> >> 0/2 <Enter> >> 3*Inf <Enter>
Algebraic numeric computations Let us explore by doing exercises: >> a = 3 <Enter> >> b = 2 <Enter> >> a b <Enter> >> a / b <Enter> >> a^2 <Enter> >> c = a * b <Enter> >> d = c^(b+1) <Enter> >> who
Hiding Output Let us explore by doing exercises: >> clear; clc <Enter> >> whos <Enter> >> a = 3; <Enter> >> b = 2; <Enter> >> c = a * b; <Enter> >> d = c^(b+1); <Enter> >> who <Enter> >> % a, b, c, d are in workspace<enter> >> a, b, c, d <Enter>
Plot y versus x Introduction to plotting & displaying data: >> clear; clc <Enter> >> x = 0:0.1:1; <Enter> >> y = x.^2; <Enter> >> whos <Enter> >> plot(x,y,x,y, o ) <Enter> >> disp(' '),disp('... x... y...'),disp([x y']) <Enter> >> x <Enter> >> y <Enter> >> % x and y are 1-by-11 arrays of numbers!
Write a Simple Program Consider computing the volume of a cone: Volume = (pi.*r.^2.*h)./3 radius = 6 inches height = 12 inches In the command window key in: >> clear; clc <Enter> >> r = 6 <Enter> >> h = 12 <Enter> >> v = (pi.*r.^2.*h)./3 <Enter> >> whos <Enter>
Editor & M-Files An M-file in MATLAB is analogous to a txtfile in Microsoft Notepad. An M-file is created in MATLAB text editor. M-files: You can save your programs (i.e., list of executable commands) as M-files. You can reopen and modify your program. They are useful for debugging (correcting errors) as you develop your programs (your technical computing tools).
Comments in programs Every time you write a program to be saved, it is helpful for you to comment (i.e., describe) it well. To insert a comment on a line in the editor or in the Command Window, use the comment operator %, then type your comment. MATLAB: will not run lines that begin with the comment operator (in the editor comments appear in green). Comments Comments allow you (and others) to more easily understand your program. When your lines of code are easy to understand, your code will be easier to use later.
Artofwell-written written code A well-written program is like literature; it contains comments explaining: what your program requires as input. what the variables in the program represent. what your program computes and displays. It is useful for you to add a set of header comments that include the name of the program, your name (as the programmer), and the date the program was created or modified.
Saving code in an M-File Open the editor by: Entering the command edit in the command window. Or click the white-sheet-of-paper icon in the upper left hand corner directly below file. Now enter the lines of code to find the volume of a cone: rr = 4 h = 12 v = (pi.*r.^2.*h)./3 REMARK: If you save it, add header comments and comments explaining what the program does. After you have typed in the code, save it as cone.m.
Execute an M-file as a Command Now execute (or run) the program by pushing F5, or by typing on the command line >> cone <Enter> or by clicking the run button. (Note that the run button looks like a page with a down arrow to its left. It can be found below help on the toolbar of the edit window.) If you entered the code as written on the previous slide you will get an error! What went wrong? Repair your program (Change rr = 4 to r = 4.), save it, and run it again. Now change the height to 24, save and run your program again.
Summary MATLAB can be used like a hand calculator to do arithmetic. You can define (or assign) variables with numbers and expressions to do calculations l as illustrated by the volume-of-cone example. The advantage of saving programs as M-files is that you open it, make changes and/or execute it again without having to type it all over again. This concludes our overview of MATLAB and a taste of things to come!
Reading Assignment CHAPTERS 1 & 2