DOC PREVIEW
GT AE 6382 - Scripts and Functions

This preview shows page 1-2-14-15-29-30 out of 30 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Scripts and FunctionsMatlab ScriptsExample Script in an m-fileScript WorkspaceMore Scripts…Matlab Functions (m-functions)M-function StructureUsing M-File Editor to Create a FunctionUsing Comments in M-functionsM-function WorkspaceM-function Workspace-cont’dglobal VariablesExample of the global StatementM-functions Can Return Multiple ValuesM-functions are the Core of MatlabMore M-function DetailsExample M-functionAnd More Details…A Special M-function: fevalfeval - continuedUse of feval in an M-functionExample: Using evaluate()A More Complicated ExampleUsing fminbnd()Application: Solving ODE’sSolving an ODE: setupMatlab ode45( ) Syntaxrhs( ) function for ode45( )Solving the Problem…A More Interesting RHS…Fall 2006AE6382 Design Computing 1Scripts and Functions•Matlab’s Command prompt is where you can enter commands to be executed immediately…–Just like a calculator–You can see what you’ve done but it must be re-entered at the command prompt to be recalculated–Only the results (variables) are retained in the Matlab workspace (diary on will log commands and results to a diary file; diary off disables this)–What if you want to enter different values for radius or length?>> length=5.5;>> radius=2.25;>> volume=pi.*radius^2.*lengthvolume = 87.4737Fall 2006AE6382 Design Computing 2Matlab Scripts•Matlab scripts are the solution to this problem!–You can create a “script” that can be repeatedly executed–This is the basic Matlab “program”•Scripts are simply text files containing Matlab statements–You can use any text editor but the built-in editor indents and uses color to highlight the language syntax–Script files always have the “.m” extension, e.g., m-files•When a script (m-file) is executed, it is simply read sequentially and each line is presented to the Matlab command prompt just like it was typed by hand–Speed and repeatability are key features–Matlab control & loop statements (e.g., if, for, while…) can be executed in this wayFall 2006AE6382 Design Computing 3Example Script in an m-file•Use File/New/M-file to start Matlab editor•Save file with .m extension in directory in Matlab’s path•Type m-file name at prompt to execute>> myscriptEnter the length of a cylinder: 8Now enter the radius: 2.4Volume of cylinder is: 144.76>> myscriptEnter the length of a cylinder: 10Now enter the radius: 1Volume of cylinder is: 31.416>>Fall 2006AE6382 Design Computing 4Script Workspace•When commands are executed, the results are left in the Matlab BASE workspace.–whos will list all the current variables in the workspace–Scripts can make use of variables already defined in the Base workspace>> myscriptEnter the length of a cylinder: 10Now enter the radius: 1Volume of cylinder is: 31.416>> whos Name Size Bytes Class length 1x1 8 double array radius 1x1 8 double array volume 1x1 8 double arrayGrand total is 3 elements using 24 bytes>> >> density=100;>> myscriptEnter the length of a cylinder: 10Now enter the radius: 1Weight of cylinder is: 3141.6>>Fall 2006AE6382 Design Computing 5More Scripts…•Try out commands indivudually…•Use scripts to collect together commands that can be used to solve more complicated problems.•Scripts can call other scripts–Can “chain” together individual small programs–Each script can be tested and debugged separately•PROBLEMS:–All scripts share same Base workspace–Variables can be confused and mis-used (may accidentally overwrite a previously defined variable)–Can’t localize results (create individual workspaces)–In other words, we can’t encapsulate variables and constants inside a script where they are hidden from other code.Fall 2006AE6382 Design Computing 6Matlab Functions (m-functions)•M-functions are like “functions” in algebra–Algebra: function is a rule that assigns a value based on specified values of the function arguments–Matlab: function is a program that computes a returned variable based on specified values of the function arguments>> cylinder(1,10)ans = 31.4159Fall 2006AE6382 Design Computing 7M-function Structurefunction volume=cylinder(radius, length)% CYLINDER computes volume of circular cylinder% given radius and length% Use:% vol=cylinder(radius, length)%volume=pi.*radius^2.*length;Function definitionFunction definitionReturned variableReturned variableArgumentsArgumentsHelp commentsHelp commentsStatements(no end required)Statements(no end required)NOTE: function names are NOT case sensitive in WindowsFall 2006AE6382 Design Computing 8Using M-File Editor to Create a Function•From Command menu choose File/New/M-file–new edit window will appear with "Untitled"–enter code for your new function–when saving, make sure file name is same as function name(the file name is what Matlab uses to identify your m-functions)On Your Own:Figure out what all the options in the File, Edit, View & Text menus do…Fall 2006AE6382 Design Computing 9Using Comments in M-functions•Comments immediately following function statement will be listed by the Matlab Help function–Place comments that define what your function does–Include comments to illustrate how to use function–Include version, date and author >> help cylinder CYLINDER computes volume of circular cylinder given radius and length Use: vol=cylinder(radius, length) >>Fall 2006AE6382 Design Computing 10M-function Workspace•Each time an m-function is executed, a new workspace is created just for that instance–All variables except arguments and returned variable are defined only in the function workspace –Function variables are not defined in Base workspace, nor are Base variables defined in function workspace–Function workspace is destroyed when function completes•Function variables are “hidden” from world (and from accidental misuse or alteration)>> cylinder(1,10)ans = 31.4159>> whos Name Size Bytes Class ans 1x1 8 double arrayGrand total is 1 elements using 8 bytesFall 2006AE6382 Design Computing 11M-function Workspace-cont’d•You can even define other M-functions inside the first M-function!–These are called “sub-functions”–Each has its own local function workspace independent of any other function workspace–These workspaces are created and destroyed with each function invocation (nothing persists between invocations)–Sub-functions are


View Full Document

GT AE 6382 - Scripts and Functions

Download Scripts and Functions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Scripts and Functions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Scripts and Functions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?