fork download
  1. % Define the matrix A (replace with your own matrix)
  2. A = rand(5);
  3.  
  4. % Method 1: Using eig() function (QR Method)
  5. tic; % Start the timer
  6. eigenvalues_qr = eig(A);
  7. time_qr = toc; % End the timer
  8.  
  9. % Method 2: Using eig() function with hess() (Hessenberg reduction)
  10. tic; % Start the timer
  11. H = hess(A); % Reduce A to Hessenberg form
  12. eigenvalues_hess = eig(H);
  13. time_hess = toc; % End the timer
  14.  
  15. % Display eigenvalues
  16. fprintf('Eigenvalues (QR Method): \n');
  17. disp(eigenvalues_qr);
  18.  
  19. fprintf('Eigenvalues (Hessenberg Reduction): \n');
  20. disp(eigenvalues_hess);
  21.  
  22. % Display runtimes
  23. fprintf('Runtime (QR Method): %.6f seconds\n', time_qr);
  24. fprintf('Runtime (Hessenberg Reduction): %.6f seconds\n', time_hess);
  25.  
  26. % Create a bar plot to compare runtimes
  27. methods = {'QR Method', 'Hessenberg Reduction'};
  28. runtimes = [time_qr, time_hess];
  29. bar(runtimes); % Bar plot without labels
  30. xticklabels(methods); % Set x-axis labels
  31. title('Eigenvalue Computation Runtimes');
  32. ylabel('Time (seconds)');
  33.  
Success #stdin #stdout 0.21s 55720KB
stdin
Standard input is empty
stdout
Eigenvalues (QR Method): 
   2.30755 + 0.00000i
   0.05547 + 0.74257i
   0.05547 - 0.74257i
   0.01394 + 0.00000i
   0.32167 + 0.00000i
Eigenvalues (Hessenberg Reduction): 
   2.30755 + 0.00000i
   0.05547 + 0.74257i
   0.05547 - 0.74257i
   0.01394 + 0.00000i
   0.32167 + 0.00000i
Runtime (QR Method): 0.031692 seconds
Runtime (Hessenberg Reduction): 0.000568 seconds