MATLAB Short Course

  • View
    68

  • Download
    0

Embed Size (px)

Text of MATLAB Short Course

ENG3103 Engineering Problem Solving 3

Faculty of Engineering and Surveying

MATLAB Short Course

Written by: KA Moore Faculty of Engineering and Surveying The University of Southern Queensland

Published by

University of Southern Queensland Toowoomba, Queensland 4350 Australia

http://www.usq.edu.au

University of Southern Queensland, 2004.2Copyrighted materials reproduced herein are used under the provisions of the Copyright Act 1968 as amended, or as a result of application to the copyright owner.

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means electronic, mechanical, photocopying, recording or otherwise without prior permission.

A Produced using L TEX 2 by KA Moore in the USQ style.

Table of Contents

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . (v)

1 The MATLAB Workspace 1.1 1.2 1.3 1.4 1.5 1.6 1.7 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The MATLAB Desktop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Basic Commands and Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Common MATLAB Functions and Relational Operators . . . . . . . . . . . . . . . . . Plotting and Input/Output Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . Problem-Solving Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Self Assessment Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Tutorial Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Solutions to Self Assessment Questions . . . . . . . . . . . . . . . . . . . . . . . . . .

1 1 2 3 8 10 12 16 17 19 20

(i)

2 Array and Matrix Operations 2.1 2.2 2.3 2.4 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Vector and Matrix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Self Assessment Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Tutorial Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Solutions to Self Assessment Questions . . . . . . . . . . . . . . . . . . . . . . . . . .

21 21 21 23 26 28 30 32

3 Files, Functions and Data Structures 3.1 3.2 3.3 3.4 3.5 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Data Import and Export . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Basic I/O and Mathematical Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . User-Dened Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Cells and Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Self Assessment Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Tutorial Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Solutions to Self Assessment Questions . . . . . . . . . . . . . . . . . . . . . . . . . .

33 33 33 34 36 38 41 44 45

(ii)

4 Programming with MATLAB 4.1 4.2 4.3 4.4 4.5 4.6 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Relational and Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Program Design and Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Self Assessment Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Tutorial Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Solutions to Self Assessment Questions . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Plotting and Model Building 5.1 5.2 5.3 5.4 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Plotting Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Function Discovery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3D Plotting and Graphics Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Self Assessment Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Tutorial Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Solutions to Self Assessment Questions . . . . . . . . . . . . . . . . . . . . . . . . . . Reference Appendix A Model Solutions for Self-Tests and Tutorial Problems A.1 The MATLAB Workspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A.2 Array and Matrix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A.3 Files, Functions and Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . .

47 47 47 49 50 52 56 57 61 62 63 63 63 66 72 73 75 76 77 79 79 82 92

A.4 Programming with MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 A.5 Plotting and Model Building . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 (iii)

(iv)

Introduction

Introduction

The ability to create, implement and interpret sometimes complex computer programs has become an almost mandatory requirement for the graduate engineer in all disciplines. These days graduates are expected by industry to be able to devise appropriate computer solutions to both standard and nonstandard engineering problems, or, where commercially available software does exist, to be able to adapt or modify existing computer models for particular non-standard applications. The objectives of this course are twofold. Firstly it is intended to provide you with some initial exposure to, and some hands-on experience with a high level computer programming language in order to develop some basic competencies in working within a programming environment, and in creating computer code to implement solutions to typical standard and non-standard engineering problems. Secondly it aims to develop skills in engineering problem-solving within the context of computer programming. As we will see later in this course, problem-solving is a process which can be generally described as a series of steps the engineer follows in order to implement practical solutions: (i) Understand the problem. (ii) Simplify the problem as much as possible. (iii) Collect the necessary data/information required to formulate a solution. (iv) Formulate an algorithm to implement a solution. (v) Implement the solution. (vi) Perform a reality check on the answers. This problem-solving methodology will be the primary focus throughout this course. The language chosen as a platform through which to impart these skills is MATLAB, although the choice of language is not particularly important in terms of the degree to which the necessary programming skills can be developed. Most of the high level languages used in engineering such as C, C++ and FORTRAN all use similar data types such as single and double precision oats, long and short integers, arrays and structures, and such programming constructs as conditional statements, loops, functions and subroutines, so that having gained mastery in one particular language it is not difcult for the student to master another. The skills you gain in this and in subsequent related subjects will also give you the condence to implement solutions in languages other than MATLAB. MATLAB is more than just a programming language; it is a programming environment. In contrast with other high level languages, the MATLAB programming environment allows interactive programming and furthermore it does not require prior compilation of the computer code in order to implement it. It is widely used in universities throughout the world and indications are that it is fast becoming an industry standard in Australia. It operates under a Windows-based environment and the language itself is very(v)

similar to C, although there are many additional constructs and built-in functions which make the MATLAB language superior to standard C in many respects. Although the idea of interactive programming is extremely useful as a learning tool, most engineering solutions require much more than just a few command-line instructions, and we will focus more on development of suitable M les to implement problem solutions. An M le is simply a set of MATLAB instructions contained within an ASCII1 text le, and has the same purpose as source code in more traditional high level programming languages. The Resource Book for this subject is the prescribed text:

Palm, W. J. (2001), Introduction to MATLAB 6 for Engineers, McGraw-Hill.The material in this booklet is intended to complement the text, providing additional explanation and emphasis where required. The text includes progressively-staged self-test questions which are generally of the short-answer type as well as tutorial problems at the end of each chapter. Answers to some of the text-based self-test questions are provided in the text with the question, and those for some of the text-based tutorial problems are provided in the nal pages (pp.590-592) of the text. This booklet provides additional self-assessment questions in mu