function hh = slopefld(arg1,arg2,arg3,arg4,arg5,arg6) %SLOPEFIELD plot. % SLOPEFLD(X,Y,U,V) plots normalized line segments with % components (u,v)*normalization factor at the points (x,y). % The matrices X,Y,U,V must all be % the same size and contain the cooresponding position and % velocity components (X and Y can also be vectors to specify % a uniform grid). slopefld automatically scales the line % segments to fit within the grid. % % SLOPEFLD(U,V) plots the line segments at equally spaced % points in the x-y plane. % % SLOPEFLD(X,Y,S) or SLOPEFLD(X,Y,U,V,S,...) % automatically scales the line segments to fit % within the grid and then multiplies them by S. % Use S=0 to plot the velocity vectors without the % automatic scaling. % % SLOPEFLD(...,STYLE) uses the plot linestyle specified % by the string STYLE for the line segments. % See PLOT for other linestyles. % % Example: % [x,y] = meshgrid(-2:.2:2,-1:.15:1); % z = x .* exp(-x.^2 - y.^2); [px,py] = gradient(z,.2,.15); % contour(x,y,z), hold on % slopefld(x,y,px,py), hold off, axis image % % See also: QUIVER, FEATHER, PLOT. % Matthias Kawski 8-22-95 error(nargchk(2,6,nargin)); % Arrow head parameters % alpha = 0.33; % Size of arrow head relative to the length of the vector % beta = 0.33; % Width of the base of the arrow head relative to the length autoscale = 1; % Autoscale if ~= 0 then scale by this. % Check numeric input arguments if nargin<4 [msg,x,y,u,v] = xyzchk(arg1,arg2); elseif nargin==4 if isstr(arg4) [msg,x,y,u,v] = xyzchk(arg1,arg2); else [msg,x,y,u,v] = xyzchk(arg1,arg2,arg3,arg4); end else [msg,x,y,u,v] = xyzchk(arg1,arg2,arg3,arg4); end if ~isempty(msg), error(msg); end if nargin==2, % slopefld(u,v) lo = get(gca,'LineStyleOrder'); sym = lo(1,:); elseif nargin==3, % slopefld(u,v,s) or slopefld(u,v,'style') if isstr(arg3), sym = arg3; else autoscale = arg3; lo = get(gca,'LineStyleOrder'); sym = lo(1,:); end elseif nargin==4, % slopefld(x,y,u,v) or slopefld(x,y,s,'style') if isstr(arg4), autoscale = arg3; sym = arg4; else lo = get(gca,'LineStyleOrder'); sym = lo(1,:); end elseif nargin==5, % slopefld(x,y,u,v,s) or slopefld(x,y,u,v,'style') if isstr(arg5), sym = arg5; else autoscale = arg5; lo = get(gca,'LineStyleOrder'); sym = lo(1,:); end elseif nargin==6, % slopefld(x,y,u,v,s,style) autoscale = arg5; sym = arg6; end if autoscale, % Base autoscale value on average spacing in the x and y % directions. Estimate number of points in each direction as % either the size of the input arrays or the effective square % spacing if x and y are vectors. if min(size(x))==1, n=sqrt(prod(size(x))); m=n; else [m,n]=size(x); end delx = diff([min(x(find(~isnan(x)))) max(x(find(~isnan(x))))])/n; dely = diff([min(y(find(~isnan(y)))) max(y(find(~isnan(y))))])/m; len = sqrt((u/delx).^2 + (v/dely).^2); autoscale = autoscale*0.9 ./ len; %(find(~isnan(len))); u = u.*autoscale; v = v.*autoscale; end ax = newplot; next = lower(get(ax,'NextPlot')); hold_state = ishold; % Make velocity vectors x = x(:).'; y = y(:).'; u = u(:).'; v = v(:).'; uu = [x-u/2;x+u/2;NaN*ones(size(u))]; vv = [y-v/2;y+v/2;NaN*ones(size(u))]; h = plot(uu(:),vv(:),sym); % Make arrow heads and plot them %hu = [x+u-alpha*(u+beta*(v+eps));x+u; ... % x+u-alpha*(u-beta*(v+eps));NaN*ones(size(u))]; %hv = [y+v-alpha*(v-beta*(u+eps));y+v; ... % y+v-alpha*(v+beta*(u+eps));NaN*ones(size(v))]; hold on %h = [h;plot(hu(:),hv(:),sym)]; if ~hold_state, hold off, view(2); set(ax,'NextPlot',next); end if nargout>0, hh = h; end