A MATLAB Exercise Book (2nd edition)

[Pages:182]A MATLAB Exercise Book

Ludmila I. Kuncheva and Cameron C. Gray

R

MATLAB is a registered trademark of Mathworks Inc. in the United States and elsewhere. All other marks are trademark and copyright of their respective owners. All rights reserved. No reproduction, copy or transmission of this publication may be made without written permission from the authors. No portion of this publication may be reproduced, copied or transmitted save with written permission in accordance with the provisions of the Copyright, Designs and Patents Act 1988 or any licence permitting limited copying issued by the Copyright Licensing Agency, Saffron House, 6-10 Kirby Street, London EC1N 8TS. Any person who does any unauthorised act in relation to this publication may be liable to criminal prosecution and civil claims for damages. The authors have asserted their rights to be identified as the authors of this work in accordance with the Copyright, Designs and Patents Act 1988.

Copyright c 2020 by Ludmila I. Kuncheva and Cameron C. Gray ISBN 978-0-244-25328-8 Second Edition

Preface to the second edition

This is still a book containing exercise problems in MATLAB. The collection of problems covers basic topics and is meant to stimulate student's creativity in designing and implementing algorithms. The respective elements of the language are briefly covered before the exercise section of each chapter. In this edition:

? We have revised the problem selection in view of some changes in the new MATLAB releases. ? Solutions are provided for all even-numbered problems. ? We realised that the cost of the book does not make it suitable as an exercise notebook, so we

removed the spaces left for notes. The reader should be aware that there are many ways to solve a problem, and the solutions that we offer in this book are not necessarily the shortest or the most time-efficient ones. Some solutions are chosen for their readability. Like most programming languages, MATLAB is developing from version to version, and some of the commands explained and used here may alter in syntax or functionality in the future. The book could be useful for MATLAB course instructors as a set of ideas and examples to draw upon when creating their own collections of problems.

Ludmila Kuncheva and Cameron Gray Bangor, January 14, 2020

Preface to the first edition

The book is meant to be used for exercise by the students taking module `Algorithm Design with MATLAB' at the School of Computer Science, Bangor University, UK. The module does not go into great details about MATLAB capabilities. Most topics are taught within one or two hour-long lectures. It is difficult to go beyond the basics and into the exciting topics such as image edge detection and segmentation, statistical analyses or intricate graphical user interfaces. Consequently, the exercises at the end of the chapters are meant to stimulate the student's ability to solve problems using the limited subset of the language rather than test their expertise in mastering MATLAB. Some of the problems assume knowledge of elementary algebra and geometry, or specific algorithms such as bubble sorting, Monte Carlo and evolutionary algorithms. However, we kept the exposition simple and self-contained, so that the book can be useful for a reader with minimal technical or mathematical background. The problems are of different difficulties. Some can be used in class tests or exams, while others require more time and effort, and are more suitable for coursework. Solutions are provided only for the examples in each chapter. Because the book is intended to be a personal hard-copy, we have left spaces for handwritten answers and notes as shown below.

We enjoyed writing this book and hope that you will enjoy the intellectual workout.

Ludmila Kuncheva and Cameron Gray Bangor, June 17, 2014

Contents

1 Getting Started

1

1.1 MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 Programming Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

1.2.1 Environment Layout and File Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2.2 Running Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.2.3 Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.2.4 Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.2.5 Good Programming Style and Design Practices . . . . . . . . . . . . . . . . . . . . 3 1.3 MATLAB as a Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

2 MATLAB: The Matrix Laboratory

7

2.1 Variables and Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2.1.1 Value Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2.1.2 Names and Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

2.2 Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 2.2.1 Creating and Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 2.2.2 Accessing Matrix Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.2.3 Visualising a Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.2.4 Concatenating and Resizing Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.2.5 Matrix Gallery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

2.3 The Colon Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2.4 Linear spaces and mesh grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2.5 Operations with matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2.6 Cell Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

3 Logical Expressions and Loops

23

3.1 Logical Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 3.1.1 Representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 3.1.2 Type and order of operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

3.2 Indexing arrays with logical indices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 3.3 MATLAB's logical functions and constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

3.3.1 Logical functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 3.3.2 Conditional operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

3.4 Loops in MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

3.4.1 The for loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

while

3.4.2 The

loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

3.5 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

3.5.1 Brute Force Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

3.5.2 When is a while loop useful? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

3.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

4 Functions

36

4.1 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 4.2 Naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 4.3 Multiple Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 4.4 Inline (Anonymous) Functions and Function Handles . . . . . . . . . . . . . . . . . . . . . . 37 4.5 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 4.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

5 Plotting

41

5.1 Plotting Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 5.1.1 Plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 5.1.2 Fill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

5.2 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 5.3 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

6 Data and Simple Statistics

53

6.1 Random Number Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 6.2 Simple statistics and plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 6.3 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 6.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56

7 Strings

69

7.1 Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 7.2 Useful String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 7.3 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

7.3.1 Imaginary Planet Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 7.3.2 String Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 7.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

8 Images

78

8.1 Types of Image Representations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 8.1.1 Binary Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 8.1.2 RGB Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78

8.1.3 Grey Intensity Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 8.1.4 Indexed Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 8.2 Useful Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 8.3 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 8.3.1 Image Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 8.3.2 Tone ASCII Art . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 8.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84

9 Animation

95

9.1 Animation Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 9.2 Mouse Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 9.3 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97

9.3.1 Shivering Ball . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 9.3.2 Three Moving Circles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 9.3.3 A Fancy Stopwatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 9.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100

10 Graphical User Interfaces - GUI

110

10.1 Programming GUIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 10.2 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

10.2.1 One Colour Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 10.2.2 Disappearing Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 10.2.3 Catch-me-up Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 10.3 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114

11 Sounds

125

11.1 Sounds as Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 11.2 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127

12 Solutions

132

Index

172

Chapter 1

Getting Started

1.1 MATLAB

R

MATLAB is a software package designed for mathematical and scientific computing. It is also a development environment and a programming language. Its primary specialisation is efficiently handling matrix and vector mathematics.

1.2 Programming Environment

1.2.1 Environment Layout and File Editor

Figure 1.1 shows a version of the default MATLAB programming environment. It consists of four spaces: (1) the MATLAB Command Window with the MATLAB prompt sign ?, (2) the Workspace displaying the variables in the MATLAB memory, (3) the Current folder box showing the folder's content, and (4) the Command history box showing a list of recent commands.

Create a new file

Navigate to the desired folder

(3) Current folder

(1) Command window

(2) Workspace (memory)

(4) Command history

Figure 1.1: Default Layout of the MATLAB Programming Environment.

Although MATLAB can execute commands typed straight in the Command Window, it is best to store the code in a bespoke `m file' or MATLAB script. A navigation button (top right in Figure 1.1) allows

1

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download