The MATLAB Editor Scripts and Functions The MATLAB Editor, Cont. Assignments References MATLAB for the Sciences The MATLAB Editor, Scripts, and Functions Jon M. Ernstberger January 6, 2008 Jon M. Ernstberger MATLAB for the Sciences The MATLAB Editor Scripts and Functions The MATLAB Editor, Cont. Assignments References The MATLAB Editor To access the MATLAB editor, simply type “edit” at the terminal. The editor provides the basic interface for programming in any form in MATLAB. This is more than JUST a text editor. You’ll see that. Jon M. Ernstberger MATLAB for the Sciences The MATLAB Editor Scripts and Functions The MATLAB Editor, Cont. Assignments References MATLAB Scripts What is a Script? A MATLAB script is a sequence of commands that has been saved to a file. Traditionally saved to files with extensions “.m”. Scope: can interact with the currently available workspace. Pro or con? You may have to clear variables! Jon M. Ernstberger MATLAB for the Sciences The MATLAB Editor Scripts and Functions The MATLAB Editor, Cont. Assignments References Exercises Do a help on “clear”. Create a simple script which adds the squares of both x and y . In your terminal set x = 2 and y = 4. What happens if you do or don’t include “clear all” in your MATLAB script? Do a help on “zeros”, and a help on “rand”. In a new MATLAB script named zero test.m make a random matrix which has dimension 1000 × 1000. Create a matrix of identical size of zeros. Add, subtract, and multiply these matrices together. In full sentences in comments, at the top of your script, describe each “built-in” MATLAB function used and the intent of this script. In the first three lines of these comments place your name, the script name, and the date. Jon M. Ernstberger MATLAB for the Sciences The MATLAB Editor Scripts and Functions The MATLAB Editor, Cont. Assignments References Exercises, cont. Do a help on “save”. In that same script file listed above, save the variable workspace to your hard-disk in a .mat file named “big matrices.mat” using the command in the script (not using a command through the MATLAB IDE). Heavily comment this line for future remembrance. Jon M. Ernstberger MATLAB for the Sciences The MATLAB Editor Scripts and Functions The MATLAB Editor, Cont. Assignments References MATLAB Functions What defines a mathematical function? (Hint: think domains and elements.) What about a function F (x) : Rn → Rm ? ex: f (x, y ) = x 2 + y 2 , x 2 − y 2 The idea is “ For one (set of) input(s), there is one (set of) output(s).” We often want to define MATLAB “functions” which take inputs and return non-ambiguous outputs. Jon M. Ernstberger MATLAB for the Sciences The MATLAB Editor Scripts and Functions The MATLAB Editor, Cont. Assignments References MATLAB Functions, cont. MATLAB functions take inputs and return the outputs. Still saved as an m-file. Functions are to be used for encapsulating often reused bodies of code. Called via >>x=2; >>y=sample_function1(x); Jon M. Ernstberger MATLAB for the Sciences The MATLAB Editor Scripts and Functions The MATLAB Editor, Cont. Assignments References Multi-input/output functions I can have multiple inputs and outputs to any MATLAB function. This saves the programmer from writing multiple functions which take the same inputs to output different values. function [f,g]=pseudo_fcn1(x,y) f=x^2+y^2; g=x^2-y^2; Jon M. Ernstberger MATLAB for the Sciences The MATLAB Editor Scripts and Functions The MATLAB Editor, Cont. Assignments References A Sample Function Compute the area of a circle in a function. A = πr 2 %This function computes the area of a circle. %Input: radius r %Output: area function area=circle_properties(r) area=pi*r^2; In-Class Exercise Download circle properties.m and change this function to also output circumference. Jon M. Ernstberger MATLAB for the Sciences The MATLAB Editor Scripts and Functions The MATLAB Editor, Cont. Assignments References More Exercises Create a function which takes as input a single value or a vector of values and returns the tangent of that/those values. You may not use the tan(x) function built into MATLAB. Name this function hard tan and save it as hard tan.m. Again heavily comment this piece of code. Save the above function as hard sec and alter this function to output the secant and cosecant of x using only the sine and cosine functions. In your comments, include all of these changes and be sure to mention which values cannot be input. Jon M. Ernstberger MATLAB for the Sciences The MATLAB Editor Scripts and Functions The MATLAB Editor, Cont. Assignments References More Exercises, cont. Create a function called half life. This function should take as input decay rates, initial amounts of a radioactive substance, and amount of time the quantity has been decaying. Heavily comment this function. Modify this function to output a second argument, the half-life of the material. Test your results with Neptunium-239 and Carbon-14. (Hint: use Google or Wikipedia.) Jon M. Ernstberger MATLAB for the Sciences The MATLAB Editor Scripts and Functions The MATLAB Editor, Cont. Assignments References Comments What’s the big deal about comments? Example % Jon Ernstberger % 12/22/2008 % Sample Comments Each comment begins with a “%”. Everything on a line that starts with the “%” will be considered a comment. Helpful Hints: Do not use short-hand. Use complete sentences. Always comment! Comment often. Jon M. Ernstberger MATLAB for the Sciences The MATLAB Editor Scripts and Functions The MATLAB Editor, Cont. Assignments References The Text Menu “Comment/Uncomment”. Highlight a body of code and comment or uncomment those commandlines. “Increase Indent/Decrease Indent”. Highlight a body of code and increase or decrease the indentation. “Smart Indent”. The MATLAB editor should “smartly” choose the indentation of an indented group of text. Should be automatic. “Code Folding”. If you have tons of nested codes, this can compact your code incredibly for purposes of readability! Jon M. Ernstberger MATLAB for the Sciences The MATLAB Editor Scripts and Functions The MATLAB Editor, Cont. Assignments References The Tools Menu MATLAB Profiler Useful for identifying trouble points in your code. The Profiler identifies portions of code which take the longest periods of time. Some codes require hours, days, or months of time to run! By reducing the runtime of the “heavy” portions of code, these runtimes are highly decreased. The Profiler also makes suggestions for reprogramming certain code portions. We’ll go into more detail at a later point (hopefully). Jon M. Ernstberger MATLAB for the Sciences The MATLAB Editor Scripts and Functions The MATLAB Editor, Cont. Assignments References The Window Menu “Split-screen”. Helps you see and compare bodies of code in conjunction. Tiling of windows is useful to keep more than two files open for editing or comparison. Programming Hint Use “Left/Right Split Screen” in order to edit scripts and functions. This helps the programmer see the most code possible. Jon M. Ernstberger MATLAB for the Sciences The MATLAB Editor Scripts and Functions The MATLAB Editor, Cont. Assignments References In-Class Assignments 1 Write a MATLAB function named temp convert which takes as input temperature in Fahrenheit and outputs the conversion in degrees Celsius. How can you test to make sure it works? 2 Write a MATLAB script which evaluates the temperature at 95◦ and −44.5◦ Fahrenheit. 3 Modify your function temp convert to have a second output for degrees Kelvin. 4 Rerun Problem 2 to test your answers. Jon M. Ernstberger MATLAB for the Sciences The MATLAB Editor Scripts and Functions The MATLAB Editor, Cont. Assignments References Take Home Assignments 1 2 The maximum height of a projectile with initial velocity V A 2 2 fired at angle A radians above the horizon is V sin 2g 2 where g = 9.81m/s . What is the maximum height reached by a rock from a volcano ejected at 700 m/s at an angle of 60◦ ?[1] (Hint: Type help sin.) The difference formula is given as f (x + h) − f (x) h 6= 0. h Set h = 1 × 10−5 and let f (x) = x 2 + 2x + 1. Write a MATLAB function for f (x) named quadratic fcn1 which takes as input x and outputs f (x). Write a MATLAB script named quadratic fcn1 eval to evaluate the difference quotient for f (x) at x = 10, x = 20, and x = 1000. Jon M. Ernstberger MATLAB for the Sciences The MATLAB Editor Scripts and Functions The MATLAB Editor, Cont. Assignments References References S.L. Edgar. Fortran for the’90s: problem solving for scientists and engineers. Computer Science Press New York, NY, 1992. Jon M. Ernstberger MATLAB for the Sciences
© Copyright 2026 Paperzz