60 likes | 151 Views
Declaring/using a function for evaluation. String Function. Inline Function. m-file Function. Declaration. Declaration. Declaration. ifname = inline( ' 2* x +2 ');. sfname = ' 2* x +2 ';. function [y] = mfname(x); y = 2* x +2;. Calculating “y-values”. Calculating “y-values”.
E N D
Declaring/using a function for evaluation String Function Inline Function m-file Function Declaration Declaration Declaration ifname = inline('2*x+2'); sfname = '2*x+2'; function [y] = mfname(x); y = 2*x+2; Calculating “y-values” Calculating “y-values” Calculating “y-values” xvec = linspace(-5,5,8); yvec =feval(ifname,xvec); xvec = linspace(-5,5,8); yvec =feval(mfname,xvec); x = linspace(-5,5,8); y = eval(sfname,x); Displaying function Displaying function Displaying function domain = [-10 10]; fplot(mfname,domain); domain = [-10 10]; fplot(sfname,domain); domain = [-10 10]; fplot(ifname,domain); Finding roots Finding roots Finding roots xroot = fzero(ifname,where); xroot = fzero(sfname,where); xroot = fzero(mfname,where); Finding minima Finding minima Finding minima xmin = fminbnd(ifname,lb,ub); ymin =feval(ifname,xmin); xmin = fminbnd(sfname,lb,ub); x = xmin; ymin = eval(sfname,x); xmin = fminbnd(mfname,lb,ub); ymin =feval(mfname,xmin); Engr 0012 (04-1) LecNotes 14-01
string inline >> negfcn = ['-1*(',sfname,')']; >> negfcn = inline( '-( 2*cos(3*x)./exp(x))' ); m-file function [y] = neg_f13a(x) y = -( 2*cos(3*x)./exp(x) ); need to create and save in current directory Finding maxima nofmaxbnd command can define new function >> negfcn = input('neg fcn name? ==> ','s'); neg fcn name? neg_f13a Engr 0012 (04-1) LecNotes 14-02
string inline >> max1 = fminbnd(negfcn,1.5,2.5) max1 = 1.9871 >> max3 = fminbnd(negfcn,1.5,2.5) max3 = 1.9871 m-file >> max2 = fminbnd(negfcn,1.5,2.5) max2 = 1.9871 Finding maxima call fminbnd with negative of function Engr 0012 (04-1) LecNotes 14-03
pos area neg area Finding areas 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 >> area3 = quadl(ifname,0,pi) area3 = 0. 2086 can use any declaration form with quad or quadl Engr 0012 (04-1) LecNotes 14-04
area = 0.2088 area = 0.2087 area = 0.2086 n = 100 n = 300 n = 500 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); Engr 0012 (04-1) LecNotes 14-05
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-') Engr 0012 (04-1) LecNotes 14-06