PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/other/Matlab/detect_ignition/ellipse_fit.m

http://github.com/jbeezley/wrf-fire
Objective C | 80 lines | 64 code | 16 blank | 0 comment | 4 complexity | bf4bebad8e5b897db763d918d2d85695 MD5 | raw file
Possible License(s): AGPL-1.0
  1. function [ ] = ellipse_fit( data,ci )
  2. %UNTITLED Summary of this function goes here
  3. % Detailed explanation goes here
  4. % Calculate the eigenvectors and eigenvalues
  5. covariance = cov(data);
  6. [eigenvec, eigenval ] = eig(covariance);
  7. % Get the index of the largest eigenvector
  8. [largest_eigenvec_ind_c, r] = find(eigenval == max(max(eigenval)));
  9. largest_eigenvec = eigenvec(:, largest_eigenvec_ind_c);
  10. % Get the largest eigenvalue
  11. largest_eigenval = max(max(eigenval));
  12. % Get the smallest eigenvector and eigenvalue
  13. if(largest_eigenvec_ind_c == 1)
  14. smallest_eigenval = max(eigenval(:,2))
  15. smallest_eigenvec = eigenvec(:,2);
  16. else
  17. smallest_eigenval = max(eigenval(:,1))
  18. smallest_eigenvec = eigenvec(1,:);
  19. end
  20. % Calculate the angle between the x-axis and the largest eigenvector
  21. angle = atan2(largest_eigenvec(2), largest_eigenvec(1));
  22. % This angle is between -pi and pi.
  23. % Let's shift it such that the angle is between 0 and 2pi
  24. if(angle < 0)
  25. angle = angle + 2*pi;
  26. end
  27. % Get the coordinates of the data mean
  28. avg = mean(data);
  29. % Get the 95% confidence interval error ellipse
  30. %chisquare_val = 2.4477;
  31. %chisquare_val = 2.2;
  32. chisquare_val = ci;
  33. theta_grid = linspace(0,2*pi);
  34. phi = angle;
  35. X0=avg(1);
  36. Y0=avg(2);
  37. a=chisquare_val*sqrt(largest_eigenval);
  38. b=chisquare_val*sqrt(smallest_eigenval);
  39. % the ellipse in x and y coordinates
  40. ellipse_x_r = a*cos( theta_grid );
  41. ellipse_y_r = b*sin( theta_grid );
  42. %Define a rotation matrix
  43. R = [ cos(phi) sin(phi); -sin(phi) cos(phi) ];
  44. %let's rotate the ellipse to some angle phi
  45. r_ellipse = [ellipse_x_r;ellipse_y_r]' * R;
  46. % Draw the error ellipse
  47. % axis square
  48. plot(r_ellipse(:,1) + X0,r_ellipse(:,2) + Y0,'-')
  49. hold on;
  50. % Plot the original data
  51. plot(data(:,1), data(:,2), '.');
  52. mindata = min(min(data));
  53. maxdata = max(max(data));
  54. xlim([mindata-3, maxdata+3]);
  55. ylim([mindata-3, maxdata+3]);
  56. hold on;
  57. % Plot the eigenvectors
  58. quiver(X0, Y0, largest_eigenvec(1)*sqrt(largest_eigenval), largest_eigenvec(2)*sqrt(largest_eigenval), '-m', 'LineWidth',2);
  59. quiver(X0, Y0, smallest_eigenvec(1)*sqrt(smallest_eigenval), smallest_eigenvec(2)*sqrt(smallest_eigenval), '-g', 'LineWidth',2);
  60. hold on;
  61. % Set the axis labels
  62. hXLabel = xlabel('x');
  63. hYLabel = ylabel('y');
  64. end