Top Banner

of 18

MATLAB Week1 Lecture3 Web

Apr 09, 2018

Download

Documents

Roberto Sanchez
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/8/2019 MATLAB Week1 Lecture3 Web

    1/18

    The MATLAB EditorScripts and Functions

    The MATLAB Editor, Cont.Assignments

    References

    MATLAB for the SciencesThe MATLAB Editor, Scripts, and Functions

    Jon M. Ernstberger

    January 6, 2008

    Jon M. Ernstberger MATLAB for the Sciences

    http://goforward/http://find/http://goback/
  • 8/8/2019 MATLAB Week1 Lecture3 Web

    2/18

    The MATLAB EditorScripts 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 anyform in MATLAB.

    This is more than JUST a text editor. Youll see that.Jon M. Ernstberger MATLAB for the Sciences

    http://goforward/http://find/http://goback/
  • 8/8/2019 MATLAB Week1 Lecture3 Web

    3/18

    The MATLAB EditorScripts 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 savedto a file.

    Traditionally saved to fileswith extensions .m.

    Scope: can interact with the

    currently availableworkspace. Pro or con?

    You may have to clearvariables!

    Jon M. Ernstberger MATLAB for the Sciences

    http://find/http://goback/
  • 8/8/2019 MATLAB Week1 Lecture3 Web

    4/18

    The MATLAB EditorScripts and Functions

    The MATLAB Editor, Cont.Assignments

    References

    Exercises

    Do a help on clear. Create a simple script which adds thesquares of both x and y. In your terminal set x = 2 andy = 4. What happens if you do or dont include clear all in

    your MATLAB script?Do a help on zeros, and a help on rand. In a newMATLAB script named zero test.m make a random matrixwhich has dimension 1000 1000. Create a matrix of identicalsize of zeros. Add, subtract, and multiply these matrices

    together. In full sentences in comments, at the top of yourscript, describe each built-in MATLAB function used andthe intent of this script. In the first three lines of thesecomments place your name, the script name, and the date.

    Jon M. Ernstberger MATLAB for the Sciences

    Th MATLAB Edi

    http://find/http://goback/
  • 8/8/2019 MATLAB Week1 Lecture3 Web

    5/18

    The MATLAB EditorScripts 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 filenamed big matrices.mat using the command in the script(not using a command through the MATLAB IDE). Heavilycomment this line for future remembrance.

    Jon M. Ernstberger MATLAB for the Sciences

    Th MATLAB Edit

    http://find/http://goback/
  • 8/8/2019 MATLAB Week1 Lecture3 Web

    6/18

    The MATLAB EditorScripts and Functions

    The MATLAB Editor, Cont.Assignments

    References

    MATLAB Functions

    What defines a mathematical function? (Hint: think domainsand elements.)

    What about a function

    F (x) : Rn Rm?

    ex: f(x, y) =

    x2 + y2, x2 y2

    The idea is For one (set of) input(s), there is one (set of)output(s).

    We often want to define MATLAB functions which takeinputs and return non-ambiguous outputs.

    Jon M. Ernstberger MATLAB for the Sciences

    The MATLAB Editor

    http://find/http://goback/
  • 8/8/2019 MATLAB Week1 Lecture3 Web

    7/18

    The MATLAB EditorScripts 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 forencapsulating often reusedbodies of code.

    Called via>>x=2;

    >>y=sample_function1(x);

    Jon M. Ernstberger MATLAB for the Sciences

    The MATLAB Editor

    http://find/http://goback/
  • 8/8/2019 MATLAB Week1 Lecture3 Web

    8/18

    The MATLAB EditorScripts and Functions

    The MATLAB Editor, Cont.Assignments

    References

    Multi-input/output functions

    I can have multiple inputs and outputs to any MATLABfunction.

    This saves the programmer from writing multiple functionswhich 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

    http://find/http://goback/
  • 8/8/2019 MATLAB Week1 Lecture3 Web

    9/18

    The MATLAB EditorScripts and Functions

    The MATLAB Editor, Cont.Assignments

    References

    A Sample Function

    Compute the area of a circle in a function.

    A = r2

    %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 alsooutput circumference.

    Jon M. Ernstberger MATLAB for the Sciences

    The MATLAB Editor

    http://find/http://goback/
  • 8/8/2019 MATLAB Week1 Lecture3 Web

    10/18

    The MATLAB EditorScripts and Functions

    The MATLAB Editor, Cont.Assignments

    References

    More Exercises

    Create a function which takes as input a single value or avector 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. Againheavily comment this piece of code.

    Save the above function as hard sec and alter this function tooutput the secant and cosecant of x using only the sine andcosine functions. In your comments, include all of thesechanges and be sure to mention which values cannot be input.

    Jon M. Ernstberger MATLAB for the Sciences

    The MATLAB Editor

    http://goforward/http://find/http://goback/
  • 8/8/2019 MATLAB Week1 Lecture3 Web

    11/18

    Scripts and FunctionsThe MATLAB Editor, Cont.

    AssignmentsReferences

    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. Heavilycomment this function.

    Modify this function to output a second argument, thehalf-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

    http://goforward/http://find/http://goback/
  • 8/8/2019 MATLAB Week1 Lecture3 Web

    12/18

    Scripts and FunctionsThe MATLAB Editor, Cont.

    AssignmentsReferences

    Comments

    Whats 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 beconsidered 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

    http://goforward/http://find/http://goback/
  • 8/8/2019 MATLAB Week1 Lecture3 Web

    13/18

    Scripts and FunctionsThe MATLAB Editor, Cont.

    AssignmentsReferences

    The Text Menu

    Comment/Uncomment. Highlight a body of code andcomment or uncomment those commandlines.

    Increase Indent/Decrease Indent. Highlight a body of codeand increase or decrease the indentation.

    Smart Indent. The MATLAB editor should smartlychoose the indentation of an indented group of text. Shouldbe automatic.

    Code Folding. If you have tons of nested codes, this cancompact your code incredibly for purposes of readability!

    Jon M. Ernstberger MATLAB for the Sciences

    The MATLAB EditorS

    http://goforward/http://find/http://goback/
  • 8/8/2019 MATLAB Week1 Lecture3 Web

    14/18

    Scripts and FunctionsThe MATLAB Editor, Cont.

    AssignmentsReferences

    The Tools Menu

    MATLAB Profiler

    Useful for identifying trouble points in your code.

    The Profiler identifies portions of code which take the longestperiods of time.

    Some codes require hours, days, or months of time to run! Byreducing the runtime of the heavy portions of code, theseruntimes are highly decreased.

    The Profiler also makes suggestions for reprogramming certaincode portions.

    Well go into more detail at a later point (hopefully).

    Jon M. Ernstberger MATLAB for the Sciences

    The MATLAB EditorS i d F i

    http://goforward/http://find/http://goback/
  • 8/8/2019 MATLAB Week1 Lecture3 Web

    15/18

    Scripts and FunctionsThe MATLAB Editor, Cont.

    AssignmentsReferences

    The Window Menu

    Split-screen. Helps you see and compare bodies of code inconjunction.

    Tiling of windows is useful to keep more than two files openfor editing or comparison.

    Programming Hint

    Use Left/Right Split Screen in order to edit scripts andfunctions. This helps the programmer see the most code possible.

    Jon M. Ernstberger MATLAB for the Sciences

    The MATLAB EditorS i ts d F ti s

    http://goforward/http://find/http://goback/
  • 8/8/2019 MATLAB Week1 Lecture3 Web

    16/18

    Scripts and FunctionsThe MATLAB Editor, Cont.

    AssignmentsReferences

    In-Class Assignments

    1 Write a MATLAB function named temp convert which takesas 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 EditorScripts and Functions

    http://goforward/http://find/http://goback/
  • 8/8/2019 MATLAB Week1 Lecture3 Web

    17/18

    Scripts and FunctionsThe MATLAB Editor, Cont.

    AssignmentsReferences

    Take Home Assignments

    1 The maximum height of a projectile with initial velocity V

    fired at angle A radians above the horizon is V2 sin2

    A

    2g

    where g = 9.81m/s2. What is the maximum height reached

    by a rock from a volcano ejected at 700 m/s at an angle of60?[1] (Hint: Type help sin.)

    2 The difference formula is given as

    f(x + h) f(x)

    hh = 0.

    Set h = 1 105 and let f(x) = x2 + 2x + 1. Write aMATLAB function for f(x) named quadratic fcn1 which takesas input x and outputs f(x). Write a MATLAB script namedquadratic 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 EditorScripts and Functions

    http://goforward/http://find/http://goback/
  • 8/8/2019 MATLAB Week1 Lecture3 Web

    18/18

    Scripts and FunctionsThe MATLAB Editor, Cont.

    AssignmentsReferences

    References

    S.L. Edgar.Fortran for the90s: problem solving for scientists and

    engineers.Computer Science Press New York, NY, 1992.

    Jon M. Ernstberger MATLAB for the Sciences

    http://goforward/http://find/http://goback/