% Script 1 for Week 3 fun = @sin; % function fun(pi) x0 = 3; % initial point xcoord1 = fzero(fun,x0) [xcoord ycoord]= fzero(fun,[10,20]) % The fminbnd command in MATLAB can be used to find the % value of a function that is a (possibly local) minimum of the function. % The command can only find one minimum at a time and can only % find minima based on one variable at a time. If there is a % single local minimum over the domain, fminbnd should find it. % If there are several, it should find one of them. t=linspace(2,7,1000); y=sin(t.*t); plot(t,y) g = fminbnd(@(x) sin(x.*x),2,7) % fminsearch tries to find the minimum of a % function of one or several variables. % The seed value must be a vector or single number. w = fminsearch(@(x) x^2-7*x+1, 0) banana = @(x)100*(x(2)-x(1).^2).^2+(1-x(1)).^2; [xval,fval] = fminsearch(banana,[-1.2, 1]) [x,y] = meshgrid([-2:.2:2]); Z = 100*(y-x.^2).^2+(1-x).^2; surf(x,y,Z) colorbar % More fun with plots. Visualizing four dimensions with color. % If v is temperature as a function of position in space % this is a way of visualizing it. % The color paints the slices according to the value of v. [x,y,z] = meshgrid(-2:.2:2,-2:.25:2,-2:.16:2); v = x.*exp(-x.^2-y.^2-z.^2); xslice = [-1.2,.8,2]; yslice = 2; zslice = [-2,0]; slice(x,y,z,v,xslice,yslice,zslice) colormap hsv colorbar % Below are the scripts for a couple of test function M-files % They illustrate how to find minima over irregular regions. %******************************************* function z=testfunc1(x,y) if x.^2+y.^2>4 z=Inf; elseif (y<0) & (x>0) & (y<-x) z=Inf; elseif (y<0) & (x<=0) z=Inf; else z=exp(-y).*cos(5.*x+y); end %******************************************* %******************************************* function z=testfunc2(x,y) if 3*pi/4 < x z=Inf; elseif pi/4 > x z=Inf; elseif 0 > y z=Inf; elseif x+y > pi z=Inf; else z = 2./(2+sin(y))+3./(2+sin(x)); end %******************************************* clear [x,y] = meshgrid([-3:.05:3]); for k=1:length(x); for w=1:length(y); Z1(k,w) = testfunc1(x(k,w), y(k,w)); end end surf(x,y,Z1) colorbar shading('interp') surfc(x,y,Z1) % tricky issue: % fminsearch requires that the functions it examines % have a single variable, but that variable can be a vector. [xval,fval] = fminsearch(testfunc1,[-1.2, 1]) % fails because testfunc1 requires two inputs. % But test1=@(x) testfunc1(x(1),x(2)) [a b]=fminsearch(test1,[-1.2 1]) % works, producing the x,y coordinate of the local min % (not a global min!!) and the actual minimum value there. for k=1:length(x); for w=1:length(y); Z2(k,w) = testfunc2(x(k,w), y(k,w)); end end surfc(x,y,Z2) colorbar shading('interp') % Find approximate location of min by examining graph: there is a % data cursor you can select near the top of a figure to get coordinates. [xval,fval] = fminsearch(testfunc2,[1, 1]) % fails because testfunc2 requires two inputs. % But test2=@(x) testfunc2(x(1),x(2)) [a b]=fminsearch(test2,[1 1]) % works, producing the x,y coordinate of the local min in a and the % actual minimum value there in b.