%
% EGR 301 HW 1:  Matlab
% February 1, 2005
%
% SOLUTIONS
%


%
% Problem #1:  Circuit analysis by inspection
%  Use Ohms' law, V = IR or  I = inv(R)*V
%
R = [ 6  -2   0
     -2  12  -2
      0  -2   7 ]
V = [ 12 -8 -20]'
I = inv(R)*V



%
% Problem #2:  Matrix manipulation
%
A = [ 1 2 3 4
      2 3 4 5
      6 7 8 9
      5 6 3 2 ]
  
%
% Matrix B is matrix A with row 3 removed.  There is more than one way to
% do this
B1 = [A(1,:); A(2,:); A(4,:)]
B2 = [A(1:2,:); A(4,:)]

%
% Matrix C is columns 1 and 2 of matrix A
C = [A(:,1) A(:,2)]

%
% Matrix D is the center 4 elements of matrix A
D = A(2:3, 2:3)

%
% Matrix E is a 4x4 matrix with only the diagonal elements of A
E = diag(diag(A))

%
% Vector F is a 7x1 vector from 2 to 5 with 0.5 step between
F = [2: 0.5: 5]'

%
%  PLOTTING in MATLAB
%

%
% Problem 3: Create an interesting, and well-labeled, 2D plot
%
x = [0: 5: 100];
y1 = x.^0.2 + 0.1*tan(x);
y2 = sin(2*x) + cos(x);
plot (x, y1, 'b--')
hold on
plot (x, y2, 'c:')
hold off
title ('EGR HW 1: 2-D Plot')
xlabel ('year')
ylabel ('Function Behavior')
legend ('x^.2 + 0.1*tan(x)','sin(2*x) + cos(x)',4)


%
% Problem 4: Create an interesting, and well-labeled, 3D plot
%
g = [0: 0.1: 2*pi];
[x, y] = meshgrid(g);
z = cos(x) + sin(y);
mesh(z);
title ('Mesh Plot of  [cos(x) + sin(y)]');
ylabel ('degrees')
xlabel ('degrees')
zlabel ('amplitude')