Commit ddc4b0dae99a51c0cad6d162538671d633d3e49f

Authored by Jean-Luc Shaw
1 parent fa2630c0
Exists in master

Added the ctrspd.m routine with calculates the speed at which a line in 2D is moving.

geo/m_circle.m 0 → 100644
... ... @@ -0,0 +1,14 @@
  1 +function m_circle(lon,lat,r,label)
  2 +
  3 +if isempty(label)
  4 + rng = [0:360 0] ;
  5 +else
  6 + rng = [290:360 0:250] ;
  7 + m_text(lon,lat-r,label,'horizontalalignment','center','fontsize',6) ;
  8 +end
  9 +
  10 +x = lon + r*cosd(rng)/cosd(lat) ;
  11 +y = lat + r*sind(rng) ;
  12 +m_plot(x,y,'k-') ;
  13 +
  14 +end
... ...
math/ctrspd.m 0 → 100644
... ... @@ -0,0 +1,132 @@
  1 +function [u,v] = ctrspd(x1,y1,x2,y2,t)
  2 +% Synthax : [u,v] = ctrspd(x1,y1,x2,y2,t)
  3 +%
  4 +% Calculates the speed at which a contour travels perpendicularly to itself.
  5 +% The instantaneous speed of a vertex is calculated as the distance between
  6 +% itself at time t_1, and the intersection of the contour at time t_2 with a
  7 +% line normal to contour 1 and crossing the vertex.
  8 +%
  9 +% Inputs are coordinates at t_1 : x1,y1
  10 +% coordinates at t_2 : x2,y2
  11 +% times 1 & 2 : t
  12 +%
  13 +% Where t is size two vector. Contours may be periodic or non periodic. The
  14 +% beginning and ends of the contour will be handled differently in these
  15 +% cases.
  16 +
  17 +% PARAMETERS
  18 +NA = numel(x1) ;
  19 +NB = numel(x2) ;
  20 +per1 = false;
  21 +per2 = false;
  22 +warning('off','MATLAB:singularMatrix') ;
  23 +warning('off','MATLAB:polyfit:RepeatedPointsOrRescale') ;
  24 +warning('off','MATLAB:nearlySingularMatrix') ;
  25 +
  26 +% MAKE CONTOURS PERIODIC according to prior periodicity
  27 +if x1(1) == x1(end) & y1(1) == y1(end) & x2(1) == x2(end) & y2(1) == y2(end)
  28 + x1 = [x1(end-1) x1(1:end-1) x1(1)] ;
  29 + y1 = [y1(end-1) y1(1:end-1) y1(1)] ;
  30 + x2 = [x2(end-1) x2(1:end-1) x2(1)] ;
  31 + y2 = [y2(end-1) y2(1:end-1) y2(1)] ;
  32 + per1 = true;
  33 + per2 = true;
  34 +elseif x1(1) == x1(end) & y1(1) == y1(end) & x2(1) ~= x2(end) & y2(1) ~= y2(end)
  35 + x1 = [x1(end-1) x1(1:end-1) x1(1)] ;
  36 + y1 = [y1(end-1) y1(1:end-1) y1(1)] ;
  37 + x2 = [x2(end) x2 x2(1)] ;
  38 + y2 = [y2(end) y2 y2(1)] ;
  39 + per1 = true;
  40 +elseif x1(1) ~= x1(end) & y1(1) ~= y1(end) & x2(1) ~= x2(end) & y2(1) ~= y2(end)
  41 + x1 = [x1(end) x1 x1(1)] ;
  42 + y1 = [y1(end) y1 y1(1)] ;
  43 + x2 = [x2(end-1) x2(1:end-1) x2(1)] ;
  44 + y2 = [y2(end-1) y2(1:end-1) y2(1)] ;
  45 + per2 = true;
  46 +else
  47 + x1 = [x1(end) x1 x1(1)] ;
  48 + y1 = [y1(end) y1 y1(1)] ;
  49 + x2 = [x2(end) x2 x2(1)] ;
  50 + y2 = [y2(end) y2 y2(1)] ;
  51 +end
  52 +
  53 +% OUTPUT VARIABLES
  54 +u = nan(size(x1)) ;
  55 +v = nan(size(x1)) ;
  56 +
  57 +% LOOP OVER CONTOUR VERTICES
  58 +for ii = 2:numel(x1) - 1
  59 + gam = range_pm180_2_360( atan2d(y1(ii+1)-y1(ii-1),x1(ii+1)-x1(ii-1))) ;
  60 + if ~per1 & ii == 2
  61 + gam = range_pm180_2_360( atan2d(y1(ii+1)-y1(ii),x1(ii+1)-x1(ii))) ;
  62 + elseif ~per1 & ii == numel(x1) - 1
  63 + gam = range_pm180_2_360( atan2d(y1(ii)-y1(ii-1),x1(ii)-x1(ii-1))) ;
  64 + end
  65 + bet = gam - 90 ;
  66 + if bet < 0 ; bet+360 ; end
  67 +
  68 + xp = x1(ii) + cosd(bet) ;
  69 + yp = y1(ii) + sind(bet) ;
  70 +
  71 + % Make the parametric equation for this line
  72 + m1 = polyfit([x1(ii) xp],[y1(ii) yp],1) ;
  73 + vec = @(x)m1(1)*x + m1(2) ;
  74 +
  75 + % Draw for the next contours x values
  76 + yvec = vec(x2) ;
  77 +
  78 + % Find intersections
  79 + I = false(NB,1) ;
  80 + for jj = 2:numel(x2) - 1
  81 + if y2(jj) > yvec(jj) & y2(jj-1) < yvec(jj-1) | y2(jj) < yvec(jj) & y2(jj-1) > yvec(jj-1)
  82 + I(jj) = true ;
  83 + end
  84 + end
  85 + I = find(I) ;
  86 +
  87 + % Narrow it down to the nearest intersection
  88 + d = nan(size(I)) ;
  89 + for jj = 1:numel(I)
  90 + d(jj) = sqrt( (x1(ii) - x2(I(jj))).^2 + (y1(ii) - y2(I(jj))).^2 ) ;
  91 + end
  92 + [~,Imin] = min(d) ;
  93 + Imin = I(Imin) ;
  94 +
  95 + % Fit this line segment
  96 + m2 = polyfit([x2(Imin) x2(Imin-1)],[y2(Imin) y2(Imin-1)],1) ;
  97 +
  98 + % Solve the linear system of these two lines
  99 + A = [ m1(1) -1; m2(1) -1];
  100 + B = [-m1(2); -m2(2)];
  101 + X = A\B ;
  102 +
  103 + % Calculate angle and magnitude of velocity
  104 + dist = sqrt( (x1(ii) - X(1)).^2 + (y1(ii) - X(2)).^2 ) ;
  105 + the = range_pm180_2_360( atan2d(X(2)-y1(ii),X(1)-x1(ii))) ;
  106 +
  107 + % Decompose into components
  108 + spd = dist/(t(2) - t(1)) ;
  109 + u(ii) = spd*cosd(the) ;
  110 + v(ii) = spd*sind(the) ;
  111 +
  112 +end
  113 +
  114 +% DROP PERIODICITY
  115 +u([1 end]) = [];
  116 +v([1 end]) = [];
  117 +x1([1 end]) = [];
  118 +y1([1 end]) = [];
  119 +
  120 +% MIRROR INPUT PERIODICITY
  121 +if per1 ; u = app(u,u(1)) ; v = app(v,v(1)) ; end
  122 +
  123 +function y = app(x,v)
  124 +
  125 +if iscolumn(x)
  126 + y = [x;v] ;
  127 +else
  128 + y = [x v] ;
  129 +end
  130 +end
  131 +
  132 +end
