E7_lecture7_F08_Function_handles_BW

E7_lecture7_F08_Function_handles_BW - 1 E7 INTRODUCTION TO...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 E7: INTRODUCTION TO COMPUTER ROGRAMMING FOR SCIENTISTS AND PROGRAMMING FOR SCIENTISTS AND ENGINEERS Lecture Outline 1. Functions revisited unction that act on functions through function 2. Function that act on functions through function handles ore on the function handle 3. More on the function handle opyright 2007 Horowitz Packard This work is licensed under the Creative Commons Attribution- hare Copyright 2007, Horowitz, Packard. This work is licensed under the Creative Commons Attribution Share Alike License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA. 2 FUNCTIONS in Matlab set of commands that execute a “task” at can be A set of commands that execute a task that can be reuse in several instances. • Function files must always possess the extension .m The file name should be the same as the function’s • Functions can call other functions! ariables in a function file are cal nd isposable Variables in a function file are local and disposable – Not recognized in Matlab’s command window or in any ther function E7 L7 other function – Not stored in the Matlab workspace. 3 Finding appropriate Functions with lookfor se ookfor om the matlab ommand window Use lookfor from the matlab command window to get functions that are relevant to your task • Example: >> lookfor imaginary I Imaginary unit. J Imaginary unit. COMPLEX Construct complex result from real and imaginary parts. MAG Complex imaginary part IMAG Complex imaginary part. E7 L7 4 Example of a function alculate and plot the ( trajectory of a projectile given: Calculate and plot the ( x - y ) trajectory of a projectile given: • It’s initial speed: p • The angle of departure v o ± Equations used (physics): x ( t ) = v o cos ( ± ) t y ( t )= v o sin ( ± ) t 1 2 gt 2 E7 L7
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
5 Function cannon1 function [t, x,y] = cannon1(v0,theta,g) [, ,y ] ( , ,g) % cannon computes the trajectory of a projectile fired at an angle theta % with initial velocity v0 under gravitational acceleration g % Outputs: t: time vector, x: - x-coordinate vector, y: y-coordinate vector % Compute flying time of the projectile tmax = 2*v0*sin(theta)/g; % Define time vector of length 100 t = linspace(0,tmax,100); % Compute x-coordinate vector x = v0*cos(theta) .* t; % Compute y-coordinate vector y = v0*sin(theta) .* t - 0.5*g .* t .* t; E7 L7 6 Function cannon1 nction x y] = cannon1(v0 theta g) function [t, x, y] = cannon1(v0,theta,g) 2 *0 *i( tht) / input scalar arguments tmax = 2*v0*sin(theta)/g; 3 input scalar arguments t = linspace(0,tmax,100); 3 vector output arguments x = v0*cos(theta) .* t; y = v0*sin(theta) .* t - 0.5*g .* t .* t;
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern