Matlab: A Practical Introduction to Programming and Problem Solving

Matlab: A Practical Introduction to Programming and Problem Solving

2.4 9
by Stormy Attaway
     
 

View All Available Formats & Editions

Assuming no knowledge of programming, this book presents both programming concepts and MATLAB’s built-in functions, providing a perfect platform for exploiting MATLAB’s extensive capabilities for tackling engineering problems. It starts with programming concepts such as variables, assignments, input/output, and selection statements, moves onto loops and

Overview

Assuming no knowledge of programming, this book presents both programming concepts and MATLAB’s built-in functions, providing a perfect platform for exploiting MATLAB’s extensive capabilities for tackling engineering problems. It starts with programming concepts such as variables, assignments, input/output, and selection statements, moves onto loops and then solves problems using both the ‘programming concept’ and the ‘power of MATLAB’ side-by-side. In-depth coverage is given to input/output, a topic that is fundamental to many engineering applications.

Ancillaries available with the text:

  • Instructor solution manual (available Aug. 1st)
  • electronic images from the text (available Aug 16th)
  • m-files (available Aug 1st)


* Presents programming concepts and MATLAB built-in functions side-by-side, giving students the ability to program efficiently and exploit the power of MATLAB to solve problems. * In depth coverage of file input/output, a topic essential for many engineering applications * Systematic, step-by-step approach, building on concepts throughout the book, facilitating easier learning * Sections on ‘common pitfalls’ and ‘programming guidelines’ direct students towards best practice

* New to this edition:

  • More engineering applications help the reader learn Matlab in the context of solving technical problems
  • New and revised end of chapter problems
  • Stronger coverage of loops and vectorizing in a new chapter, chapter 5
  • Updated to reflect current features and functions of the current release of Matlab

Editorial Reviews

From the Publisher

"This book is an excellent way to learn quickly how to use MATLAB…The most significant changes in this edition include a new chapter on vectorized code and manipulating vectors, concepts used in image processing, modified and new end-of-chapter exercises, and the use of MATLAB version R2011a."—Electrical Insulation Magazine, January 2013, Vol. 29, No. 1, page 66 "This book is an excellent way to learn quickly how to use MATLAB…Anyone who wants to learn the basis of MATLAB quickly should own this book."—IEEE Electrical Insulation Magazine,page 66 "In-depth coverage is given to input/output, a topic that is fundamental to many engineering applications. New to this edition are more engineering applications to help the reader learn MATLAB in the context of solving technical problems, new and revised end-of-chapter problems, stronger coverage of loops and vectorizing in a new chapter and updates to reflect current features and functions of the current release of MATLAB."—Lunar and Planetary Information "Assuming no knowledge of programming, this book presents both programming concepts and MATLAB’s built-n functions, providing a perfect platform for exploiting its extensive capabilities for tackling engineering problems. The book starts with programming concepts such as variables, assignments, input/output, and selection statements, moves onto loops, and then solves problems using both the ‘programming concepts’ and the ‘power of MATLAB’ side by side. In-depth coverage is given to input/output, a topic that is fundamental to many engineering applications. New to this edition are more engineering applications to help the reader learn MATLAB in the context of solving technical problems, new and revised end-of-chapter problems, stronger coverage of loops and vectorizing in a new chapter, and updates to reflect current features and functions of the current release of MATLAB."—Lunar and Planetary Information Bulletin, December 2011, Issue 127, page 46 "This is the perfect book for anyone wanted to acquire a secure understanding of MATLAB fundaments and master its language. Many engineers and scientists now use MATLAB and Simulink to solve real-world problems. With the help of this book, they will be able to exploit the full power of MATLAB much sooner than they would using the online manuals, and be able to solve real problems much more quickly."—IEEE Electrical Insulation Magazine, page 70

Product Details

ISBN-13:
9780123850829
Publisher:
Elsevier Science
Publication date:
06/30/2011
Sold by:
Barnes & Noble
Format:
NOOK Book
Pages:
544
File size:
4 MB

Read an Excerpt

MATLAB

A Practical Introduction to Programming and Problem Solving
By Stormy Attaway

Butterworth-Heinemann

Copyright © 2012 Elsevier Inc.
All right reserved.

ISBN: 978-0-12-385082-9


Chapter One

Introduction to MATLAB

CONTENTS

1.1 Getting into MATLAB 4 1.2 The MATLAB Desktop Environment 5 1.3 Variables and Assignment Statements 6 1.4 Expressions 10 1.5 Characters and Encoding 19 1.6 Vectors and Matrices 21

KEY TERMS

