A MATLAB Exercise Book

A MATLAB Exercise Book

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.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 3.4.2 The while 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 . ..

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    182 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us