Download presentation
Presentation is loading. Please wait.
Published byLester Stokes Modified over 9 years ago
1
Engr 0012 (04-1) LecNotes 14-01 Declaring/using a function for evaluation String Function Declaration sfname = '2*x+2'; Calculating “y-values” x = linspace(-5,5,8); y = eval(sfname,x); Displaying function domain = [-10 10]; fplot(sfname,domain); Finding roots xroot = fzero(sfname,where); Finding minima xmin = fminbnd(sfname,lb,ub); x = xmin; ymin = eval(sfname,x); m-file Function Declaration function [y] = mfname(x); y = 2*x+2; Calculating “y-values” xvec = linspace(-5,5,8); yvec =feval(mfname,xvec); Displaying function domain = [-10 10]; fplot(mfname,domain); Finding roots xroot = fzero(mfname,where); Finding minima xmin = fminbnd(mfname,lb,ub); ymin =feval(mfname,xmin); Inline Function Declaration ifname = inline('2*x+2'); Calculating “y-values” xvec = linspace(-5,5,8); yvec =feval(ifname,xvec); Displaying function domain = [-10 10]; fplot(ifname,domain); Finding roots xroot = fzero(ifname,where); Finding minima xmin = fminbnd(ifname,lb,ub); ymin =feval(ifname,xmin);
2
Engr 0012 (04-1) LecNotes 14-02 Finding maxima no fmaxbnd command can define new function string >> negfcn = ['-1*(',sfname,')']; m-file function [y] = neg_f13a(x) y = -( 2*cos(3*x)./exp(x) ); need to create and save in current directory >> negfcn = input('neg fcn name? ==> ','s'); neg fcn name? neg_f13a inline >> negfcn = inline( '-( 2*cos(3*x)./exp(x))' );
3
Engr 0012 (04-1) LecNotes 14-03 Finding maxima call fminbnd with negative of function string >> max1 = fminbnd(negfcn,1.5,2.5) max1 = 1.9871 m-file >> max2 = fminbnd(negfcn,1.5,2.5) max2 = 1.9871 inline >> max3 = fminbnd(negfcn,1.5,2.5) max3 = 1.9871
4
Engr 0012 (04-1) LecNotes 14-04 Finding areas neg area pos area easiest: use quad or quadl area = quad(fname,xmin,xmax); or area = quadl(fname,xmin,xmax); >> area1 = quad(sfname,1.5,2.5) area1 = 0.1620 >> area2 = quadl(mfname,1.5,2.5) area2 = 0. 1620 can use any declaration form with quad or quadl >> area3 = quadl(ifname,0,pi) area3 = 0. 2086
5
Engr 0012 (04-1) LecNotes 14-05 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; if string x = xtrap; ytrap = eval(sfname,x); if m-file ytrap = feval(mfname,xtrap); if inline ytrap = feval(ifname,xtrap); area = trapz(xtrap,ytrap); n = 100 area = 0.2088 n = 300 area = 0.2087 n = 500 area = 0.2086
6
Engr 0012 (04-1) LecNotes 14-06 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 = xmin:(xmax-xmin)/500:xmax; for i = 1:1:length(xpts) cumarea(i) = quad(fname,xpts(1),xpts(i)); end % plot cumulative area hold on plot(xpts,cumarea,'g-')
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.