-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolorvfield.m
83 lines (76 loc) · 2.63 KB
/
colorvfield.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
function colorvfield(X,Y,U,V,varargin)
%
% COLORVFIELD Colored 2D Vector Field Plotter.
%
% COLORVFIELD(X,Y,U,V) plots colored vectors with components (u,v) at the
% points (x,y). The vectors are colored using the jet color map (with
% the smallest vectors colored blue the largest colored red) and are
% divided into 32 discrete color levels.
%
% COLORVFIELD(X,Y,U,V,NUMBER_COLOR_LEVELS) divides the color map into
% NUMBER_COLOR_LEVELS dicrete color levels.
%
% Example:
% X = linspace(-10,10,32);
% Y = linspace(0, 0, 32);
% U = linspace(-1,-5,32);
% V=linspace(1,5,32);
% colorvfield(X,Y,U,V,16); % plot 16 color levels
% title('Vectors Plotted with Color Map (colorvfield.m)')
% axis([-12.25,11.75,-0.25,4.75])
% xlabel('X')
% ylabel('Y')
%
% Written by M. B. Sullivan, George Mason University ([email protected])
%
% Revision 1.0.
% Released 26 November 2007.
%
% error checking
if nargin < 4 % too few arguments
disp('COLORVFIELD Error: Too few input arguments.');
return
elseif nargin > 5 % too many arguments
disp('COLORVFIELD Error: Too many input arguments.');
return
end
% improperly sized input vectors
if length(X) ~= length(Y) || length(X) ~= length(U)...
|| length(X) ~= length(V) || length(Y) ~= length(U)...
|| length(Y) ~= length(V) || length(U) ~= length(V)
disp('COLORVFIELD Error: X, Y, U and V are not the same length!');
return
end
% define constants
if length(varargin) == 0 % number of color levels not given
NUMBER_COLOR_LEVELS = 32; % default value
else
NUMBER_COLOR_LEVELS = varargin{1}(:); % set number of color levels
end
ARRAY_LENGTH = length(X); % length of X,Y,Z,U,V and W
% form color map
vcolormap = jet(NUMBER_COLOR_LEVELS); % blue is smallest, red largest
% create color level bounds
vnorm = zeros(1,ARRAY_LENGTH); % preallocate for speed
for i=1:ARRAY_LENGTH
vnorm(i) = norm([U(i),V(i)]);
end
vmin = min(vnorm); % smallest vector
vmax = max(vnorm); % largest vector
vbounds = linspace(vmin, vmax, NUMBER_COLOR_LEVELS+1); % color bounds
% plot each point in the appropriate color
for j=1:ARRAY_LENGTH
% figure out what color level is appropriate
color_level = NUMBER_COLOR_LEVELS; % start at max index
for k=1:NUMBER_COLOR_LEVELS
if vnorm(j) >= vbounds(k)
color_level = k;
else
break
end
end
% color_level now holds the approprate color level
% plot the vector in its approprate color
quiver(X(j),Y(j),U(j),V(j),'Color',vcolormap(color_level,:))
hold on
end