A MATLAB Exercise Book

Total Page:16

File Type:pdf, Size:1020Kb

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 . ..
Recommended publications
  • Cesifo1 Wp1333.Pdf
    A Service of Leibniz-Informationszentrum econstor Wirtschaft Leibniz Information Centre Make Your Publications Visible. zbw for Economics Peitz, Martin; Waelbroeck, Patrick Working Paper An economists guide to digital music CESifo Working Paper, No. 1333 Provided in Cooperation with: Ifo Institute – Leibniz Institute for Economic Research at the University of Munich Suggested Citation: Peitz, Martin; Waelbroeck, Patrick (2004) : An economists guide to digital music, CESifo Working Paper, No. 1333, Center for Economic Studies and ifo Institute (CESifo), Munich This Version is available at: http://hdl.handle.net/10419/18698 Standard-Nutzungsbedingungen: Terms of use: Die Dokumente auf EconStor dürfen zu eigenen wissenschaftlichen Documents in EconStor may be saved and copied for your Zwecken und zum Privatgebrauch gespeichert und kopiert werden. personal and scholarly purposes. Sie dürfen die Dokumente nicht für öffentliche oder kommerzielle You are not to copy documents for public or commercial Zwecke vervielfältigen, öffentlich ausstellen, öffentlich zugänglich purposes, to exhibit the documents publicly, to make them machen, vertreiben oder anderweitig nutzen. publicly available on the internet, or to distribute or otherwise use the documents in public. Sofern die Verfasser die Dokumente unter Open-Content-Lizenzen (insbesondere CC-Lizenzen) zur Verfügung gestellt haben sollten, If the documents have been made available under an Open gelten abweichend von diesen Nutzungsbedingungen die in der dort Content Licence (especially Creative
    [Show full text]
  • Elite Karaoke Draft by Artist 2 Pistols Feat
    Elite Karaoke Draft by Artist 2 Pistols Feat. Ray J You Know Me (Hed) Planet Earth 2 Pistols Feat. T-Pain & Tay Bartender Dizm Blackout She Got It Other Side 2 Play Feat. Thomas Jules & Renegade Jucxi D 10 Years Careless Whisper Actions & Motives 2 Unlimited Beautiful No Limit Drug Of Choice Twilight Zone Fix Me 20 Fingers Fix Me (Acoustic) Short Dick Man Shoot It Out 21 Demands Through The Iris Give Me A Minute Wasteland 21 Savage Feat. Offset, Metro 10,000 Maniacs Boomin & Travis Scott Because The Night Ghostface Killers Candy Everybody Wants 2Pac Like The Weather Changes More Than This Dear Mama These Are The Days How Do You Want It Trouble Me I Get Around 100 Proof (Aged In Soul) So Many Tears Somebody's Been Sleeping Until The End Of Time 101 Dalmations 2Pac Feat. Dr. Dre Cruella De Vil California Love 10cc 2Pac Feat. Elton John Dreadlock Holiday Ghetto Gospel Good Morning Judge 2Pac Feat. Eminem I'm Not In Love One Day At A Time The Things We Do For Love 2Pac Feat. Eric Williams Things We Do For Love Do For Love 112 2Pac Feat. Notorious B.I.G. Dance With Me Runnin' Peaches & Cream 3 Doors Down Right Here For You Away From The Sun U Already Know Be Like That 112 Feat. Ludacris Behind Those Eyes Hot & Wet Citizen Soldier 112 Feat. Super Cat Dangerous Game Na Na Na Duck & Run 12 Gauge Every Time You Go Dunkie Butt Going Down In Flames 12 Stones Here By Me Arms Of A Stranger Here Without You Far Away It's Not My Time (I Won't Go) Shadows Kryptonite We Are One Landing In London 1910 Fruitgum Co.
    [Show full text]
  • "DPW FOUR COUNTRY CIRCUIT 2021" Salon Croatia
    6th Circular Exhibition of Photography "DPW FOUR COUNTRY CIRCUIT 2021" Salon Croatia PSA 2021-1214 Accepted Theme A) Nature Australia Andrew Raguse - An evil eye Andrew Raguse - Silver throated tanager Dennis Hill - Serengeti Leopard 6 Paul Thompson - A Flamboyance of Flamingos Paul Thompson - Fijian Banded Iguana Paul Thompson - Seduction Roy Killen - Busty nest builder Roy Killen - Impala and leopard Sandy Mahon - The Mouse Plague Austria Hiwa Naqshi - Goling Belgium Jhony Vandebroeck - Jonge Steenbok op wandel Jhony Vandebroeck - Stevige klauwen Rene Van Echelpoel - Eating hawk Rene Van Echelpoel - Rustende libel Rene Van Echelpoel - Squirrel with chestnut Roland Wantens - Vroeg Hert Tiffany Heymans - 3 is better then 2 Tiffany Heymans - Beetle love Canada Danlei Ye - Galapagos wildlife94 China Dongsheng Nie - parade Dongsheng Nie - tree and birds Guomei Yang - Promised shoal Jie Niu - Spring in the mouth Jiyuan Zhang - Multicolored Jiyuan Zhang - Multicolored music notes Qingshun Liu - a solemn expression Qingshun Liu - covering its face Qingshun Liu - good meal Tianyi Xiong - Circling Weiling Xiao - in spring Xiping An - The eagle spread wings yichi wang - insupportable kiss yichi wang - seek for food Yongjiang Zhang - Care Yongjiang Zhang - Group dance Zhimin Zhang - fall in love 1 Zhimin Zhang - strength and skill England Andrew Crawford - Dancing Goose Andrew Crawford - Stag Horn David Jellie - Blue Tailed Damselfly David Price - Blue Tit D David Price - Common Darter Mating in Flight jennifer margaret webster - Little Owl Landingwith
    [Show full text]
  • "DPW FOUR COUNTRY CIRCUIT 2021" Salon Serbia
    6th Circular Exhibition of Photography "DPW FOUR COUNTRY CIRCUIT 2021" Salon Serbia PSA 2021-1214 Accepted Theme A) Nature Australia Andrew Raguse - An evil eye Dennis Hill - Amboseli Elephants Grace Bryant - Whale At Play Ian Patterson - Hiding Hawkfish Paul Thompson - A Flamboyance of Flamingos Roy Killen - Busty nest builder Roy Killen - Impala and leopard Belgium Jhony Vandebroeck - Stevige klauwen Rene Van Echelpoel - Eating hawk Roland Wantens - Vroeg Hert Tiffany Heymans - 3 is better then 2 Canada Danlei Ye - Have a fight79 Danlei Ye - The polar bear26 China Dongsheng Nie - parade Dongsheng Nie - tree and birds Guomei Yang - Promised shoal Guomei Yang - Vigorous renewal Huimin Wang - Habitat Jiyuan Zhang - Multicolored music notes Qingshun Liu - a solemn expression Qingshun Liu - covering its face Qingshun Liu - good meal Tianyi Xiong - Circling Xiping An - Blue tale Xiping An - Dance trippingly Xiping An - The eagle spread wings yichi wang - insupportable kiss yichi wang - seek for food Yongjiang Zhang - Mount Zhimin Zhang - air dinner Zhimin Zhang - bomber Zhimin Zhang - fall in love 1 Zhimin Zhang - strength and skill England Andrew Crawford - Dancing Goose Andrew Crawford - Wild Geese David Jellie - Blue Tailed Damselfly jennifer margaret webster - Snow Leopard jennifer margaret webster - Two Grizzly bears eating Salmon Rebekah Nash - Great Crested Grebes Courtship Dance Trevor Woods - Grey Heron Nest Building 0006 Trevor Woods - Red Squirrel 0341 Finland Jarmo Mäki - Attentive France JACKY DUMANGIN - REGARDS Marc Anagnostidis - Capture Agrodiaetus Marc Anagnostidis - Libellula Ludoviciana PASCALE BECK-CARPENTIER - Dans mon anemone Robert Klauth - Grue demoiselle Robert Klauth - Les Loups Germany Frank Hausdoerfer - Utah 55 Klaus Reibl - ApThe Boss Klaus Reibl - Joy Klaus Reibl - Peli Klaus Schwinges - Gorilla 9 Wolfgang Schweden - Red Squirrel in spring 1 Hong Kong Ching Ching Chan - Capture Ching Ching Chan - Chatting Ching Ching Chan - Childhood Sweetheart Ching Ching Chan - Parental Love H.W.
    [Show full text]
  • Factorial Validity of Utrecht Work Engagement Scale As
    REVIEW PAPER International Journal of Occupational Medicine and Environmental Health 2017;30(2):161 – 175 https://doi.org/10.13075/ijomeh.1896.00947 DO WE ALL AGREE ON HOW TO MEASURE WORK ENGAGEMENT? FACTORIAL VALIDITY OF UTRECHT WORK ENGAGEMENT SCALE AS A STANDARD MEASUREMENT TOOL – A LITERATURE REVIEW KONRAD KULIKOWSKI Jagiellonian University, Kraków, Poland Faculty of Philosophy, Institute of Psychology Abstract Work engagement as a predictor of health is an emerging concept in occupational science and the Utrecht Work En- gagement Scale (UWES) is the most popular work engagement measurement tool. However, despite its popularity, the UWES is not free from controversy concerning its factorial validity. In this paper, 21 research studies on both UWES-9 and UWES-17 factorial validity within the confirmatory factor analysis (CFA) approach have been reviewed in order to answer the question as to which of the UWES factorial structures displays greater validity. The originally proposed three- factor structure of the UWES has been recognized as superior in 6 studies. In further 6 studies, the UWES structure with 1 general factor has been found to be superior. In 8 studies, the authors have concluded that the one- and three-factor structures could be considered equivalent. One study has failed to confirm either the one- or three-factor structure of the UWES. These ambiguous results from studies focusing on the UWES factorial validity are puzzling because they not only indicate a lack of validity for the UWES as a measurement tool but might also challenge the whole concept of work en- gagement as a three-factor structure of dedication, vigor and absorption.
    [Show full text]