Introduction to MATLAB

Similar documents
The MATLAB system The MATLAB system consists of five main parts:

Using Matlab in the Chemical Engineering Curriculum at the University of Tennessee. A Workshop presented by David Keffer August 17, 2001

MATLAB. Miran H. S. Mohammed. Lecture 1

Lecture 1: What is MATLAB?

2.0 MATLAB Fundamentals

Introduction to MATLAB for CSE390

Fundamentals of MATLAB Usage

MATLAB is a multi-paradigm numerical computing environment fourth-generation programming language. A proprietary programming language developed by

System Design S.CS301

ENGR 253 LAB #1 - MATLAB Introduction

Chapter 1 Introduction to MATLAB

Introduction to Matlab

INTRODUCTION TO MATLAB, SIMULINK, AND THE COMMUNICATION TOOLBOX

MATLAB = MATrix LABoratory. Interactive system. Basic data element is an array that does not require dimensioning.

MatLab Just a beginning

Introduction to Unix and Matlab

Introduction to MATLAB

An Introduction to MATLAB See Chapter 1 of Gilat

MATLAB Tutorial EE351M DSP. Created: Thursday Jan 25, 2007 Rayyan Jaber. Modified by: Kitaek Bae. Outline

Introduction to MatLab. Introduction to MatLab K. Craig 1

AMATH 352: MATLAB Tutorial written by Peter Blossey Department of Applied Mathematics University of Washington Seattle, WA

Lab of COMP 406. MATLAB: Quick Start. Lab tutor : Gene Yu Zhao Mailbox: or Lab 1: 11th Sep, 2013

How to Use MATLAB. What is MATLAB. Getting Started. Online Help. General Purpose Commands

ELEC4042 Signal Processing 2 MATLAB Review (prepared by A/Prof Ambikairajah)

Starting Matlab. MATLAB Laboratory 09/09/10 Lecture. Command Window. Drives/Directories. Go to.

Eng Marine Production Management. Introduction to Matlab

Creates a 1 X 1 matrix (scalar) with a value of 1 in the column 1, row 1 position and prints the matrix aaa in the command window.

Getting Started with MATLAB

Introduction to MATLAB

Colorado State University Department of Mechanical Engineering. MECH Laboratory Exercise #1 Introduction to MATLAB

A/D Converter. Sampling. Figure 1.1: Block Diagram of a DSP System

A Guide to Using Some Basic MATLAB Functions

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

Fall 2015 Math 337. Basic MatLab

Introduction to MATLAB

Learning from Data Introduction to Matlab

Table of Contents. Introduction.*.. 7. Part /: Getting Started With MATLAB 5. Chapter 1: Introducing MATLAB and Its Many Uses 7

HERIOT-WATT UNIVERSITY DEPARTMENT OF COMPUTING AND ELECTRICAL ENGINEERING. B35SD2 Matlab tutorial 1 MATLAB BASICS

University of Alberta

Introduction to MATLAB

MATLAB The first steps. Edited by Péter Vass

