mase201sp09_Mar17_PRINTABLE

# mase201sp09_Mar17_PRINTABLE - Numerical Differentiation and...

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

Numerical Differentiation and Integration • Numeric Differentiation Topics (Tues 3/3): – Finite difference formulas • Forward, Backward and Central Finite Difference • 2 nd derivatives diff function – Curve fitting before taking derivative olyder nction (sec 8.1.4) 3/17/2009 1 polyder function (sec 8.1.4) – MATLAB built-in function for numeric differentiation: gradient – Partial differentiation • Today: Writing MATLAB functions • Today and Tue 3/24: Numerical Integration topics MASE201 Spring 2009 Functions (Review) In mathematics, a function may be defined as: “… a relation that uniquely associates members of one set with members of another set.” http://mathworld.wolfram.com/Function.html “ … a set of ordered pairs of numbers (x,y) with the following property: for each value of the first variable (x) there corresponds a unique value of the second variable (y)” homas, alculus and Analytic Geometry 5 d. 1979 3/17/2009 2 Thomas, Calculus and Analytic Geometry , 5 th ed. 1979 In computer programming, a function (or subprogram) may be described as: “ … like a sauce recipe given once and used as part of many other recipes. Subprograms take inputs (the quantity needed) and produce results (the sauce)” (http://search.eb.com.libproxy.wustl.edu/eb/article-248137) MASE201 Spring 2009 MATLAB functions • MATLAB is a computer programming environment that specializes in scientific computing, which involves, among other tasks, evaluation of mathematical functions ATLAB has many built “functions”, some of 3/17/2009 3 • MATLAB has many built-in “functions”, some of that involve mathematics, and some that do not • You can write your own functions that you use just like the MATLAB built-in ones MASE201 Spring 2009 Advantages of using MATLAB functions • Compared to scripts: – For complicated tasks, they keep the workspace free of “clutter”, i.e. temporary variables – This makes them easier to share with other users – The function can easily be re-used with different variables as input and output arguments xample: onvert ( y coordinates to ( coordinates • Example: convert ( x,y ) coordinates to ( r, θ ) coordinates Definition: (function saved as cart2pol.m ): Use from Command Window: xvec=[2 1 0]; yvec=[0 1 2]; [r,theta]=cart2pol(xvec,yvec) 3/17/2009 4 function [r, theta ] = cart2pol(x,y) r = sqrt(x.^2 + y.^2); theta = atan2(y,x); %four quadrant arctangent MASE201 Spring 2009

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

View Full Document
Function Arguments and Variables Input 1 Input 2 Input 3 Output 1 Output 2 In 1 In 2 In 3 Out 1 Out 2 k var1 var2 3/17/2009 5 A function has inputs and outputs In mathematics the inputs are called “independent variables” or “arguments” MATLAB uses the term “arguments” for both inputs and outputs, e.g. “input arguments” and “output arguments” MASE201 Spring 2009 Creating a function m-file • Open a new m-file in the Editor Window • The first line of a function m-file must start with the word “ function
This is the end of the preview. Sign up to access the rest of the document.

## This document was uploaded on 05/05/2010.

### Page1 / 8

mase201sp09_Mar17_PRINTABLE - Numerical Differentiation and...

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

View Full Document
Ask a homework question - tutors are online