E7_lecture7_F08_Function_handles_BW

E7_lecture7_F08_Function_handles_BW - 1 E7: INTRODUCTION TO...

Info iconThis 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
Background image of page 1

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

View Full DocumentRight 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;
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/01/2009 for the course ENGLISH 7 taught by Professor Sengupta during the Spring '09 term at Berkeley.

Page1 / 8

E7_lecture7_F08_Function_handles_BW - 1 E7: INTRODUCTION TO...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online