3 An Introductory Demonstration Execute the following command to view a quick introduction to Matlab. >> intro (Use your mouse to position windows on

Introduction to Matlab. Summer School CEA-EDF-INRIA 2011 of Numerical Analysis

PHYS-4007/5007: Computational Physics Course Lecture Notes Appendix D

MATLAB Tutorial. Primary Author: Shoumik Chatterjee Secondary Author: Dr. Chuan Li

EE 216 Experiment 1. MATLAB Structure and Use

Introduction to MATLAB

BRUNO WELFERT Arizona State University AND. R. Kent Nagle. University of South Florida. Edward B. Saff. Vanderbilt University. A.

A QUICK INTRODUCTION TO MATLAB

The value of f(t) at t = 0 is the first element of the vector and is obtained by

A QUICK INTRODUCTION TO MATLAB. Intro to matlab getting started

Grace days can not be used for this assignment

Matlab Lecture 1 - Introduction to MATLAB. Five Parts of Matlab. Entering Matrices (2) - Method 1:Direct entry. Entering Matrices (1) - Magic Square

! The MATLAB language

Inlichtingenblad, matlab- en simulink handleiding en practicumopgaven IWS

Eric W. Hansen. The basic data type is a matrix This is the basic paradigm for computation with MATLAB, and the key to its power. Here s an example:

Matlab Tutorial and Exercises for COMP61021

Getting Started with MATLAB

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

Getting started with MATLAB

PC-MATLAB PRIMER. This is intended as a guided tour through PCMATLAB. Type as you go and watch what happens.

A very brief Matlab introduction

MATLAB Project: Getting Started with MATLAB

MATLAB BASICS. < Any system: Enter quit at Matlab prompt < PC/Windows: Close command window < To interrupt execution: Enter Ctrl-c.

Introduction to Scientific Computing with Matlab

Mathworks (company that releases Matlab ) documentation website is:

An Introduction to MATLAB. Lab tutor : Dennis Yang LIU Lab 1: Sept. 11, 2014

MATLAB Introductory Course Computer Exercise Session

MATLAB BASICS. M Files. Objectives

MATLAB Project: Getting Started with MATLAB

A General Introduction to Matlab

Getting To Know Matlab

Introduction to MATLAB

Matlab Tutorial for COMP24111 (includes exercise 1)

Appendix A. Introduction to MATLAB. A.1 What Is MATLAB?

A Brief Introduction to MATLAB

AMS 27L LAB #1 Winter 2009

Computer Project: Getting Started with MATLAB

Matlab Tutorial, CDS

Computational Modelling 102 (Scientific Programming) Tutorials

Matlab Programming Introduction 1 2

INTRODUCTION TO MATLAB

Lab. Manual. Practical Special Topics (Matlab Programming) (EngE416) Prepared By Dr. Emad Saeid

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain

AMS 27L LAB #2 Winter 2009

1 Introduction. 2 Useful linear algebra (reprise) Introduction to MATLAB Reading. Spencer and Ware (2008), secs. 1-7, 9-9.3,

ECE Lesson Plan - Class 1 Fall, 2001

EE3TP4: Signals and Systems Lab 1: Introduction to Matlab Tim Davidson Ext Objective. Report. Introduction to Matlab

Introduction to Scientific and Engineering Computing, BIL108E. Karaman

Laboratory 1 Octave Tutorial

Lecturer: Keyvan Dehmamy

Computer Exercises for Econometrics

To start using Matlab, you only need be concerned with the command window for now.

(Creating Arrays & Matrices) Applied Linear Algebra in Geoscience Using MATLAB

Introduction to Scientific Computing with Matlab

ECON 502 INTRODUCTION TO MATLAB Nov 9, 2007 TA: Murat Koyuncu

AN INTRODUCTION TO MATLAB

Matlab Tutorial. The value assigned to a variable can be checked by simply typing in the variable name:

PART 1 PROGRAMMING WITH MATHLAB

Introduction to MATLAB. Simon O Keefe Non-Standard Computation Group

Transcription:

Introduction to MATLAB Contents 1.1 Objectives... 1 1.2 Lab Requirement... 1 1.3 Background of MATLAB... 1 1.4 The MATLAB System... 1 1.5 Start of MATLAB... 3 1.6 Working Modes of MATLAB... 4 1.7 Basic Commands... 4 1.8 Basic Operations... 7 1.9 An Example of Using MATLAB to Perform Geometric Transformation... 8 Lab 1: Generation of an Engineering Drawing Using MATLAB... 11 1.1 Objectives To become familiar with the software MATLAB, and use it to perform numerical calculation and geometric transformations. This lab serves as the preparation step for Lab 1, which is the generation of engineering drawing using MATLAB. 1.2 Lab Requirement Students are required to read the manual, try the common commands listed in this manual on computer, and run the attached example in Section 0. 1.3 Background of MATLAB Originally developed to be a "matrix laboratory" by Cleve Moler, the recent versions that are written in C by the MathWorks Inc., have capabilities far beyond the original MATLAB. It is an interactive system and programming language for general scientific and technical computation. There are over 350 numeric and graphical functions available in the recent versions of MATLAB. Associated with it are several so-called toolboxes, each provides a fairly large set of additional commands that are of particular use for computing tasks in a specific engineering area such as control, system identification, digital signal processing, neural networks, optimization, etc. Together the MATLAB/Toolboxes package offers the user a powerful computing tool with superb graphics capabilities. More importantly, as MATLAB commands are similar to the way we express solution steps in mathematics, programming in MATLAB is much easier than its C or Fortran counterpart. 1.4 The MATLAB System

1. The MATLAB language. This is a high-level matrix/array language with control flow statements, functions, data structures, input/output, and object-oriented programming features. It allows both "programming in the small" to rapidly create quick and dirty throw-away programs, and "programming in the large" to create complete large and complex application programs. 2. The MATLAB working environment. This is the set of tools and facilities that you work with as the MATLAB user or programmer. It includes facilities for managing the variables in your workspace and importing and exporting data. It also includes tools for developing, managing, debugging, and profiling M-files, MATLAB's applications. 3. Handle Graphics. This is the MATLAB graphics system. It includes high-level commands for two-dimensional and three-dimensional data visualization, image processing, animation, and presentation graphics. It also includes low-level commands that allow you to fully customize the appearance of graphics as well as to build complete Graphical User Interfaces on your MATLAB applications (See Figure 1). 4. The MATLAB mathematical function library. This is a vast collection of computational algorithms ranging from elementary functions like sum, sine, cosine, and complex arithmetic, to more sophisticated functions like matrix inverse, matrix eigenvalues, Bessel functions, and fast Fourier transforms. 5. The MATLAB Application Program Interface (API). This is a library that allows you to write C and Fortran programs that interact with MATLAB. It includes facilities for calling routines from MATLAB (dynamic linking), calling MATLAB as a computational engine, and for reading and writing MAT-files. One can design customized graphical user interface (GUI) using Matlab language. Figure 1 shows an GUI developed in Matlab for performing optimization.

Figure 1 An example of a GUI developed using Matlab. 1.5 Start of MATLAB In PC workstation, run the MATLAB program by select it from the start program MATLAB menu, or double click the matlab icon on your desktop. The MATLAB tool will start and the main command window is shown in Figure 2. You can skip the following paragraph and move to Section 1.6 now. For UNIX users, before you are able to use UNIX workstations, you should have applied for a user account (your cc account). By entering your user name and password, you can log in to any terminal in the UNIX lab. Press the right button of the mouse, you will see a menu system. Go to Tools Terminal. You can open a terminal from which you can start your work. Repeat the process to open another terminal. You can also see a menu called Texteditor by browsing through the menu system. You can start a texteditor now for programming. For experienced UNIX users, you might find vi or emacs be more convenient for editing. In a UNIX workstation, by entering matlab in a terminal, you should quickly see the MATLAB prompt (>>) waiting for you to enter a command.

Figure 2 The main command window of Matlab. 1.6 Working Modes of MATLAB MATLAB provides two working modes. The first is command mode, in which users type a command following the prompt (>>) and MATLAB executes the command right after the end of the command. Data are stored in the memory and accessible unless being deliberately deleted (see command clear below). This command mode is useful for simple calculations and for learning or testing a command. Another working mode, the script mode, is to write a script file with suffix.m. One edits a script file by arranging commands in a sensible way. After saving the file, e.g. filename.m, one can type the filename directly after the prompt (>>) to execute the scripts written in the filename.m file. Note that you don t need to type.m for the filename. MATLAB will interpret and execute the commands in the script file one by one. This mode is often used to develop savable programs or large projects. 1.7 Basic Commands In what follows we attempt to identify and introduce a set of MATLAB commands that are used very often. 1. Help functions

If you need to know how to use a specific command, say plot, use command help plot. In case the explanation occupies more than one full screen page, you need to use more on which will cause MATLAB to pause after every screen full of information. You can type carriage return to continue. Type q while it is paused to exit out of displaying the current item. Also, more off disables automatic paging. If one doesn t know the command or want to know more commands in a topic, use command help to see a list of help topics. You can then select the topic for which you want to have further information. Some of the topics are listed below: HELP topics: matlab/general matlab/ops matlab/lang matlab/elmat matlab/elfun matlab/specfun matlab/matfun matlab/datafun matlab/polyfun matlab/funfun matlab/sparfun matlab/graph2d matlab/graph3d matlab/specgraph matlab/graphics matlab/uitools matlab/strfun matlab/iofun matlab/timefun matlab/datatypes matlab/demos - General purpose commands. - Operators and special characters. - Programming language constructs. - Elementary matrices and matrix manipulation. - Elementary math functions. - Specialized math functions. - Matrix functions - numerical linear algebra. - Data analysis and Fourier transforms. - Interpolation and polynomials. - Function functions and ODE solvers. - Sparse matrices. - Two dimensional graphs. - Three dimensional graphs. - Specialized graphs. - Handle Graphics. - Graphical user interface tools. - Character strings. - File input/output. - Time and dates. - Data types and structures. - Examples and demonstrations. For more help on directory/topic, type "help topic". For instance, if you want to know all the matrix functions, type help matfun to view all the related commands. Another command is demo, which provides demo on each Matlab function. One can also use the Help menu on the Matlab window to access web resources. 2. Directory Operations In a PC version, just simply press the directory icon in the left column as a standard WINDOWS operation. For UNIX users, to change current directory, one can use cd as in a UNIX terminal. The pwd command shows the current directory, and ls lists all the files in the current directory.

3. Exit Matlab To exit MATLAB, use quit or exit. 4. Save Data in Matlab If you want to save some of the quantities that you have generated using MATLAB, say you want to save two matrices A, B and a vector c as a data file named data-01 for future use, use save data-01 A B c After that you will then find a file entitled data-0l.mat in your directory. Next time when you invoke the software again, use load data-01 to load the data you saved. 5. Abort a Command To abort a command in MATLAB, hold down the control key and then press the letter c. 6. Case Sensitivity Unless you have used command casesen off, MATLAB is case-sensitive. For example, the variable named dt, Dt, and DT are different from each other. Case sensitivity can be turned on and off with casesen that changes the case-sensitivity setting each time when the command is executed. 7. Memory Clean-up MATLAB uses a command window to enter comments and data and to print results, and uses a graphics window to display plots. Use clf to clear the graphics window. To clear variables, say A and c, use command clear A c. Executing command clear only (without any specified variables following it) will delete all the variables generated. 8. Display Control If one adds a comma at the end of a command, MATLAB will not generate any output for that command, if it generates some output. Otherwise, the output will be printed to the screen after the command. For example, if one types after the prompt (>>) A=3+4; MATLAB will generate no output and one will see another prompt after the command. If only A=3+4 is types, MATLAB will output A=7 on the screen. This feature is very useful in the script mode for users to control the program output. 9. Some Default Settings

By default, the angle unit in MATLAB is radian (Note you need to deal with angles in your first lab. Pay attention to this!). MATLAB also has pre-defined constant, pi, which can help you transfer the unit degree to radian. 10. List Variables in Memory The command who lists the variables that you have defined, while command whos lists the variables along with their sizes. 11. Print out One can output a figure created by MATLAB to a file or a printer. For instance, one has generated a plot. In the command line, type print djpeg filename.jpg and MATLAB will export the plot to the file filename.jpg as a graph. A variety of file types, besides jpeg file, can be generated in the same way. For detailed info, please use help print. 1.8 Basic Operations In this section we introduce more M-ATLAB commands for basic computations. Arithmetic operations Example: Calculate (1+2)*(1+2)/(3-2)*2 The command in MATLAB will be >>(1+2)^2/(3-2)*2 18 >> Matrix operations 1. Define a matrix >> A = [1 2; 2 3; 3 4] A = 1 2 2 3 3 4 Transpose of matrix A >>A ans = 1 2 3 2 3 4 2. Check the size of a matrix >> size(a) ans = 3 2

>>size(a, 2) ans = 2 The latter command refers to the number of columns of A. If size(a, 1), then the output will be 3, the number of rows. 3. Generate a matrix with all zeros and is of the same size of A >> B = zeros(size(a)) B = 0 0 0 0 0 0 4. Matrix reference To refer to the data on the third row and the second column in A, the command is >> A(3, 2) To refer to the first two rows of A >> A(1:2,:) To refer to the second column of A >> A(:, 2) 5. Matrix operations Matrix operations take the same form as basic arithmetic operations provided that all the matrices in the operations are of the same size, i.e., the same number of rows and columns. Graphics Commands The most common graphics command is plot. Please type help plot in the MATLAB s command window to see the detail description of the command. One can use hold on if multiple plots are expected to be printed in a same figure. Otherwise use hold off. The default setting of MATLAB is hold off. One can use the axis command to control the output data range. For example. Axis([- 2 2 3 3]) specifies that the x-coordinate between [-2 2] and the y-coordinate between [-3 3] will be printed. One can use title( your title ) to add a title to a plot. Commands xlabel( your x label ) and ylabel( your y label ) add label to a 2-D plot. 1.9 An Example of Using MATLAB to Perform Geometric Transformation Problem: The square in position 1 has been transformed into the rectangle shown in Position 2. Find: A set of matrices that would accomplish the transformation. (Note the point order change during the transformation.

x y y D C A 4 4 1 C 8 8 A B B -2-1 D -6-2 A B 2 Solution: D C Let s assume that the point A of the square translates to the origin, then reflects with respect to x axis, scales down to the size of that in position 2, and finally translates to position 2. x The program, written in MATLAB, is as follows. Students are encouraged to create a file, called Yourfilename.m, with the following content. Please note sentences that start with symbol % are comments. % ------------ Program starts here --------------- % MATLAB script file for the example % Date: 12/21/99 % Clear all the stored variables in memory. This is useful when you have multiple % script files running at the same time and these files might have variables with the % same name. clear; % T1 is for the first translation T1=[1 0 0-4; 0 1 0-4; 0 0 1 0; 0 0 0 1]; % reflection with respect to x axis REFx = [1 0 0 0; 0-1 0 0; 0 0 1 0; 0 0 0 1]; % scale down S = [1 0 0 0; 0 0.25 0 0; 0 0 1 0; 0 0 0 1]; % the second translation T2 = [1 0 0-6; 0 1 0-1; 0 0 1 0;

0 0 0 1]; % The original point matrix, note that the last point repeats the first point % to make a closed box. P1 = [4 8 8 4 4; 4 4 8 8 4; 0 0 0 0 0; 1 1 1 1 1]; % The resultant transformation matrix C = T2 * S * REFx * T1; % The point matrix of the rectangular in position 2 P2 = C * P1; % Prepare for plot clf; hold on; % plot grid grid on; % plot x and y axes as yellow lines plot([0,0],[-8, 8], y ); plot([-8,8],[0,0], y ); % plot box in original (red) and transformed (green) positions plot(p1(1,:), P1(2,:), r ); plot(p2(1,:), P2(2,:), g ); axis([-8 8 8 8]); %specify the axis range % End the hold status hold off % ------------ Program ends here ------------------- Having created the file, save it as Yourfilename.m, then in the command window of MATLAB, type Yourfilename to execute it. If some error exists, MATLAB will prompt you on which line it hangs up. You can go back to the line in the script file to correct the error.

Lab 1: Generation of an Engineering Drawing Using MATLAB Objectives Practice of 3-D geometric transformation Understanding of the projections in generating perspective views of an object Application of CAD theories in creating an engineering drawing Understanding how the computer technology aids in engineering design Tasks Given an array of points of a 3D object, you are asked to generate a preliminary engineering drawing for the object using MATLAB. The given points are in Table 1. The points and some dimensions are shown in Figure 3 to help you better perceive the object. All the windows and doors are put in the drawing to make the object meaningful. Students are expected to perform suitable geometric transformation and projection using MATLAB. The finished drawing should be like that in Figure 4 (Coordinate tags in the figure are not required.) Table 1 Point coordinates of the object Points x y z 1 0 3 0 2 0 10.2 0 3 0 10.2 3 4 0 3 4.7 5 5.4 3 4.7 6 5.4 3 0 7 5.4 0 0 8 5.4 0 3 9 10.2 0 3 10 10.2 0 0 11 10.2 10.2 0 12 10.2 10.2 3 13 10.2 7.2 7.2 14 0 7.2 7.2 Questions In a commercial CAD package, an engineering drawing is generated in a similar manner from the geometric model of an object as you have done in the lab. A noticeable difficulty in this lab is to identify which points are connected, while a commercial CAD package can generate all the drawings automatically. It seems that a CAD package knows which points are connected. Based on what you have learned in this course, how do you think a commercial package solve this problem? Can you think of any alternative ways? Report The lab report should follow the format given in the Appendix and should include the final drawing, source code, and answers to the questions above.

Figure 3 An illustration of the object. Figure 4 The generated drawing from MATLAB.