





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of matlab's programming and scripting environment, focusing on m-files and functions. It explains how to create and save m-files, embed comments, and execute scripts. Additionally, it covers the concept of functions, their definition, and usage with examples.
Typology: Assignments
1 / 9
This page cannot be seen from the preview
Don't miss anything!






While MATLAB is a powerful environment for command-line numerical analysis, problem solving and data presentation, its true power lies in its programming and scripting environment. This handout summarizes the two major features of this capability and presents several examples. Matlab m-files The basic unit of programming in MATLAB is the m-file. These are text files containing lists of MATLAB command that can be invoked, or called, from the command prompt (or from another m-file). MATLAB has an m-file editor built in and can be invoked from the main tool bar by clicking on the ‘new document’ icon on the left hand side. Before you start the editor, make sure you set the current directory to a directory you have control over. See the figure below: Set Current Directory Start the m-file editor
Let’s do a quick example. Once you click on the ‘new file’ icon, the m-file editor comes up with a window that looks like this: Let’s enter a few lines that store some variables in the work space and perform a simple operation. File name in title bar line number along left side
Note that nothing was echoed to the screen, but the three variables we assigned within the script file now appear in the workspace. You can verify their values by typing them at the command prompt. Type ‘c’ at the prompt and verify that your script executed properly. Sometimes, you might want the results of the script file to echo to the screen immediately. This is particularly useful for debugging purposes. As an exercise, go to the editor and delete the semicolons at the end of each assignment statement, re-save the file and execute it from the command line. You will see that all three statements will be reported in the command window. Hence, we have another feature of the MATLAB environment: Semicolons at the end of a statement will suppress output to the screen. Functions: A special kind of file Sometimes, you will find it useful to write a special kind of program called a function. Like the mathematical concept, a function receives information from the external environment in the form of arguments and returns one or more values. If that statement is confusing, don’t lose hope, let’s look at some examples.
We’ll start simple. Let’s write a function that returns the absolute value of a single variable. Recall the mathematical definition of the absolute value: 0 0 x for x x for x x Go back to your editor and start a new file (click on the ‘new document’ icon) and enter these lines: Notice that your first file is still in the editor, you can page through your open files by click on the tabs at the bottom of the window. This particular function uses the if-else functions in Matlab. Now save this as the file my_abs. You invoke a function slightly differently than a script file. Since you have defined an argument (x) for this function, the call from the command line must also contain an argument. Let’s test the function by finding the absolute value of –2.8: Note the syntax for the first line
Which is the typical manner of calling functions. In this case, the number –35 is the argument passed to the function, the result, 35 is the value returned by the function. Arguments and returned values can be matrices. Also, there can be several arguments passed to functions, as we will see presently. Example: Roots of a quadratic equation. The quadratic equation arises in a multitude of physical models and applications. A quadratic equation in x is of this form: ax^2 + bx + c = 0 The variables a, b, and c are constant coefficients. The two roots (values for which the equation is correct) are given by this well-know equation. a b b ac a b b ac x 2 4 , 2 ^2 4 ^2
However, it can get complicated. The expression under the square-root sign can be positive, zero or negative. If it is positive, then the results are given by the above equation and two unique, real roots are found. If the expression under the square root sign is zero, then the answer is a pair of repeated roots (both roots the same). If the expression under the square root is less than zero, then the roots are complex numbers. Complex numbers are numbers that have real and imaginary parts. The basis of imaginary numbers is the square root of –1, given by the symbol “i”. MATLAB represents and manipulates complex numbers, as we will see presently. The window below shows a MATLAB function that computes the roots of a quadratic equation. Examine it carefully. There’s a lot new in this function. First, notice that both the argument and the value returned are matrices, not single values. Since Matlab treats every piece of data as a matrix, no special syntax is required. Also, note the decision-making structure. I used a single if -statement that had an elseif clause as well as an else clause. Finally, notice the manner in which the individual elements of the argument matrix are accessed and the way in which the individual elements of the return matrix are set. The figure below shows the function being run for a simple set of coefficients.