1 / 6

Engr 0012 (04-1) LecNotes 15-01

Declaring/using a function for evaluation. String Function. Inline Function. m-file Function. Declaration. Declaration. Declaration. str = ' 2* x +2 '; fcn = inline( str); negf = inline[' -( ',str,' ) ' ];. fcn = ' 2* x +2 '; negf = [' -( ',fcn,' ) ' ];. function [y] = fcn(x)

erling
Download Presentation

Engr 0012 (04-1) LecNotes 15-01

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Declaring/using a function for evaluation String Function Inline Function m-file Function Declaration Declaration Declaration str = '2*x+2'; fcn = inline(str); negf = inline['-(',str,')' ]; fcn = '2*x+2'; negf = ['-(',fcn,')' ]; function [y] = fcn(x) y = 2*x+2; function [y] = negf(x) y = -fcn(x); Calculating “y-values” Calculating “y-values” Calculating “y-values” xvec = linspace(-5,5,8); yvec = feval(fcn,xvec); xvec = linspace(-5,5,8); yvec = feval(fcn,xvec); x = linspace(-5,5,8); y = eval(fcn,x); Displaying function - any declartion domain = [-10 10]; fplot(fcn,domain); Finding roots - any declaration xroot = fzero(fcn,where); Engr 0012 (04-1) LecNotes 15-01

  2. Declaring/using a function for evaluation String Function m-file Function Inline Function Finding minima Finding area between (lb,ub) - any declaration Finding minima Finding maxima Finding maxima Finding minima Finding maxima xmax = fminbnd(negf,lb,ub); ymax = feval(fcn,xmax); xmin = fminbnd(fcn,lb,ub); ymin = feval(fcn,xmin); xmax = fminbnd(negf,lb,ub); ymax = feval(fcn,xmax); xmin = fminbnd(fcn,lb,ub); ymin = feval(fcn,xmin); area = quad(fcn,lb,ub); or area = quadl(fcn,lb,ub); xmax = fminbnd(negf,lb,ub); x = xmax; ymax = eval(fcn,x); xmin = fminbnd(fcn,lb,ub); x = xmin; ymin = eval(fcn,x); Engr 0012 (04-1) LecNotes 15-02

  3. Declaring/using a function for evaluation String Function m-file Function Inline Function Finding area - traps Plotting cumulative area - any declaration Finding area - traps Finding area - traps ntraps = 100; xtrap = lb:(ub-lb)/ntraps:ub; x = xtrap; ytrap = eval(fcn,x); area = trapz(xtrap,ytrap); % calc cumarea from xmin to xpt xpts = xmin:(xmax-xmin)/500:xmax; for i = 1:1:length(xpts) cumarea(i) = quad(fcn,xpts(1),xpts(i)); end % plot cumulative area hold on plot(xpts,cumarea,'g-') ntraps = 100; xtrap = lb:(ub-lb)/ntraps:ub; ytrap = feval(fcn,xtrap); area = trapz(xtrap,ytrap); ntraps = 100; xtrap = lb:(ub-lb)/ntraps:ub; ytrap = feval(fcn,xtrap); area = trapz(xtrap,ytrap); Engr 0012 (04-1) LecNotes 15-03

  4. Calculating a derivative forward difference backward difference central difference definition of derivative approximations of the derivative - for small x Engr 0012 (04-1) LecNotes 15-04

  5. Calculating a derivative implementation in MATLAB specify x evaluate function at x+x evaluate function at x-x estimate derivative Engr 0012 (04-1) LecNotes 15-05

  6. Declaring/using a function for evaluation Inline Function String Function m-file Function Plotting derivative Plotting derivative Finding derivative at xpt Finding derivative at xpt Finding derivative at xpt Plotting derivative delx = (xmax-xmin)/10000; fx_p = feval(fcn,xpt+delx); fx_m = feval(fcn,xpt-delx); der = (fx_p-fx_m)/(2*delx); % calc derivative at xpts xpts = xmin:(xmax-xmin)/ 500:xmax; fx_p=feval(fcn,xpts+delx); fx_m=feval(fcn,xpts-delx); der =(fx_p-fx_m)/(2*delx); % plot derivative hold on plot(xpts,der,'b--') % calc derivative at xpts xpts = xmin:(xmax-xmin)/ 500:xmax; x = xpts+delx; fx_p = eval(fcn,x); x = xpts-delx; fx_m = eval(fcn,x); der =(fx_p-fx_m)/(2*delx); % plot derivative hold on plot(xpts,der,'b--') % calc derivative at xpts xpts = xmin:(xmax-xmin)/ 500:xmax; fx_p=feval(fcn,xpts+delx); fx_m=feval(fcn,xpts-delx); der =(fx_p-fx_m)/(2*delx); % plot derivative hold on plot(xpts,der,'b--') delx = (xmax-xmin)/10000; fx_p = feval(fcn,xpt+delx); fx_m = feval(fcn,xpt-delx); der = (fx_p-fx_m)/(2*delx); delx = (xmax-xmin)/10000; x = xpt+delx; fx_p = eval(fcn,x); x = xpt-delx; fx_m = eval(fcn,x); der = (fx_p-fx_m)/(2*delx); Engr 0012 (04-1) LecNotes 15-06

More Related