# Engr 0012 (04-1) LecNotes 13-01. Engr 0012 (04-1) LecNotes 13-02 Functional analysis y = f(x) things to do 1. sketch graph 2. find roots (zeros) 3. find.

## Presentation on theme: "Engr 0012 (04-1) LecNotes 13-01. Engr 0012 (04-1) LecNotes 13-02 Functional analysis y = f(x) things to do 1. sketch graph 2. find roots (zeros) 3. find."— Presentation transcript:

Engr 0012 (04-1) LecNotes 13-01

Engr 0012 (04-1) LecNotes 13-02 Functional analysis y = f(x) things to do 1. sketch graph 2. find roots (zeros) 3. find minima 4. find maxima 5. find area “under” curve 6. show  f(x) dx 7. show d[f(x)]/dx

Engr 0012 (04-1) LecNotes 13-03 Declaring/using a function for evaluation two methods string declaration fcn_name = 'function definition'; myfcn = ' 2*cos(3*x)./exp(x) ' myfcn = 2*cos(3*x)./exp(x) creates a string variable for function string use - use eval function yvalues = eval(fcn_name); >> x = linspace(0,pi,8); creates 8 points in (0,  ) required!! a vector or variable with same name as variable used in fucntion >> y1 = eval(myfcn) y1 = 2.0000 0.2841 -0.7344 -0.3244 0.2071 0.1911 -0.0301 -0.0864 evaluates fcn at those 8 points

Engr 0012 (04-1) LecNotes 13-04 Declaring/using a function for evaluation string use >> clear x >> xpts = linspace(0,pi,8); >> y1 = eval(myfcn) ??? Undefined function or variable 'x'. no longer have a vector or variable “x” in the workspace potential problem: if eval is working, is it working on the data set you intended to use - or some prior definition of x?

Engr 0012 (04-1) LecNotes 13-05 Declaring/using a function for evaluation string declaration/use advantages easy to use in MATLAB command window good for one-time evaluations disadvantages hard-wired - cannot change without effort can’t use in general purpose script or function must have vector with same variable name as used in function declaration

Engr 0012 (04-1) LecNotes 13-06 Declaring/using a function for evaluation m-file declaration function [y] = fcn_name(x) needs results function [y] = f13a(x) y = 2*cos(3*x)./exp(x); create function, save to current directory m-file use - use feval function >> fname = input( 'Enter function file name ==> ', 's' ) Enter function file name ==> f13a fname = f13a ask for function name - response does not need to have.m because it must be an m-file y_vec = feval(fcn_name,x_vec) “indirect” reference to the function

Engr 0012 (04-1) LecNotes 13-07 Declaring/using a function for evaluation m-file use - use feval function >> ypts = feval(fname,xpts) y2 = 2.0000 0.2841 -0.7344 -0.3244 0.2071 0.1911 -0.0301 -0.0864 >> xnew = 1:1:5 xnew = 1 2 3 4 5 >> ynew = feval(fname,xnew) ynew = -0.7284 0.2599 -0.0907 0.0309 -0.0102 feval works with any vector name - not just the one that was used in the function file

Engr 0012 (04-1) LecNotes 13-08 Declaring/using a function for evaluation m-file declaration/use advantages generic - can write scripts that ask for function name scripts can be used for multiple function evaluations without editing works with any independent vector name/set disadvantages have to create separate function with equation cumbersome to use in MATLAB command window

Engr 0012 (04-1) LecNotes 13-09 Quick plot of a function - fplot >> domain = [min(x),max(x)] domain = 0 3.1416 establish domain for plot >> fplot(fname,domain) m-file function >> fplot(myfcn,domain) string function can use either declaration form with fplot

Engr 0012 (04-1) LecNotes 13-10 Finding roots - fzero xzero = fzero(fcn_name,approximate location of root) >> xzero1 = fzero(fname,0.5) xzero1 = 0.5236 >> xzero2 = fzero(myfcn,1.5) xzero2 = 1.5708 can use either declaration form with fzero

Engr 0012 (04-1) LecNotes 13-11 Finding minima - fminbnd xmin = fminbnd(fcn_name,xlow,xhigh) >> xmin1 = fminbnd(fname,0.5,1.5) xmin1 = 0.9399 >> xmin2 = fminbnd(fname,2.5,max(x)) xmin2 = 3.0343 can use either declaration form with fminbnd

Engr 0012 (04-1) LecNotes 13-12 Finding maxima no fmaxbnd command can define new function string >> neg_myfcn = ['-1*(',myfcn,')']; call fminbnd with negative of function >> max1 = fminbnd(neg_myfcn,1.5,2.5) max1 = 1.9871 m-file function [y] = neg_f11a(x) y = -2*cos(3*x)./exp(x); need to create and save in current directory >> negf = input( 'neg fcn name? ==> ','s' ); neg fcn name? neg_f13a >> max2 = fminbnd(negf,1.5,2.5) max2 = 1.9871

Engr 0012 (04-1) LecNotes 13-13 Finding areas neg area pos area easiest: use quad area = quad(fcn_name,xmin,xmax) >> area1 = quad(myfcn,1.5,2.5) area1 = 0.1620 >> area2 = quad(fname,min(x),max(x)) area2 = 0.2086 can use either declaration form with quad

Engr 0012 (04-1) LecNotes 13-14 Finding areas more complex and not as accurate!!! use trapz area = trapz(xtrap,ytrap) need to define x & y vectors of points numtraps = 100; xtrap = xlow:(xhigh-xlow)/numtraps:xhigh; ytrap = feval(fcn_name,xtrap) area = trapz(xtrap,ytrap)

Engr 0012 (04-1) LecNotes 13-15 Finding  f(x) dx over a range objective is to create a vector of cumulative area versus x value and then plot them on the graph (i.e. area(x) vs x % create xpts for plotting; xpts = xlow:(xhigh-xlow)/200:xhigh; for i = 1:1:length(xpts) cumarea(i) = quad(fcn_name,xpts(1),xpts(i)); end % plot cumulative area plot(xpts,cumarea,‘g-’)

Engr 0012 (04-1) LecNotes 13-16 Finding d[f(x)]/dx over a range objective is to create a vector of derivatives versus x value and then plot them on the graph (i.e. deriv(x) vs x similar to cumulative area see text, Section 4.15, for discussion

Download ppt "Engr 0012 (04-1) LecNotes 13-01. Engr 0012 (04-1) LecNotes 13-02 Functional analysis y = f(x) things to do 1. sketch graph 2. find roots (zeros) 3. find."

Similar presentations