prompt programs script files variables assignment statement assignment operator user initializing incrementing decrementing identifier names reserved words key words mnemonic default unary operand binary scientific notation exponential notation precedence associativity nesting call a function arguments returning values constants types classes double precision floating point unsigned characters strings type casting saturation arithmetic random numbers seed pseudorandom character encoding character set vectors matrices row vector column vector scalar elements array array operations iterate step value concatenating index subscript index vector transposing subscripted indexing unwinding a matrix linear indexing vector of variables empty vector deleting elements three-dimensional matrices

MATLAB® is a very powerful software package that has many built-in tools for solving problems and developing graphical illustrations. The simplest method for using the MATLAB product is interactively; an expression is entered by the user and MATLAB immediately responds with a result. It is also possible to write scripts and programs in MATLAB, which are essentially groups of commands that are executed sequentially.

This chapter will focus on the basics, including many operators and built-in functions that can be used in interactive expressions. Means of storing values, including vectors and matrices, will also be introduced.

1.1 GETTING INTO MATLAB

MATLAB is a mathematical and graphical software package with numerical, graphical, and programming capabilities. It has built-in functions to perform many operations, and there are toolboxes that can be added to augment these functions (e.g., for signal processing). There are versions available for different hardware platforms, in both professional and student editions.

When the MATLAB software is started, a window opens in which the main part is the Command Window (see Figure 1.1). In the Command Window, you should see:

>>

The >> is called the prompt. In the Student Edition, the prompt instead is:

EDU >>

In the Command Window, MATLAB can be used interactively. At the prompt, any MATLAB command or expression can be entered, and MATLAB will immediately respond with the result.

It is also possible to write programs in MATLAB that are contained in script files or M-files. Programs will be introduced in Chapter 2.

The following commands can serve as an introduction to MATLAB and allow you to get help:

* info will display contact information for the product.

* demo has demos of some of the features of MATLAB.

* help will explain any command; help help will explain how help works.

* helpbrowser opens a Help Window.

* lookfor searches through the help for a specific word or phrase. (Note: This can take a long time.)

To get out of MATLAB, either type quit at the prompt, or choose File, then Exit MATLAB from the menu.

1.2 THE MATLAB DESKTOP ENVIRONMENT

In addition to the Command Window, there are several other windows that can be opened and may be opened by default. What is described here is the default layout for these windows in Version R2011a, although there are other possible configurations. Different versions of MATLAB may show other configurations by default, and the layout can always be customized. Therefore, the main features will be briefly described here.

Directly above the Command Window, there is a pull-down menu for the Current Folder. The folder that is set as the Current Folder is where files will be saved.

To the right of the Command Window is the Workspace Window on top and the Command History Window on the bottom. The Command History Window shows commands that have been entered, not just in the current session (in the current Command Window), but previously as well. (The Workspace Window will be described in the next section.) To the left of the Command Window is the Current Folder Window. This shows the files that are stored in the Current Folder. These can be grouped by type, and sorted by name. If a file is selected, information about that file is shown on the bottom.

This default configuration can be altered by clicking on Desktop, or using the icons at the top right corner of each window. These include an "x" that will close a particular window, and a curled arrow that in its initial state pointing to the upper right allows one to undock that window. Once undocked, clicking on the curled arrow pointing to the lower right will dock the window again.

1.3 VARIABLES AND ASSIGNMENT STATEMENTS

To store a value in a MATLAB session, or in a program, a variable is used. The Workspace Window shows variables that have been created. One easy way to create a variable is to use an assignment statement. The format of an assignment statement is

variablename = expression

The variable is always on the left, followed by the = symbol, which is the assignment operator (unlike in mathematics, the single equal sign does not mean equality), followed by an expression. The expression is evaluated and then that value is stored in the variable. For example, this is the way it would appear in the Command Window:

>> mynum = 6 mynum = 6 >>

Here, the user (the person working in MATLAB) typed "mynum = 6" at the prompt, and MATLAB stored the integer 6 in the variable called mynum, and then displayed the result followed by the prompt again. Since the equal sign is the assignment operator, and does not mean equality, the statement should be read as "mynum gets the value of 6" (not "mynum equals 6").

Note that the variable name must always be on the left, and the expression on the right. An error will occur if these are reversed.

>> 6 = mynum ??? 6 = mynum

Error: The expression to the left of the equals sign is not a valid target for an assignment.

>>

Putting a semicolon at the end of a statement suppresses the output. For example,

>> res = 9 - 2; >>

This would assign the result of the expression on the right side the value 7 to the variable res; it just doesn't show that result. Instead, another prompt appears immediately. However, at this point in the Workspace Window the variables mynum and res and their values can be seen.

The spaces in a statement or expression do not affect the result, but make it easier to read. The following statement, which has no spaces, would accomplish exactly the same thing as the previous statement:

