A MATLAB Exercise Book (2nd edition)

A MATLAB Exercise Book

Ludmila I. Kuncheva and Cameron C. Gray

MATLAB R 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 MATLAB . . . . . . . . . . . . . . . . . . . . . .

1.2 Programming Environment . . . . . . . . . . .

1.2.1 Environment Layout and File Editor .

1.2.2 Running Your Code . . . . . . . . . . .

1.2.3 Getting Help . . . . . . . . . . . . . . .

1.2.4 Tips . . . . . . . . . . . . . . . . . . . . .

1.2.5 Good Programming Style and Design

1.3 MATLAB as a Calculator . . . . . . . . . . . .

1.4 Exercises . . . . . . . . . . . . . . . . . . . . . .

2 MATLAB: The Matrix Laboratory

2.1 Variables and Constants . . . . . . .

2.1.1 Value Assignment . . . . . .

2.1.2 Names and Conventions . .

2.2 Matrices . . . . . . . . . . . . . . . .

2.2.1 Creating and Indexing . . .

2.2.2 Accessing Matrix Elements

2.2.3 Visualising a Matrix . . . . .

2.2.4 Concatenating and Resizing

2.2.5 Matrix Gallery . . . . . . . .

2.3 The Colon Operator . . . . . . . . .

2.4 Linear spaces and mesh grid . . . .

2.5 Operations with matrices . . . . . .

2.6 Cell Arrays . . . . . . . . . . . . . . .

2.7 Exercises . . . . . . . . . . . . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

Matrices

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

3 Logical Expressions and Loops

3.1 Logical Expressions . . . . . . . . . . . . . . .

3.1.1 Representation . . . . . . . . . . . . .

3.1.2 Type and order of operations . . . .

3.2 Indexing arrays with logical indices . . . .

3.3 MATLAB¡¯s logical functions and constructs

3.3.1 Logical functions . . . . . . . . . . . .

3.3.2 Conditional operations . . . . . . . .

.

.

.

.

.

.

.

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

Practices

. . . . . . .

. . . . . . .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

1

1

1

1

2

3

3

3

4

5

.

.

.

.

.

.

.

.

.

.

.

.

.

.

7

7

7

8

8

8

9

11

11

12

13

14

16

17

18

.

.

.

.

.

.

.

23

23

23

23

25

26

26

26

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

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

Google Online Preview   Download