... ...
math/genopt.m
1   -function [A,HS] = genopt(Q,C,dom,NP,NI,pc,pm)
2   -% Synthax: [A,HS] = genopt(@Q,@X,dom,NP,NI,pc,pm)
  1 +function [A,S] = genopt(Q,C,dom,NP,NI,pc,pm)
  2 +% Synthax: [A,S] = genopt(@Q,@X,dom,NP,NI,pc,pm)
3 3 %
4 4 % Optimisation routine following a genetic algorithm. Returns the best parameter
5 5 % set which solves the problem expressed by the anonymous function 'Q', when
... ... @@ -61,9 +61,9 @@ for kk = 1:NI
61 61 end
62 62 end
63 63 end
64   -disp(['Last iteration: ' num2str(kk)]) ;
  64 +%disp(['Last iteration: ' num2str(kk)]) ;
65 65  
66 66 % OUTPUT
67 67 A = MOM ;
68   -HS = HS(1) ;
  68 +S = HS(1) ;
69 69 end
... ...
physics/currextrap.m
... ... @@ -56,9 +56,9 @@ for ii = 1:2:nargin-5
56 56 case 'section'
57 57 switch varargin{ii+1}
58 58 case 'top'
59   - W = zc/z;
  59 + W = zc./z;
60 60 case 'bottom'
61   - W = z/zc;
  61 + W = z./zc;
62 62 case 'all'
63 63 W = ones(size(z));
64 64 end
... ...
util/points2grid.m
1   -function points2grid(x,y,m_map,varargin)
  1 +function [varargout] = points2grid(x,y,m_map,varargin)
2 2 % Synthax : points2grid(x,y,m_map,varargin)
3 3 %
4 4 % Takes as input a grid of coordinates contained in 'x' and 'y' with the
... ... @@ -16,6 +16,10 @@ function points2grid(x,y,m_map,varargin)
16 16 % plotted grid itself.
17 17 %
18 18  
  19 +% Options
  20 +% default
  21 +%
  22 +
19 23 % input size
20 24 [M,N] = size(x) ;
21 25  
... ... @@ -36,6 +40,10 @@ INTERPy = scatteredInterpolant(2*I,2*J,yvec(:), ...
36 40 X = INTERPx(indexx',indexy') ;
37 41 Y = INTERPy(indexx',indexy') ;
38 42  
  43 +% Save full grid for output
  44 +FX = X ;
  45 +FY = Y ;
  46 +
39 47 % Replace initial nan values
40 48 [I,J] = find(isnan(x) | isnan(y)) ;
41 49 for ii = 1:numel(I)
... ... @@ -184,4 +192,18 @@ for jj = 1:2:2*N+1
184 192 plot(X(:,jj),Y(:,jj),varargin{:})
185 193 end
186 194 end
  195 +
  196 +% Manage output
  197 +if nargout==2
  198 + varargout{1} = X ;
  199 + varargout{2} = Y ;
  200 +elseif nargout==4
  201 + varargout{1} = X ;
  202 + varargout{2} = Y ;
  203 + varargout{3} = FX ;
  204 + varargout{4} = FY ;
  205 +elseif nargout==0
  206 +else
  207 + varargout = [] ;
  208 +end
187 209 end
... ...