Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

MATLAB Programming: Functions and Scripts - Understanding MATLAB's m-files and Functions, Assignments of Engineering

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

Pre 2010

Uploaded on 08/19/2009

koofers-user-jm8-1
koofers-user-jm8-1 🇺🇸

10 documents

1 / 9

Toggle sidebar

Related documents


Partial preview of the text

Download MATLAB Programming: Functions and Scripts - Understanding MATLAB's m-files and Functions and more Assignments Engineering in PDF only on Docsity! MATLAB Programming Functions and Scripts 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 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 xforx xforx 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 Notice that the answer has been placed in a variable named ‘ans’. You can store the result in any variable name, using the following format: 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: ax2 + 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 acbb a acbb x 2 4 , 2 4 22  
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved