clear
echo on
%Solution to Problem 8
%This script uses the attached function
%classnorm.m which must be split off and
%saved separately.
%
%Solution to part a:
%We define the vector x:
x = [1, 2, 3, 4]
%and call the function classnorm:
[manhattan euclid infinity]=classnorm(x)
pause
%we can compare these to the matlab norms:
[manhattan norm(x,1)]
[euclid norm(x,2)]
[infinity norm(x,inf)]
%so they all match.
pause


%Solution to part b:
%First we set up the matrix A:
a=[1,2,3;4,5,6;7,8,10]
pause
%Next we set up the angles theta
%and phi:
theta=[0:.02:pi];
phi=[0:.02:pi];
ipts=length(theta);
jpts=length(phi);

%Now for the for loop:
for i=1:ipts
 for j=1:jpts
  %We set up the vector x:
  x=[sin(theta(i))*cos(phi(j));sin(theta(i))*sin(phi(j));cos(theta(i))];
  %And the right hand side:
  b=a*x;
  %Now for the norm:
  ratio(i,j)=norm(b,1)/norm(x,1);
  echo off
 end
end
echo on
pause
%We can now plot this:
mesh(theta,phi,ratio)
%I hope that you didn't make the
%discretization in theta and phi too
%large!  Now we look at the maximum:
maxratio=max(max(ratio))
pause
%which is agrees with the actual value of
%10+6+3=19.  The minimum is:
minratio=min(min(ratio))
%We can compare this to the columns of 
%inv(A):
pause
inv(a)
%So the true minimum should be 1/7.  The
%value estimated from the plot is 1/6.25
%for this degree of discretization.
pause
%Finally, the condition number is (from
%the plot, anyway):
condition_a=maxratio/minratio
%which is close to the exact condition
%number of 7 * 19 = 133.  The matlab
%estimate is:
cond(a)
%which is about 2/3 of the value calculated
%above.  This is because matlab calculates
%the condition number using the 2-norm rather
%than the manhattan norm as we have done.
pause
%The 1-norm estimate is provided by the function
%condest(a):
condest(a)
%which equals the true maximum value.  For
%larger matrices the estimated value would not
%match the exact value, but would be close.  The
%1-norm is always larger than the 2-norm, as
%may be easily proved.
echo off


__________________________


function [man,euclid,infinity]=classnorm(x)
%This function calculates the manhattan,
%euclidean, and infinity norms of a vector
%x and returns all three as a row vector.
man = sum(abs(x));
euclid = (sum(x.*x))^.5;
infinity = max(abs(x));