MATLAB AN INTRODUCTION WITH APPLICATIONS 4TH EDITION PDF
MATLAB®. An Introduction with Applications. Fourth Edition. Amos Gilat. Department of Mechanical Engineering. The Ohio State University. JOHN WILEY . The Nutrition Society Textbook Series. Introduction to Human Nutrition. Introduction to Human Nutrition: a global perspe. MATLAB ® An Introduction with Applications Fourth Edition Amos Gilat Department of Mechanical Engineering The Ohio State University JOHN WILEY & SONS.
|Language:||English, Spanish, Japanese|
|ePub File Size:||22.36 MB|
|PDF File Size:||20.29 MB|
|Distribution:||Free* [*Regsitration Required]|
By Amos Gilat Matlab An Introduction With Applications Fourth 4th Edition - [Free] By Introduction with myavr.info - Sat, 06 Apr GMT. Gilat Matlab An Introduction With Applications 4th Edition (FREE) Wai Yan Htet Aung. myavr.info - Sat, 06 Apr GMT Homework Help and. Matlab: An Introduction With Applications - Third Edition an introduction with applications third edition amos gilat department of mechanical engineering.
An Introduction with Applications, Fourth Edition. Amos Gilat. Amos Gilat This book was printed and bound by Malloy Lithographers. The cover was printed by Malloy Lithographers. This book is printed on acid free paper. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections or of the United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, Inc.
These copies are licensed and may not be sold or transferred to a third party. Upon completion of the review period, please return the evaluation copy to Wiley. Return instructions and a free of charge return shipping label are available at www. Outside of the United States, please contact your local representative. The software is popular because it is powerful and easy to use. For university freshmen in it can be thought of as the next tool to use after the graphic calculator in high school.
This book was written following several years of teaching the software to freshmen in an introductory engineering course. The objective was to write a book that teaches the software in a friendly, non-intimidating fashion.
Therefore, the book is written in simple and direct language. In many places bullets, rather than lengthy text, are used to list facts and details that are related to a specific topic. The book includes numerous sample problems in mathematics, science, and engineering that are similar to problems encountered by new users of MATLAB. The last two chapters are 3D plotting now Chapter 10 and symbolic math Chapter In addition, the end of chapter problems have been revised. In addition, the problems cover a wider range of topics.
I would like to thank several of my colleagues at The Ohio State University. Mike Parke read sections of the book and suggested modifications.
Special thanks go to Professor Mike Lichtensteiger OSU , and my daughter Tal Gilat Marquette University , who carefully reviewed the first edition of the book and provided valuable comments and criticisms.
Professor Brian Harper OSU has made a significant contribution to the new end of chapter problems in the present edition. I would like to express my appreciation to all those who have reviewed the first edition of the text at its various stages of development, including Betty Barr, University of Houston; Andrei G. MATLAB can be used for math computations, modeling and simulations, data analysis and processing, visualization and graphics, and algorithm development.
MATLAB is widely used in universities and colleges in introductory and advanced courses in mathematics, science, and especially engineering. In industry the software is used in research, development, and design.
In addition, MATLAB has optional toolboxes that are collections of specialized programs designed to solve specific types of problems. Examples include toolboxes for signal processing, symbolic calculations, and control systems.
Consequently, the majority of the literature that has been written about MATLAB assumes that the reader has knowledge of computer programming. Today, however, MATLAB is being introduced to college students as the first and often the only computer program they will learn. For these students there is a need for a book that teaches MATLAB assuming no prior experience in computer programming.
An Introduction with Applications is intended for students who are using MATLAB for the first time and have little or no experience in computer programming. The book can also serve as a reference in more advanced science and engineering courses where MATLAB is used as a tool for solving problems. In addition, the book can be a supplement or a secondary book in courses where MATLAB is used but the instructor does not have the time to cover it extensively.
The 1 2 Introduction assumption is that once these foundations are well understood, the student will be able to learn advanced topics easily by using the information in the Help menu. The order in which the topics are presented in this book was chosen carefully, based on several years of experience in teaching MATLAB in an introductory engineering course.
The topics are presented in an order that allows the student to follow the book chapter after chapter. Every topic is presented completely in one place and then used in the following chapters.
The first chapter describes the basic structure and features of MATLAB and how to use the program for simple arithmetic operations with scalars as with a calculator. Script files are introduced at the end of the chapter. The next two chapters are devoted to the topic of arrays. This concept, which makes MATLAB a very powerful program, can be a little difficult to grasp for students who have only limited knowledge of and experience with linear algebra and vector analysis.
The concept of arrays is introduced gradually and then explained in extensive detail. Chapter 2 describes how to create arrays, and Chapter 3 covers mathematical operations with arrays. Following the basics, more advanced topics that are related to script files and input and output of data are presented in Chapter 4.
This is followed by coverage of two-dimensional plotting in Chapter 5. This includes flow control with conditional statements and loops. User-defined functions, anonymous functions, and function functions are covered next in Chapter 7. The coverage of function files user-defined functions is intentionally separated from the subject of script files.
This has proven to be easier to understand by students who are not familiar with similar concepts from other computer programs. The next three chapters cover more advanced topics. It includes solving nonlinear equations, finding minimum or a maximum of a function, numerical integration, and solution of first-order ordinary differential equations.
Chapter 10 describes how to produce three-dimensional plots, an extension of the chapter on twodimensional plots. The Framework of a Typical Chapter In every chapter the topics are introduced gradually in an order that makes the concepts easy to understand. Some of the longer examples in Chapters 1—3 are titled as tutorials. Additional explanations appear in boxed text with a white background.
In addition, every chapter includes formal sample problems that are examples of applications of MATLAB for solving problems in math, science, and engineering.
Each example includes a problem statement and a detailed solution. Some sample problems are presented in the middle of the chapter. All of the chapters except Chapter 2 have a section at the end with several sample problems of applications.
The solutions of the sample problems are written such that they are easy to follow. The students are encouraged to try to write their own solutions and compare the end results. At the end of each chapter there is a set of homework problems. They include general problems from math and science and problems from different disciplines of engineering. Symbolic math operations, however, can be executed if the Symbolic Math toolbox is installed. The Symbolic Math toolbox is included in the student version of the software and can be added to the standard program.
It should be emphasized, however, that the book covers the basics of MATLAB, which do not change much from version to version. It is assumed that the software is installed on the computer, and the user has basic knowledge of operating the computer.
The Order of Topics in the Book It is probably impossible to write a textbook where all the subjects are presented in an order that is suitable for everyone. The order of topics in this book is such that the fundamentals of MATLAB are covered first arrays and array operations , and, as mentioned before, every topic is covered completely in one location, which makes the book easy to use as a reference.
The order of the topics in this fourth edition of the book is a little bit different than in previous editions. Programming is introduced before user-defined functions. This allows using programming in user-defined functions.
Also, applications of MATLAB in numerical analysis now Chapter 9, previously 10 follow Chapter 8 which covers polynomials, curve fitting, and interpolation. Next, the Command Window is introduced in detail. This chapter shows how to use MATLAB for arithmetic operations with scalars in a fashion similar to the way that a calculator is used. This includes the use of elementary math functions with scalars.
The chapter then shows how to define scalar variables the assignment operator and how to use these variables in arithmetic calculations. The last section in the chapter introduces script files.
The window contains four smaller windows: A list of several windows and their purpose is given in Table Four of the windows—the Command Window, the Figure Window, the Editor Window, and the Help Window—are used extensively throughout the book and are briefly described on the following pages.
More detailed descriptions are included in the chapters where they are used. Command Window: It is convenient to have the Command Window as the only visible window, and this can be done by either closing all the other windows click on the x at the top right-hand side of the window you want to close or by first selecting the Desktop Layout in the Desktop menu, and then 5 6 Chapter 1: Working in the Command Window is described in detail in Section 1.
Figure Table Figure Window Contains output commands. Editor Window Creates and function files.
Matlab An Introduction With Applications 4th Edition Solutions Manual
Help Window Provides help information. Workspace Window Provides information variables that are used. Current Folder Window Shows the files in the current folder.
The Figure Window opens automatically when graphics commands are executed, and contains graphs created by these commands. An example of a Figure Window is shown in Figure A more detailed description of this window is given in Chapter 5. Example of a Figure Window. Editor Window: The Editor Window is used for writing and editing programs. This window is opened from the File menu. An example of an Editor Window is shown in Figure More details on the Editor Window are given in Section 1.
Example of an Editor Window. Help Window: The Help Window contains help information. Figure shows an open Help Window. The Help Window.
For most beginners it is probably more convenient to close all the windows except the Command Window. Each of the windows can be closed by clicking on the button.
The closed windows can be reopened by selecting them from the Desktop menu. The windows shown in Figure can be displayed by selecting first Desktop Layout in the Desktop menu and then Default from the submenu.
The various windows in Figure are docked to the desktop. A window can be undocked become a separate, independent window by clicking on the button on the upper right-hand corner. An independent window can be redocked by clicking on the button. An example of the Command Window, with several simple commands that will be explained later in this chapter, is shown in Figure The Command Window. Notes for working in the Command Window: However, only the last command is executed. Everything executed previously that might be still displayed is unchanged.
This is done by typing a comma between the commands. When the Enter key is pressed the commands are executed in order from left to right. When the command is displayed at the command prompt, it can be modified if needed and then executed.
The down-arrow key can be used to move down the list of previously typed commands. The continuation of the command is then typed in the new line. The command can continue line after line up to a total of 4, characters. When a command is typed in the Command Window and the Enter key is pressed, the command is executed. Any output that the command generates is displayed in the Command Window. If a semicolon ; is typed at the end of a command the output of the command is not displayed.
Typing a semicolon is useful when the result is obvious or known, or when the output is very large. If several commands are typed in the same line, the output from any of the commands will not be displayed if a semicolon is typed between the commands instead of a comma. This means that when the Enter key is pressed the line is not executed. This has no effect on the execution of the command.
Usually there is no need for comments in the Command Window. Comments, however, are frequently used in a program to add descriptions or to explain the program see Chapters 4 and 6. The clc command: The clc command type clc and press Enter clears the Command Window.
After working in the Command Window for a while, the display may become very long. Once the clc command is executed a clear window is displayed. The command does not change anything that was done before. For example, if some variables were defined previously see Section 1. The up-arrow key can also be used to recall commands that were typed before. The Command History Window: This includes commands from previous sessions.
By double-clicking on the command, the command is reentered in the Command Window and executed. It is also possible to drag the command to the Command Window, make changes if needed, and then execute it. The list in the Command History Window can be cleared by selecting the lines to be deleted and then selecting Delete Selection from the Edit menu or right-click the mouse when the lines are selected and then choose Delete Selection in the menu that opens.
As will be explained later in the chapter, numbers can be used in arithmetic calculations directly as with a calculator or they can be assigned to variables, which can subsequently be used in calculations. The symbols of arithmetic opera- 11 1. For scalars, the left division is the inverse of the right division. The left division, however, is mostly used for operations with arrays, which are discussed in Chapter 3.
This order is the same as used in most calculators. Precedence Mathematical Operation First Parentheses. For nested parentheses, the innermost are executed first. Second Exponentiation.
MATLAB: An Introduction with Applications, 3rd Edition
Third Multiplication, division equal precedence. Fourth Addition and subtraction. In an expression that has several operations, higher-precedence operations are executed before lower-precedence operations. If two or more operations have the same precedence, the expression is executed from left to right. As illustrated in the next section, parentheses can be used to change the order of calculations. This is done in the Command Window by typing a mathematical expression and pressing the Enter key.
This is demonstrated in Tutorial In Tutorial , the output format is fixed-point with four decimal digits called short , which is the default format for numerical values. The format can be changed with the format command. Once the format command is entered, all the output that follows is displayed in the specified format.
Several of the available formats are listed and described in Table Details of these formats can be obtained by typing help format in the Command Window. Display formats Command Description Example format short Fixed-point with 4 decimal digits for: A function has a name and an argument in parentheses.
For example, the function that calculates the square root of a number is sqrt x. Its name is sqrt, and the argument is x. When the function is used, the argument can be a number, a variable that has been assigned a numerical value explained in Section 1.
Functions can also be included in arguments, as well as in expressions.
Tutorial shows examples 14 Chapter 1: Tutorial Using the sqrt built-in function. Argument is a number. A complete list of functions organized by category can be found in the Help Window. Elementary math functions Function Description Example sqrt x Square root. If x is negative n must be an odd integer. Base e logarithm ln. Elementary math functions Continued Function Description Example factorial x The factorial function x!
Trigonometric math functions Function Description Example sin x sind x Sine of angle x x in radians. Sine of angle x x in degrees. Cosine of angle x x in degrees. Tangent of angle x x in degrees. Cotangent of angle x x in degrees. The hyperbolic trigonometric functions are sinh x , cosh x , tanh x , and coth x. Rounding functions Function Description Example round x Round to the nearest integer. Once a variable is assigned a numerical value, it can be used in mathematical expressions, in functions, and in any MATLAB statements and commands.
A variable is actually a name of a memory location. When the variable is used the stored data is used. If the variable is assigned a new value the content of the memory location is replaced. In Chapter 1 we consider only variables that are assigned numerical values that are scalars. Assigning and addressing variables that are arrays is discussed in Chapter 2.
The assignment operator assigns a value to a variable. When the Enter key is pressed the numerical value of the right-hand side is assigned to the variable, and MATLAB displays the variable and its assigned value in the next two lines.
The following shows how the assignment operator works. A new value is assigned to x. The new value is 3 times the previous value of x minus The use of previously defined variables to define a new variable is demonstrated next. Assign 12 to a. As an example, the last demonstration is repeated below using semicolons. The value of the variable C is displayed by typing the name of the variable. The assignments must be separated with a comma spaces can be added after the comma.
When the Enter key is pressed, the assignments are executed from left to right and the variables and their assignments are displayed. A variable is not displayed if a semicolon is typed instead of a comma.
For example, the assignments of the variables a, B, and C above can all be done in the same line. For example: A value of 72 is assigned to the variable ABB. For example, AA, Aa, aA, and aa are the names of four different variables. Once a function name is used to define a variable, the function cannot be used. These words are: An error message is displayed if the user tries to use a keyword as a variable name.
The keywords can be displayed by typing the command iskeyword. Some of the predefined variables are: NaN Stands for Not-a-Number. The predefined variables can be redefined to have any other value. The variables pi, eps, and inf, are usually not redefined since they are frequently used in many applications.
Other predefined variables, such as i and j, are sometime redefined commonly in association with loops when complex numbers are not involved in the application. When these commands are typed in the Command Window and the Enter key is pressed, either they provide information, or they perform a task as specified below. Command Outcome clear Removes all variables from the memory.
Although every MATLAB command can be executed in this way, using the Command Window to execute a series of commands—especially if they are related to each other a program —is not convenient and may be difficult or even impossible.
The commands in the Command Window cannot be saved and executed again. In addition, the Command Window is not interactive. This means that every time the Enter key is pressed only the last command is executed, and everything executed before is unchanged.
If a change or a correction is needed in a command that was previously executed and the results of this command are used in commands that follow, all the commands have to be entered and executed again. A different better way of executing commands with MATLAB is first to create a file with a list of commands program , save it, and then run execute the file. When the file runs, the commands it contains are executed in the order that they are listed.
If needed, the commands in the file can be corrected or changed and the file can be saved and run again. Files that are used for this purpose are called script files. This section covers only the minimum that is required in order to run simple programs. This will allow the student to use script files when practicing the material that is presented in this and the next two chapters instead of typing repeatedly in the Command Window.
Script files are considered again in Chapter 4 where many additional topics that are essential for understanding MATLAB and writing programs in script file are covered. This window is opened from the Command Window. In the File menu, select New, and then select Script. Line number The commands in the script file are typed line by line.
The lines are numbered automatically. A new line starts when the Enter key is pressed. Once the window is open, the commands of the script file are typed line by line. Define three variables. Calculating the two roots. The Run icon. This is done by choosing Save As The rules for naming a script file follow the rules of naming a variable must begin with a letter, can include digits and underscore, no spaces, and up to 63 characters long.
The names of user-defined variables, predefined variables, and MATLAB commands or functions should not be used as names of script files. The file will be executed if the folder where the file is saved is the current folder of MATLAB or if the folder is listed in the search path, as explained next.
If an attempt is made to execute a script file by clicking on the Run icon in the Editor Window when the current folder is not the folder where the script file is saved, then the prompt shown in The current folder is shown here. The Current folder field in the Command Window. Figure will open. The user can then change the current folder to the folder where the script file is saved, or add it to the search path.
Once two or more different current folders are used in a session, it is possible to switch from one to another in the Current Folder field in the Command Window. The current folder can also be changed in the Current Folder Window, shown in Figure , which can be opened from the Desktop menu. The Current Folder can be changed by choosing the drive and folder where the file is saved.
Changing the current directory. Current folder shown here. Click here to change the folder. Click here to browse for a folder.
MATLAB An Introduction with Applications (4th edition)
Click here to go up one level in the file system. The Current Folder Window. An alternative simple way to change the current folder is to use the cd command in the Command Window. To change the current folder to a different drive, type cd, space, and then the name of the directory followed by a colon: For example, to change the current folder to drive F e. If the script file is saved in a folder within a drive, the path to that folder has to be specified. This is done by typing the path as a string in the cd command.
For example, cd 'F: The following example shows how the current folder is changed to be drive E. Then the script file from Figure , which was saved in drive E as ProgramExample. The script file is executed by typing the name of the file and pressing the Enter key. The output generated by the script file the roots x1 and x2 is displayed in the Command Window. Trigonometric identity A trigonometric identity is given by: Define x. Geometry and trigonometry Four circles are placed as shown in the figure.
At each point where two circles are in contact they are tangent to each other. Determine the distance between the centers C2 and C4. The radii of the circles are: Solution The lines that connect the centers of the circles create four triangles.
The problem is solved by writing the following program in a script file: When the script file is executed, the following the value of the variable C2C4 is displayed in the Command Window: Determine, to the nearest degree, the temperature of the can after three hours. Solution The problem is solved by typing the following commands in the Command Window.
Compounded interest The balance B of a savings account after t years when a principal P is invested at an annual interest rate r and the interest is compounded n times a year is given by: In both accounts the interest rate is 8. Use MATLAB to determine how long in years and months it would take for the balance in the second account to be the same as the balance of the first account after 17 years. Solution Follow these steps: Calculate B from Eq.
Step b: Solve Eq. Step c: Determine the number of years. Determine the number of months. Define the variables a, b, c, and d as: A cube has a side of 18 cm. The perimeter P of an ellipse with semi-minor axes a and a b 1 b is given approximately by: Determine a and b.
Two trigonometric identities are given by: Define two variables: Using these variables, show that the following trigonometric identity is correct by calculating the values of the left and right sides of the equation. Define a, b, and c as variables, and then: Law of Cosines: Define a and c as variables, and then: First define the variables A, B, C, D, x0, y0, and z0, and then calculate d.
Use the abs and sqrt functions. The arc length s of the parabolic segment BOC is given by: Oranges are packed such that 52 are placed in each box. Determine how many boxes are needed to pack 4, oranges. The voltage difference V ab between points a and b in the Wheatstone bridge circuit is: Assign the prices to variables named oak and pine, change the display format to bank, and calculate the following by typing one command: The resonant frequency f in Hz for the circuit shown is given by: The number of combinations C n, r of taking r objects out of n objects is given by: A deck of poker cards has 52 different cards.
Determine how many different combinations are possible for selecting 5 cards from the deck. Use the builtin function factorial. The formula for changing the base of a logarithm is: The current I in amps t seconds after closing the switch in the circuit shown is: Radioactive decay of carbon is used for estimating the age of organic material.
Carbon has a half-life of approximately 5, years. A sample of paper taken from the Dead Sea Scrolls shows that Determine the estimated age of the scrolls.
Solve the problem by writing a program in a script file. Fractions can be added by using the smallest common denominator.
Then use the function to show that the least common multiple of: The monthly payment M of a loan amount P for y years and interest rate r can be calculated by the formula: Define the variables P, r, and y and use them to calculate M. Use MATLAB to determine how many fewer days it will take to earn the same if the money is invested in an account where the interest is compounded continuously. The temperature dependence of vapor pressure p can be estimated by the Anteing equation: Calculate the vapor pressure of toluene at and K.
Sound level L P in units of decibels dB is determined by: Determine its sound level in decibels. By how many times is the sound pressure of the jet engine larger louder than the sound of the passing car? Use the Help Window to find a display format that displays the output as a ratio of integers.
For example, the number 3. Change the display to this format and execute the following operations: The steady-state heat conduction q from a cylindrical solid wall is determined by: Stirling's approximation for large factorials is given by: The simplest array one-dimensional is a row or a column of numbers.
A more complex array twodimensional is a collection of numbers arranged in rows and columns. One use of arrays is to store information and data, as in a table. In science and engineering, one-dimensional arrays frequently represent vectors, and two-dimensional arrays often represent matrices. This chapter shows how to create and address arrays, and Chapter 3 shows how to use arrays in mathematical operations.
Strings are discussed in Section 2. One example is the representation of the position of a point in space in a three-dimensional Cartesian coordinate system. As shown in Figure , the position of point A is defined by a list of the three numbers 2, 4, and 5, which are the coordinates of the point. The position of point A can be z expressed in terms of a position vector: The numbers 2, 4, and 5 can be used to define a row or a column vector.
For example, Table con2 tains population growth data that can be Figure Position of a point. Each list can be entered as elements in a vector with the numbers placed in a row or in a column.
Creating Arrays Table Population data Year Population millions In MATLAB, a vector is created by assigning the elements of the vector to a variable. This can be done in several ways depending on the source of the information that is used for the elements of the vector. When a vector contains specific numbers that are known like the coordinates of point A , the value of each element is entered directly.
Each element can also be a mathematical expression that can include predefined variables, numbers, and functions. Often, the elements of a row vector are a series of numbers with constant spacing. A vector can also be created as the result of mathematical operations as explained in Chapter 3. Creating a vector from a known list of numbers: The vector is created by typing the elements numbers inside square brackets [ ]. To create a row vector type the elements with a space or a comma between the elements inside the square brackets.
Column vector: To create a column vector type the left square bracket [ and then enter the elements with a semicolon between them, or press the Enter key after each element.
Type the right square bracket ] after the last element.
Proof of Heaven: A Neurosurgeon's Journey into the Afterlife
Tutorial shows how the data from Table and the coordinates of point A are used to create row and column vectors. Creating vectors from given data. The Enter key is pressed after each element is typed. In a vector with constant spacing the difference between the elements is the same. A vector in which the first term is m, the spacing is q, and the last term is n is created by typing: Some examples are: First element 1, spacing 2, last element If spacing is omitted, the default is 1.
Creating a vector with linear equal spacing by specifying the first and last terms, and the number of terms: A vector with n elements that are linearly equally spaced in which the first element is xi and the last element is xf can be created by typing the linspace command MATLAB determines the correct spacing: Columns 1 through 10 Columns 91 through 4.
Matrices can be used to store information like the arrangement in a table. Matrices play an important role in linear algebra and are used in science and engineering to describe many physical quantities. In a square matrix the number of rows and the number of columns is equal. In general, the number of rows and columns can be different. For example, the matrix: A matrix is created by assigning the elements of the matrix to a variable.
This is done by typing the elements, row by row, inside square brackets [ ]. First type the left bracket [ then type the first row, separating the elements with spaces or commas. To type the next row type a semicolon or press Enter. Type the right bracket ] at the end of the last row. All the rows must have the same number of elements. If an element is zero, it has to be entered as such. Examples of matrices defined in different ways are shown in Tutorial Creating matrices.
The Enter key is pressed before a new line is entered. Creating Arrays Tutorial The zeros m,n and the ones m,n commands create a matrix with m rows and n columns in which all elements are the numbers 0 and 1, respectively. The eye n command creates a square matrix with n rows and n columns in which the diagonal elements are equal to 1 and the rest of the elements are 0.
This matrix is called the identity matrix. Examples are: This topic is covered in Chapter 3. A scalar is an array with one element, a vector is an array with one row or one column of elements, and a matrix is an array with elements in rows and columns. There is no need to define the size of the array single element for a scalar, a row or a column of elements for a vector, or a two-dimensional array of elements for a matrix before the elements are assigned.
For example, a scalar can be changed to a vector or a matrix; a vector can be changed to a scalar, a vector of different length, or a matrix; and a matrix can be changed to have a different size, or be reduced to a vector or a scalar.
These changes are made by adding or deleting elements.
This subject is covered in Sections 2. When applied to a matrix, it switches the rows columns to columns rows.
MATLAB An Introduction with Applications (4th edition)
D has 4 rows and 3 columns. This is useful when there is a need to redefine only some of the elements, when specific elements are to be used in calculations, or when a subgroup of the elements is used to define a new variable. For a vector named ve, ve k refers to the element in position k. The first position is 1. For example, if the vector ve has nine elements: A single vector element, v k , can be used just as a variable.
For example, it is possible to change the value of only one element of a vector by assigning a new value to a specific address. This is done by typing: A single element can also be used as a variable in a mathematical expression.
For a matrix assigned to a variable ma, ma k,p refers to the element in row k and column p. For example, if the matrix is: As with vectors, it is possible to change the value of just one element of a matrix by assigning a new value to that element. Also, single elements can be used like variables in mathematical expressions and functions. Creating Arrays 2. For a vector: Refers to all the elements of the vector va either a row or a column vector.
Refers to the elements in all the columns of row n of the matrix A. Refers to the elements in all the columns between rows m and n of the matrix A. The use of the colon symbol in addressing elements of matrices is demonstrated in Tutorial Using a colon in addressing arrays. It is possible, however, to select only specific elements, or specific rows and columns of existing variables to create new variables. This is done by typing the selected elements or rows or columns inside brackets, as shown below: A vector a matrix with a single row or column can be changed to have more elements, or it can be changed to be a two-dimensional matrix.
The addition of elements can be done by simply assigning values to the additional elements, or by appending existing variables. Adding elements to a vector: Elements can be added to an existing vector by assigning values to the new elements.
For example, if a vector has 4 elements, the vector can be made longer by assigning values to elements 5, 6, and so on.
Define vector DF with 4 elements. Elements can also be added to a vector by appending existing vectors. Two examples are: This can be done by assigning new values, or by appending existing variables. This must be done carefully since the size of the added rows or columns must fit the existing matrix.
The numbers of rows in E and K must be the same. Zeros are assigned to the other elements that are added. Assign a value to the 3,4 element of a new matrix. This is done by using square brackets with nothing typed in between them.
By deleting elements a vector can be made shorter and a matrix can be made to have a smaller size. Eliminate elements 3 through 6. Some of these are listed below: Built-in functions for handling arrays Function Description Example length A Returns the number of elements in the vector A. The elements are taken column after column. Matrix A must have m times n elements. Built-in functions for handling arrays Continued Function Description Example diag v When v is a vector, creates a square matrix with the elements of v in the diagonal.
Sample Problem Reassign the number 1 to the 3rd and 4th columns. Creating Arrays Sample Problem The first two rows are columns 1 through 4 of rows 1 and 2 of matrix B. The third row consists of elements 5 through 8 of vector v. The fourth row consists of columns 2 through 5 of row 3 of matrix B. It is created by typing the characters within single quotes. When the single quote at the end of the string is typed, the color of the string changes to purple.
They are used in output commands to display text messages Chapter 4 , in formatting commands of plots Chapter 5 , and as input arguments of some functions Chapter 7. More details are given in these chapters when strings are used for these purposes. See Chapter 5 for details.
Strings can also be assigned to variables by simply typing the string on the right side of the assignment operator, as shown in the examples below: Each character, including a space, is an element in the array. This means that a one-line string is a row vector in which the number of elements is equal to the number of characters. The elements of the vectors are 54 Chapter 2: Creating Arrays addressed by position.
For example, in the vector B that was defined above the 4th element is the letter n, the 12th element is J, and so on. For example, in the vector B above the name John can be changed to Bill by: Strings can also be placed in a matrix.
As with numbers, this is done by typing a semicolon ; or pressing the Enter key at the end of each row. Each row must be typed as a string, which means that it must be enclosed in single quotes. In addition, as with a numerical matrix, all rows must have the same number of elements. This requirement can cause problems when the intention is to create rows with specific wording. Rows can be made to have the same number of elements by adding spaces. MATLAB has a built-in function named char that creates an array with rows having the same number of characters from an input of rows not all of the same length.
MATLAB makes the length of all the rows equal to that of the longest row by adding spaces at the end of the short lines. In the char function, the rows are entered as strings separated by a comma according to the following format: John Smith Grade: The function char creates an array with four rows with the same length as the longest row by adding empty spaces to the shorter lines.
For example, as shown below, x is defined to be the number , and y is defined to be a string made up of the digits The variable x can be used in mathematical expressions, while the variable y cannot.
Create a row vector that has the following elements: Create a column vector that has the following elements: Create a row vector in which the first element is 2 and the last element is 37, with an increment of 5 between the elements 2, 7, 12, … , Creating Arrays 8.
Create a row vector with 9 equally spaced elements in which the first element is 81 and the last element is Create a column vector in which the first element is A column vector can be created by the transpose of a row vector. Create a column vector with 15 equally spaced elements in which the first element is —21 and the last element is Using the colon symbol, create a row vector assign it to a variable named same with seven elements that are all —3.
Use a single command to create a row vector assign it to a variable named a with 9 elements such that the last element is 7.
Do not type the vector explicitly. Create a vector name it vecA that has 14 elements of which the first is 49, the increment is —3, and the last element is Then, using the colon symbol, create a new vector call it vecB that has 8 elements. The first 4 elements are the first 4 elements of the vector vecA, and the last 4 are the last 4 elements of the vector vecA. Create a vector name it vecC that has 16 elements of which the first is 13, the increment is 4 and the last element is Then create the following two vectors: In both parts use vectors of odd and even numbers for the index of Codd and Ceven, respectively.
Do not type the vectors explicitly. Do not type individual elements explicitly. Create the following matrix by typing one command. Creating Arrays Create three row vectors: Create two row vectors: Parts b , c , and d use the vector that was defined in part a.
In addition, the book can be a supplement or a secondary book in courses where MATLAB is used but the instructor does not have the time to cover it extensively. The 1 2 Introduction assumption is that once these foundations are well understood, the student will be able to learn advanced topics easily by using the information in the Help menu. The order in which the topics are presented in this book was chosen carefully, based on several years of experience in teaching MATLAB in an introductory engineering course.
The topics are presented in an order that allows the student to follow the book chapter after chapter. Every topic is presented completely in one place and then used in the following chapters. The first chapter describes the basic structure and features of MATLAB and how to use the program for simple arithmetic operations with scalars as with a calculator. Script files are introduced at the end of the chapter.
The next two chapters are devoted to the topic of arrays. This concept, which makes MATLAB a very powerful program, can be a little difficult to grasp for students who have only limited knowledge of and experience with linear algebra and vector analysis. The concept of arrays is introduced gradually and then explained in extensive detail.
Chapter 2 describes how to create arrays, and Chapter 3 covers mathematical operations with arrays. Following the basics, more advanced topics that are related to script files and input and output of data are presented in Chapter 4. This is followed by coverage of two-dimensional plotting in Chapter 5.
This includes flow control with conditional statements and loops. User-defined functions, anonymous functions, and function functions are covered next in Chapter 7. The coverage of function files user-defined functions is intentionally separated from the subject of script files.
This has proven to be easier to understand by students who are not familiar with similar concepts from other computer programs. The next three chapters cover more advanced topics. It includes solving nonlinear equations, finding minimum or a maximum of a function, numerical integration, and solution of first-order ordinary differential equations.
Chapter 10 describes how to produce three-dimensional plots, an extension of the chapter on twodimensional plots. The Framework of a Typical Chapter In every chapter the topics are introduced gradually in an order that makes the concepts easy to understand. Some of the longer examples in Chapters 1—3 are titled as tutorials.
Additional explanations appear in boxed text with a white background. In addition, every chapter includes formal sample problems that are examples of applications of MATLAB for solving problems in math, science, and engineering. Each example includes a problem statement and a detailed solution. Some sample problems are presented in the middle of the chapter. All of the chapters except Chapter 2 have a section at the end with several sample problems of applications.
The solutions of the sample problems are written such that they are easy to follow. The students are encouraged to try to write their own solutions and compare the end results. At the end of each chapter there is a set of homework problems.Define two variables: In the commands above, table is the name of the variable that is a matrix containing the data to be displayed.
Matlab Tool contains many algorithms and toolboxes freely available. The current i in amps t seconds after closing the switch in the circuit shown is given by: When the program is executed, the following is displayed in the Command Window: