Matlab for beginners a gentle approach pdf download
This book assumes no prior programming knowledge and will teach you the basic programming concepts like variables, assignments, and selection statements, loops, etc.
View on Amazon View on Amazon India. MATLAB For Dummies is a book for beginners and will teach you how to plot graphs, solve equations, and how to write clean code quickly. You will understand how to automate your work with programming scripts and functions and quickly plot graphs in 2D and 3D to visualize your data.
You will learn how to how to program in MATLAB for creating applications that are most commonly used in the field of neuroscience and psychology. Select a Web Site. Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:. Select the China site in Chinese or English for best site performance.
Other MathWorks country sites are not optimized for visits from your location. Store the script in a script file called example8. Finally run the script file. Write a function of three lines to calculate the volume of a sphere of radius r. The first line should include the name of the function which is volume r. The second line should be a comment line. Write a function with two arguments to calculate the area of a rectangle with sides a and b.
The function should have three lines. The first line should include the name of the function which is RectangleArea a,b. Store the function in a function file called RectangleArea. Write a script containing a For loop to compute the vector x to 7. Include a comment line at the beginning. Store the script in a script file called example9. Write a script containing two nested For loops to compute both m and n each has the range from 1 to 4. Store the script in a script file called example Write a script containing a While loop using the two variables tol and n.
Make the loop end when the value of tol becomes equal or larger than 1. Write a function called price items containing an If construct as follows. Store the function in a function file called price. Make sure that the function displays the results for the variable price. Write a function called price2 items containing an If Elseif construct as follows. If the value of the variable items is less than 3, then compute the variable price2 by multiplying items by In the second case, if the value of the variable items is less than 5, then compute the variable price2 by multiplying items by In the last case, if the value of the variable items is larger than 5, then compute the variable price2 by multiplying the items by Store the function in a function file called price2.
Make sure that the function displays the results for the variable price2. Write a function called price3 items containing a Switch Case construct. The function should produce the same results obtained in Exercise 8 above. Store the function in a function file called price3. Make sure that the function displays the results for the variable price3.
Include a comment Write a script file to store the following symbolic matrix A line at the beginning. Write a function called SquareRoot2 matrix similar to the function SquareRoot matrix described at the end of this chapter but with the following change. Make sure that you include a comment line at the beginning.
Store the function in a function file called SquareRoot2. Both two-dimensional and three-dimensional graphs are presented. First, we will consider two-dimensional or planar graphs. To plot a two-dimensional graph, we need two vectors. Note how the command plot was used above along with the two vectors x and y.
For example, we can use the title command to add a title to the graph. For this we use the hold on command as follows: Figure 9. First, we need to define the vector x, then calculate the vector y using the above formula. Then we use the plot command as usual for the two vectors x and y. Figure 9. The resulting graph is shown in Figure 9. The line style may be changed from solid to dotted, dashed, or dash-dot lines by changing the parameters in the plot command.
In addition, the color of the plotted line may be changed. Note that colors are not displayed in this book. In addition to changing the line style and color, we can include symbols at the plotted points. For example, we can use the circle or cross symbols to denote the plotted points. There are other commands for other symbols like different variations of triangles, etc.
The above options for line style, color, and symbols can be combined in the same command. Here is the previous example with a red dotted line with cross symbols but without the title and label information note that no colors appear in this book. See Figure 9. In this example, we plot two curves on the same graph. We use the modified plot command as plot x,y,x,z to plot the two curves.
Here are the needed commands See Chapter 8 for details about scripts and script files. We can further use several axis commands to customize the appearance of the two axes including the tick marks, but this is beyond the scope of this book.
These tools are available from the menus of the graph window — the window where the graph appears. Semi-logarithmic plots can also be generated by using the MATLAB commands semilogx or semilogy but their use is beyond the scope of this book. The use of this command will enable you to present several plot diagrams on the same graph.
In this example, we use four instances of the subplot command to show four diagrams on the same plot. We will plot four mathematical functions — each on its own diagram — but the four diagrams will appear on the same graph. We can do this by playing with the parameters of the subplot command. Note that the diagrams are shown without title or axis labels in order to emphasize the use of the subplot command.
The first two parameters 2,2 indicate that the graph area should be divided into four quadrants with each row and column 36These commands may be entered individually on the command prompt or stored in as a script in a script file.
The third parameter indicates in which sub-area the next plot will appear. Note also that each subplot command is followed by a corresponding plot command. Actually, each subplot command reserves a specific area for the plot while the subsequent corresponding plot command does the actual plotting. You can control the number and arrangement of the diagrams that are displayed by controlling the three parameters of the subplot command. The first command is plot3 which is used to plot curves in space.
It requires three vectors as input. Here is an example of this command followed by the resulting graph as shown in Figure 9. The use of the mesh command is used to generate mesh surfaces.
The simplest use of this command is with a matrix. It assumes that the matrix elements are values of a grid spanning the matrix dimensions. The values of the elements of the matrix are taken automatically along the z-axis while the numbers of rows and columns are taken along the x- and y-axes.
Here is an example followed by the resulting graph as shown in Figure 9. Again, the reader should use the title and axis information commands to display information on the graph but these are not shown in this example.
Its use is similar to the mesh command. Again, the MATLAB command meshgrid may be used to generate a grid for the three-dimensional plot if one is not available. Again, the reader should use the title and axis information commands to display these types of information on the plot above but these are not used in this example. The use of this command produces contour plots of three-dimensional surfaces.
The use of this command is similar to the mesh and surf commands. For example, the meshc and surfc commands produce the same mesh and surface plots as the commands mesh and surf but with the contour plot appearing underneath the surface.
The use of this command is very simple and is not shown here. In Exercise 1 above, add to the graph a suitable title along with labels for the x-axis and the y-axis. Include a title for the graph as well as labels for the two axes. Repeat the plot of Exercise 3 above but show the curve with a blue dashed line with circle symbols at the plotted points.
Distinguish the two curves by plotting one 4 with a dashed line and the other one with a dotted line. Include the title and axis information on the graph as well as a grid and a legend. Plot the following four mathematical functions each with its own diagram using the subplot command.
There is no need to show title or axis information. Use the mesh command to plot a three-dimensional mesh surface of elements of the matrix M given below.
Use the surf command to plot a three-dimensional surface of the elements of the matrix M given in Exercise 8 above. Use the contour command to plot a contour map of the elements of the matrix M given in Exercise 8 above. There is no need to show the contour values on the graph.
Use the surfc command to plot a three-dimensional surface along with contours underneath it of the matrix M given in Exercise 8 above. We will discuss both linear and nonlinear algebraic equations. We will also discuss systems of simultaneous linear and nonlinear algebraic equations. Let us now solve a more complicated equation. These values are the five roots of the polynomial given above. Note that three of these roots are real while two roots are complex conjugates of each other. Next, let us discuss how to solve a system of linear simultaneous algebraic equations.
In order to solve such systems there are several methods available with most of them involving the use of matrices and vectors. The coefficients of x and y on the left-hand-side of the equations are considered as the elements of a square matrix of size 2x2 here while the numbers on the right- hand-side are entered into a vector.
Note 37 Consult a book on linear algebra for the proof of this solution. In this case, it was quick to find this inverse because the coefficient matrix was small. Therefore, it is advised to use another method to solve the above system.
One such method that is rather fast in execution is called Gaussian elimination. The use of Gaussian elimination along with the backslash operator in MATLAB is greatly recommended for solving large systems of algebraic linear simultaneous equations. Systems of simultaneous nonlinear algebraic equations can also be solved in MATLAB but these are significantly more difficult to solve.
One will have to use the MATLAB Optimization Toolbox to find certain functions for the solution of these types of equations — for example the command fsolve in this toolbox will solve a system of nonlinear equations.
This option will be illustrated in detail below. Please note that the command roots that was discussed earlier in this chapter cannot be used for the symbolic solution of algebraic equations. It needs to be replaced with the solve command.
Note that we cannot use the trick of writing it as a polynomial and use the roots38 command. Next, let us solve the following quadratic equation for x in a terms of the constants a, b, and c: 38 The roots command can only be used with numerical computations. Let us now solve another nonlinear equation not involving a polynomial. The correct use of the pretty command in the example above would be pretty x.
The resulting solution is approximated as 0. Next, we will consider systems of equations. Our final example will illustrate the solution of a nonlinear system of simultaneous algebraic equations. Finally, one may use the MATLAB command format short to display the six solutions above using four decimal digits only. Solve the following linear algebraic equation for the variable x. Use the roots command. Solve the following quadratic algebraic equation for the variable x.
Solve the following algebraic equation for x. Solve the following system of linear simultaneous algebraic equations for the variables x and y. Use the inverse matrix method. In Exercise 4 above, solve the same linear system again but using the Gaussian elimination method with the backslash operator. Solve the following system of linear simultaneous algebraic equations for the variables x, y, and z.
Use Gaussian elimination with the backslash operator. Solve the following linear algebraic equation for the variable x in terms of the constant a. Solve the following quadratic algebraic equation for the variable x in terms of the constants a and b. Solve the following nonlinear equation for the variable x. Solve the following system of linear simultaneous algebraic equations for the variables x and y in terms of the constant c. Solve the following system of nonlinear simultaneous algebraic equations for the variables x and y.
Note that the definition of the the inline command. Note also that we had to use the syms command to declare that x was a symbolic variable. The above integral was an indefinite integral. Also, we can use MATLAB to evaluate double and triple integrals by repeated uses of the int command or by using certain arguments.
However, these exercises are straightforward and will be left to the reader. The same thing can also be done with the diff and int commands that were used previously for differentiation and integration, respectively.
If the above expressions are to be evaluated at a certain point and their sum or product is needed, one can use the MATLAB commands sum42 and prod for this purpose. For example, some of these commands are expand, simplify, simple, factor, collect, gradient, and subs However, the use of these commands will not be illustrated in this introductory book.
Finally, we will illustrate the use of MATLAB in solving a simple ordinary differential equation44 using the command dsolve. Higher order ordinary differential equations can also be used by repeated use of the dsolve command.
Our final note is 43One of these commands has been used in a previous chapter. Actually, the unknowns are functions that need to be determined. In Exercise 1 above, differentiate the function f with respect to x.
In Exercise 5 above, differentiate the function g with respect to y. In Exercise 5 above, find the indefinite integral of the function g. In Exercise 8 above, evaluate the value obtained numerically using the double command. Find the Taylor series expansion for the function cos x up to eight terms. Find the Taylor series expansion for the function e x up to nine terms.
Introduction 1. INV X is the inverse of the square matrix X. A warning message is printed if X is badly scaled or nearly singular.
See also slash, pinv, cond, condest, lsqnonneg, lscov. Arithmetic Operations 1. Variables 1. Undefined function or variable 'm'. We get an error message because the variable m is not defined. Mathematical Functions 1. Compute the value of the expression 2 2 sin x cos y. Complex Numbers 1. Vectors 1. Form a new vector by joining the two vectors [9 3 -2 5 0] and [1 2 - 4]. We get an error because the two vectors do not have the same length.
We get an error message because the two vectors do not have the same length. In Exercise 22 above, perform the operation of scalar subtraction - 2-w. Yes, because this operation needs to be performed element by element.
If they were real variables, then the results would be the same. Matrices 1. What is the size of this new sub- matrix? In Exercise 11 above, multiply the two matrices R and S element- by-element.
In Exercise 11 above, divide the two matrices R and S element-by- element. Try to check this answer by multiplying this complex matrix by itself to get the original matrix. Programming 1. Write a script containing a For loop to compute the vector x to Include a comment line at the beginning.
Write a script containing two nested For loops to compute the n each has the range from 1 to 4. Include a comment line at the Write a script file to store the following symbolic matrix A then beginning. Substitute the value of 1. Distinguish the two curves by plotting one with a dashed 4 line and the other one with a dotted line. No need to show title or axis information. Use the plot3 command to show a three-dimensional curve of 10] for both x and y.
No need to show the contour values on the graph. Solving Equations 1. Beginning Calculus 1. Consult a book on special function for more details. The tutorial emphasizes vectors, matrices, vector operations, loops, plots, executable files scripts , subroutines functions , if statements, and data files. The tutorial emphasizes matrices, variables, functions, decisions, loops, and scripts.
This tutorial emphasizes matrices, decisions, loops, scalar functions, vector functions, matrix functions, strings, and graphics. You may also download the tutorial as a postscript file 39 pages that include a comprehensive reference section. It emphasizes matrices, arithmetic and logical operators, control structures, selective indexing, polynomial operations, signal processing functions, graphics, scripts, and functions.
It covers matrices, various MATLAB commands, help, plotting, polynomials, variables, scripts, functions, loops, debugging, differential equations, vectorization, three-dimensional graphics, and symbolic math. They provide six PDF files for download in the form of six lessons for a mathematics course they teach there. It covers the basics of vectors, functions, plotting, polynomials, matrices, printing, help, and M-files. This tutorial covers variables, calculations, plotting, scripts, and files.
This is a very simple tutorial recommended for beginners.
0コメント