Matlab: A Practical Introduction to Programming and Problem Solving
Stormy Attaway
Language: English
Pages: 560
ISBN: 0124058760
Format: PDF / Kindle (mobi) / ePub
MATLAB has become the standard software tool for solving scientific and engineering problems due to its powerful built-in functions and its ability to program. Assuming no knowledge of programming, this book guides the reader through both programming and built-in functions to easily exploit MATLAB's extensive capabilities for tackling engineering problems.
The book starts with programming concepts, such as variables, assignments, and selection statements, moves on to loops, and then solves problems using both the programming concept and the power of MATLAB. In-depth coverage is given to input/output, a topic fundamental to many engineering applications.
The third edition of MATLAB: A Practical Introduction to Programming and Problem Solving has been updated to reflect the functionality of the current version of MATLAB. It features new and revised end-of-chapter exercises, stronger coverage of loops and vectorizing, and more engineering applications to help the reader learn this software tool in context.
- Presents programming concepts and MATLAB built-in functions side-by-side
- 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
third elements of v2: >> v2(index2) ans =3 5 The function ismember receives two vectors as input arguments, and returns a logical vector that is the same length as the first argument, containing 1 for true if the element in the first vector is also in the second, or 0 for false if not. The order of the arguments matters for this function. >> v1 v1 = >> v2 v2 = >> ismember(v1,v2) ans = >> ismember(v2,v1) ans = Using the result from the ismember function as an index
by both the vertices and the faces of the polygon that connect these vertices. One way of calling this function ispatch(fv), where fv is a structure variable with fields called vertices and faces. polyhedron.vertices = [. . . 0 0 0 1 0 0 0 1 0 0.5 0.5 1]; polyhedron.faces = [. . . 1 2 3 1 2 4 1 3 4 2 3 4]; pobj = patch(polyhedron, . . . ‘FaceColor’,[0.8, 0.8, 0.8],. . . ‘EdgeColor’,‘black’); The polyhedron.vertices field is a matrix in which each row represents (x,y,z) points. The
either a ‘y’ or ‘n’ in response to a prompt. The user’s input is read into a character variable called letter. The script will print "OK, continuing" if the user enters either a ‘y’ or ‘Y’ or it will print "OK, halting" if the user enters an ‘n’ or ‘N’, or it will print "Error" if the user enters anything else. Put this statement in the script first:letter = input(‘Enter your answer:’, ‘s’); Write the script using a single nested if-else statement (elseif clause is permitted). 12 Write the
string of any length.’) outstr = input(‘Enter the string here:’, ‘s’); >> mystring = stringprompt When prompted, enter a string of any length. Enter the string here: Hi there mystring = Hi there Practice 5.3 Write a function that will prompt the user for a positive number, loop to error-check to make sure that the number is positive, and return the positive number. Quick Question! It is important that the number of arguments in the call to a function be the same as the
incremented and the value is printed. With the second function, however, when the function exits, the variable remains with its value, so the next time the function is called the variable is incremented again. persistex.m % This script demonstrates persistent variables % The first function has a variable count fprintf(‘This is what happens with a normal variable:\n’) func1 func1 % The second fn has a persistent variable count fprintf(‘\nThis is what happens with a persistent