Transcription of A MATLAB Exercise Book (2nd edition)
1 A MATLAB Exercise BookLudmila I. Kuncheva and Cameron C. GrayMATLABR is a registered trademark of Mathworks Inc. in the United States and elsewhere. All other marks are trademarkand copyright of their respective rights reserved. No reproduction, copy or transmission of this publication may be made without written permissionfrom the authors. No portion of this publication may be reproduced, copied or transmitted save with written permission inaccordance with the provisions of the Copyright, Designs and Patents Act 1988 or any licence permitting limited copyingissued by the Copyright Licensing Agency, Saffron House, 6-10 Kirby Street, London EC1N person who does any unauthorised act in relation to this publication may be liable to criminal prosecution and civilclaims for authors have asserted their rights to be identified as the authors of this work in accordance with the Copyright, Designsand Patents Act 2020 by Ludmila I.
2 Kuncheva and Cameron C. GrayISBN 978-0-244-25328-8 Second EditionPreface to the second editionThis is still a book containing Exercise problems in MATLAB . The collection of problems covers basictopics and is meant to stimulate student s creativity in designing and implementing respective elements of the language are briefly covered before the Exercise section of 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 weremoved the spaces left for reader should be aware that there are many ways to solve a problem, and the solutions that weoffer in this book are not necessarily the shortest or the most time-efficient ones.
3 Some solutions arechosen for their readability. Like most programming languages, MATLAB is developing from versionto version, and some of the commands explained and used here may alter in syntax or functionality inthe book could be useful for MATLAB course instructors as a set of ideas and examples to draw uponwhen creating their own collections of Kuncheva and Cameron GrayBangor, January 14, 2020 Preface to the first editionThe book is meant to be used for Exercise by the students taking module Algorithm Design withMATLAB at the School of Computer Science, Bangor University, UK. The module does not go intogreat details about MATLAB capabilities. Most topics are taught within one or two hour-long is difficult to go beyond the basics and into the exciting topics such as image edge detection andsegmentation, statistical analyses or intricate graphical user interfaces.
4 Consequently, the exercises atthe end of the chapters are meant to stimulate the student s ability to solve problems using the limitedsubset of the language rather than test their expertise in mastering of the problems assume knowledge of elementary algebra and geometry, or specific algorithmssuch as bubble sorting, Monte Carlo and evolutionary algorithms. However, we kept the expositionsimple and self-contained, so that the book can be useful for a reader with minimal technical ormathematical problems are of different difficulties. Some can be used in class tests or exams, while othersrequire more time and effort, and are more suitable for coursework. Solutions are provided only forthe examples in each chapter.
5 Because the book is intended to be a personal hard-copy, we have leftspaces for handwritten answers and notes as shown enjoyed writing this book and hope that you will enjoy the intellectual Kuncheva and Cameron GrayBangor, June 17, 2014 Contents1 Getting MATLAB .. Programming Environment .. Environment Layout and File Editor .. Running Your Code .. Getting Help .. Tips .. Good Programming Style and Design Practices .. MATLAB as a Calculator .. Exercises .. 52 MATLAB : The Matrix Variables and Constants .. Value Assignment .. Names and Conventions .. Matrices .. Creating and Indexing .. Accessing Matrix Elements.
6 Visualising a Matrix .. Concatenating and Resizing Matrices .. Matrix Gallery .. The Colon Operator .. Linear spaces and mesh grid .. Operations with matrices .. Cell Arrays .. Exercises .. 183 Logical Expressions and Logical Expressions .. Representation .. Type and order of operations .. Indexing arrays with logical indices .. MATLAB s logical functions and constructs .. Logical functions .. Conditional operations .. Loops in MATLAB .. Theforloop .. Thewhileloop .. Examples .. Brute Force Sorting .. When is a while loop useful? .. Exercises .. 314 Syntax .. Naming .. Multiple Functions.
7 Inline (Anonymous) Functions and Function Handles .. Recursion .. Exercises .. 395 Plotting Commands .. Plot .. Fill .. Examples .. Exercises .. 466 Data and Simple Random Number Generation .. Simple statistics and plots .. Examples .. Exercises .. 567 Encoding .. Useful String Functions .. Examples .. Imaginary Planet Names .. String Formatting .. Exercises .. 728 Types of Image Representations .. Binary Images .. RGB Images .. Grey Intensity Images .. Indexed Images .. Useful Functions .. Examples .. Image Manipulation .. Tone ASCII Art .. Exercises .. 849 Animation Methods.
8 Mouse Control .. Examples .. Shivering Ball .. Three Moving Circles .. A Fancy Stopwatch .. Exercises .. 10010 Graphical User Interfaces - Programming GUIs .. Examples .. One Colour Button .. Disappearing Shapes .. Catch-me-up Game .. Exercises .. 11411 Sounds as Data .. Exercises .. 12712 Solutions132 Index172 Chapter 1 Getting MATLABMATLABR is a software package designed for mathematical and scientific computing. It is alsoa development environment and a programming language. Its primary specialisation is efficientlyhandling matrix and vector Programming Environment Layout and File EditorFigure shows a version of the default MATLAB programming environment.
9 It consists of four spaces:(1) the MATLAB Command Window with the MATLAB prompt sign , (2) the Workspace displaying thevariables in the MATLAB memory, (3) the Current folder box showing the folder s content, and (4) theCommand history box showing a list of recent commands.(1) Command window(2) Workspace (memory)(3) Current folder(4) CommandhistoryNavigate to the desired folderCreate a new fileFigure : Default Layout of the MATLAB Programming MATLAB can execute commands typed straight in the Command Window, it is best to storethe code in a bespoke m file or MATLAB script. A navigation button (top right in Figure ) allows12 CHAPTER 1. GETTING STARTEDthe user to choose a folder where the work will be stored. Click on the New file icon at the top leftcorner.
10 The editor window appears as shown in Figure the whole file from hereRun a cell from hereFigure : The editor Running Your CodeA set of MATLAB commands can be executed by one of the following ways1. Type the commands directly in the MATLAB Command Highlight and copy the commands from the editor window and paste them in the Highlight the commands in the editor window and press function keyF9. For Apple/OS Xmachines, you should use+F7(by default you will also need to hold thefnkey to avoidthe special meaning of F7).4. Run the file with the commands in the editor window from the run icon, as indicated in Figure the first run, MATLAB will ask you to save the file if you have not done this already. All thecommands in the editor will be carried out in the order of Save the file and run it by typing its name at the prompt in the Command Window.