PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/other/Matlab/detect_ignition/ellipse_3d.m

http://github.com/jbeezley/wrf-fire
Objective C | 160 lines | 123 code | 37 blank | 0 comment | 10 complexity | c8823a236fccb9fab541bbc8a6003759 MD5 | raw file
Possible License(s): AGPL-1.0
  1. function [ ] = ellipse_fit( data,ci ,rate_vector,test_flag)
  2. % function takes in a matrix of points (data) and a confidence interval
  3. % (ci)and plots a 3d cone of the fire. rate_vector is best guess as what
  4. % direction in which the fire spreads most rapidly
  5. %test_flag =1 tells function you are
  6. % using random data. Used to scale figure window
  7. % fitting of ellipse based on code from
  8. % http://www.visiondummy.com/2014/04/draw-error-ellipse-representing-covariance-matrix/
  9. covariance = cov(data);
  10. [eigenvec, eigenval ] = eig(covariance);
  11. % Get the index of the largest eigenvector
  12. [largest_eigenvec_ind_c, r] = find(eigenval == max(max(eigenval)));
  13. largest_eigenvec = eigenvec(:, largest_eigenvec_ind_c);
  14. % Get the largest eigenvalue
  15. largest_eigenval = max(max(eigenval));
  16. % Get the smallest eigenvector and eigenvalue
  17. if(largest_eigenvec_ind_c == 1)
  18. smallest_eigenval = max(eigenval(:,2));
  19. smallest_eigenvec = eigenvec(:,2);
  20. else
  21. smallest_eigenval = max(eigenval(:,1));
  22. smallest_eigenvec = eigenvec(1,:);
  23. end
  24. % Calculate the angle between the x-axis and the largest eigenvector
  25. angle = atan2(largest_eigenvec(2), largest_eigenvec(1));
  26. % This angle is between -pi and pi.
  27. % Let's shift it such that the angle is between 0 and 2pi
  28. if(angle < 0)
  29. angle = angle + 2*pi;
  30. end
  31. % Get the coordinates of the data mean
  32. avg = mean(data);
  33. % Get the 95% confidence interval error ellipse
  34. %chisquare_val = 2.4477;
  35. %chisquare_val = 2.2;
  36. chisquare_val = ci;
  37. %parameters of initial ellipse x^2/a^2 + y^2/b^2 = 1
  38. phi = angle;
  39. X0=avg(1);
  40. Y0=avg(2);
  41. a=chisquare_val*sqrt(largest_eigenval);
  42. b=chisquare_val*sqrt(smallest_eigenval);
  43. %set up mesh
  44. theta_incs = 40;
  45. theta_grid = linspace(0,2*pi,theta_incs);
  46. time_incs = 20;
  47. time_grid = linspace(0,1,time_incs);
  48. [u,t] = meshgrid(theta_grid,time_grid);
  49. % the ellipse in x and y coordinates
  50. ellipse_x_r = a*cos( theta_grid );
  51. ellipse_y_r = b*sin( theta_grid );
  52. %Define a rotation matrix
  53. R = [ cos(phi) sin(phi); -sin(phi) cos(phi) ];
  54. %let's rotate the ellipse to some angle phi
  55. r_ellipse = [ellipse_x_r;ellipse_y_r]' * R;
  56. % Draw the error ellipse
  57. % axis square
  58. plot(r_ellipse(:,1) + X0,r_ellipse(:,2) + Y0,'-')
  59. hold on;
  60. %find dot product between rate_vector and axis of ellipse
  61. %largest_eigenvec
  62. axis_dot = dot(largest_eigenvec,rate_vector);
  63. %find location of focus of ellipse
  64. f = sqrt(a^2-b^2);
  65. %if axis_dot >=0
  66. if (axis_dot >= 0)
  67. f_x = X0-f*cos(phi);
  68. f_y = Y0-f*sin(phi);
  69. %if axis_dot <0
  70. else
  71. f_x = X0+f*cos(phi);
  72. f_y = Y0+f*sin(phi);
  73. end %if
  74. format long g
  75. disp('Coordinates of focus: ')
  76. fprintf('Lon: %d Lat: %d \n',f_x,f_y)
  77. %plot location of focus of ellipse
  78. plot(f_x,f_y,'*');
  79. %generate surface for unrotated system
  80. if (axis_dot >= 0)
  81. x_s = (f*t + a*cos(u).*t);
  82. y_s = b*sin(u).*t;
  83. else
  84. x_s = -(f*t + a*cos(u).*t);
  85. y_s = -b*sin(u).*t;
  86. end %if
  87. z_s = t;
  88. %Define a rotation matrix
  89. rot = [cos(phi) sin(phi) ; sin(phi) -cos(phi) ];
  90. %rotate layers and shift
  91. x_r = zeros(time_incs,theta_incs);
  92. y_r = x_r;
  93. for i = 1:time_incs
  94. new = rot*[x_s(i,:);y_s(i,:)];
  95. x_r(i,:) = f_x + new(1,:);
  96. y_r(i,:) = f_y + new(2,:);
  97. end
  98. %plot cone surface
  99. view(3)
  100. surfc(x_r,y_r,z_s)
  101. hold on
  102. % Draw the error ellipse
  103. plot(r_ellipse(:,1) + X0,r_ellipse(:,2) + Y0,'-')
  104. hold on;
  105. % Plot the original data
  106. plot(data(:,1), data(:,2), '.');
  107. x_min = min(data(:,1));
  108. x_max = max(data(:,1));
  109. y_min = min(data(:,2));
  110. y_max = max(data(:,2));
  111. xlim([x_min-0.04,x_max+0.04]);
  112. ylim([y_min-0.04,y_max+0.04]);
  113. if test_flag == 1
  114. mindata = min(min(data));
  115. maxdata = max(max(data));
  116. xlim([mindata-3, maxdata+3]);
  117. ylim([mindata-3, maxdata+3]);
  118. end
  119. hold on;
  120. % Plot the eigenvectors
  121. quiver(X0, Y0, -largest_eigenvec(1)*sqrt(largest_eigenval), -largest_eigenvec(2)*sqrt(largest_eigenval), '-m', 'LineWidth',2);
  122. %quiver(X0, Y0, smallest_eigenvec(1)*sqrt(smallest_eigenval), smallest_eigenvec(2)*sqrt(smallest_eigenval), '-g', 'LineWidth',2);
  123. end