% 3D visualization of x'Ax written by kihwan choi % A is a symmetric matrix in R^3 clear all; close all; randn('state', 1); A = randn(3); % generate a random matrix in R^3 A = (A+A')/2; % update A with the symmertic part of A % generate a sphere with n-by-n faces n = 100; % larger n higher resolution [X Y Z] = sphere(n); % assign x'Ax as the color value for i = 1:n+1 for j = 1:n+1 C(i,j) = [X(i,j);Y(i,j);Z(i,j)]'*A*[X(i,j);Y(i,j);Z(i,j)]; end end % plot the result surf(X,Y,Z,C,'Edgecolor','none'); axis square;