This repository has been archived on 2021-05-17. You can view files and clone it, but cannot push or open issues or pull requests.
scientificComputing/linearalgebra/code/matrixbox.m
2014-11-12 18:39:02 +01:00

46 lines
1.7 KiB
Matlab

function matrixbox( m, s )
% visualizes the effect of a matrix m on a set of vectors forming a box
% m: a 2x2 matrix
% s: a string plotted as the title of the plot
% this function is called by matrix2dtrafos.m
% the 5 vectors that point into the cornes of a unit square:
v = [ 0 1 1 0 0;
0 0 1 1 0 ];
% transform v by means of m into new vector w:
w = m*v;
clf;
hold on;
% axis:
plot( [-2 2], [0 0], 'k', 'LineWidth', 1 );
plot( [0 0], [-2 2], 'k', 'LineWidth', 1 );
% old vectors:
plot( v(1,:), v(2,:), 'k', 'LineWidth', 1.0 )
quiver( v(1,1), v(2,1), v(1,3), v(2,3), 1.0, 'k', 'LineWidth', 2.0 );
scatter( v(1,2), v(2,2), 60.0, 'filled', 'k' );
% transformed vectors:
plot( w(1,:), w(2,:), 'b', 'LineWidth', 2.0 )
quiver( w(1,1), w(2,1), w(1,3), w(2,3), 1.0, 'b', 'LineWidth', 3.0 );
scatter( w(1,2), w(2,2), 100.0, 'filled', 'b' );
% eigenvectors:
[ev ed] = eig(m);
n = ev*ed;
if isreal( n )
quiver( [0 0], [0 0], n(1,:), n(2,:), 1.0, 'r', 'LineWidth', 2.0 );
text( 0.1, 0.2, sprintf( '\\lambda_1 = %.3g', ed(1,1) ), 'Units', 'normalized' )
text( 0.1, 0.1, sprintf( '\\lambda_2 = %.3g', ed(2,2) ), 'Units', 'normalized' )
end
hold off;
xlim( [ -2 2 ] );
ylim( [ -2 2 ] );
axis( 'equal' );
m( abs(m) < 1e-3 ) = 0.0; % make zeros really a zero
text( 0.7, 0.15, 'A =', 'Units', 'normalized' )
text( 0.8, 0.2, sprintf( '%.3g', m(1,1) ), 'Units', 'normalized' )
text( 0.9, 0.2, sprintf( '%.3g', m(1,2) ), 'Units', 'normalized' )
text( 0.8, 0.1, sprintf( '%.3g', m(2,1) ), 'Units', 'normalized' )
text( 0.9, 0.1, sprintf( '%.3g', m(2,2) ), 'Units', 'normalized' )
title( s );
waitforbuttonpress;
end