>> res=9-2;

MATLAB uses a default variable named ans if an expression is typed at the prompt and it is not assigned to a variable. For example, the result of the expression 6 + 3 is stored in the variable ans.

>> 6 + 3 ans = 9

This default variable is reused any time just an expression is typed at the prompt.

A shortcut for retyping commands is to hit the up arrow [up arrow], which will go back to the previously typed command(s). For example, if you decided to assign the result of the expression 6 + 3 to the variable "result" instead of using the default ans, you could hit the up arrow and then the left arrow to modify the command rather than retyping the entire statement.

>> result = 6 + 3 result = 9

This is very useful, especially if a long expression is entered with an error, and it is desired to go back to correct it.

To change a variable, another assignment statement can be used, which assigns the value of a different expression to it. Consider, for example, the following sequence of statements:

>> mynum = 3 mynum = 3 >> mynum = 4 + 2 mynum = 6

>> mynum = mynum + 1 mynum = 7

In the first assignment statement, the value 3 is assigned to the variable mynum. In the next assignment statement, mynum is changed to have the value of the expression 4 + 2, or 6. In the third assignment statement, mynum is changed again, to the result of the expression mynum + 1. Since at that time mynum had the value 6, the value of the expression was 6 + 1, or 7.

At that point, if the expression mynum + 3 is entered, the default variable ans is used since the result of this expression is not assigned to a variable. Thus, the value of ans becomes 10 but mynum is unchanged (it is still 7). Note that just typing the name of a variable will display its value.

>> mynum + 3 ans = 10

>> mynum mynum = 7

1.3.1 Initializing, incrementing, and decrementing

Frequently, values of variables change. Putting the first or initial value in a variable is called initializing the variable.

Adding to a variable is called incrementing. For example, the statement

mynum = mynum + 1

increments the variable mynum by 1.

1.3.2 Variable names

Variable names are an example of identifier names. We will see other examples of identifier names, such as file names, in future chapters. The rules for identifier names are:

* The name must begin with a letter of the alphabet. After that, the name can contain letters, digits, and the underscore character (e.g., value_1), but it cannot have a space.

* There is a limit to the length of the name; the built-in function name-lengthmax tells what this maximum length is.

* MATLAB is case-sensitive, which means there is a difference between upper- and lowercase letters. So, variables called mynum, MYNUM, and Mynum are all different (although this would be confusing and should not be done).

* Although underscore characters are valid in a name, their use can cause problems with some programs that interact with MATLAB, so some programmers use mixed case instead (e.g., partWeights instead of part_weights)

* There are certain words called reserved words, or key words, that cannot be used as variable names.

* Names of built-in functions can be but should not be used as variable names.

Additionally, variable names should always be mnemonic, which means that they should make some sense. For example, if the variable is storing the radius of a circle, a name such as radius would make sense; x probably wouldn't.

The Workspace Window shows the variables that have been created in the current Command Window and their values.

The following commands relate to variables:

* who shows variables that have been defined in this Command Window (this just shows the names of the variables)

* whos shows variables that have been defined in this Command Window (this shows more information on the variables, similar to what is in the Workspace Window)

* clear clears out all variables so they no longer exist

* clear variablename clears out a particular variable

* clear variablename1 variablename2 ... clears out a list of variables (note: separate the names with spaces)

If nothing appears when who or whos is entered, that means there aren't any variables! For example, in the beginning of a MATLAB session, variables could be created and then selectively cleared (remember that the semicolon suppresses output).

>> who >> mynum = 3; >> mynum + 5;

>> who Your variables are: ans mynum

>> clear mynum >> who Your variables are: ans

(Continues...)



Excerpted from MATLAB by Stormy Attaway Copyright © 2012 by Elsevier Inc.. Excerpted by permission of Butterworth-Heinemann. All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.

Meet the Author

Stormy Attaway is Assistant Professor and Faculty Coordinator for Special Programs within the Mechanical Engineering Department at Boston University. She also currently serves as Director of Curricular Assessment and Improvement for the College of Engineering. In this capacity, she coordinates the curricular assessment and improvement programs within the College, in order to better the academic experience of the undergraduates. She has been the course coordinator for the Engineering Computation courses at Boston University for over twenty years, and has taught a variety of programming courses using many different languages and software packages.

Customer Reviews

Average Review:

Write a Review

and post it to your social network

     

Most Helpful Customer Reviews

See all customer reviews >

Matlab: A Practical Introduction to Programming and Problem Solving 3.5 out of 5 based on 0 ratings. 2 reviews.
Anonymous More than 1 year ago
Anonymous More than 1 year ago