close all
clear all

[rho S Cp C ] = Constants();
C4 = C/4;

spine_pts = [-38.55       35.45         35.45          -38.55        -38.55 ;
    11.28        11.28         5.28           5.28         11.28]; %inches

wing_ctr = 0;%[78.45:-14:-76];
wing_ctr(2,:) = 8.28*ones(1,length(wing_ctr(1,:)));

spine_pts = .0254*spine_pts;  %meters
wing_ctr = 0.0254*wing_ctr;  %meters

Chord = C;
ctr = Cp;

V = [.25 .5 .75  1  1];
theta = [-60:.2:60];

fh1 = figure('Position',[ 1000     107    1147    1391]);
ah1(1) = subplot(2,1,1); hold all; grid on;
ah1(2) = subplot(2,1,2); hold all; grid on;

fh2 = figure('Position',[ 1000     107    1147    1391]);
ah2(1) = subplot(3,1,1); hold all; grid on;
ah2(2) = subplot(3,1,2); hold all; grid on;
ah2(3) = subplot(3,1,2); hold all; grid on;

for k = 1:length(V)
    v_w(1,:) = -V(k)*cos(theta*pi/180);
    v_w(2,:) = -V(k)*sin(theta*pi/180);
    
    if(k<length(V))
      for i = 1:length(theta)
          [alpha(i) F(:,i)] = ActualWingAngle(theta(i),V(k));
          leg_str{k} = ['|V| = ' num2str(V(k)) ' m/s'];
      end
    else
      for i = 1:length(theta)
          [alpha(i) F(:,i)] = OptimumWingAngle(theta(i),0);
          leg_str{k} = ['Optimum Wing Angle'];
      end    
    end
    
    AOA = alpha-theta;
    idx = find(AOA < -180);
    AOA(idx) = AOA(idx)+360;
    idx = find(AOA > 180);
    AOA(idx) = AOA(idx)-360;
    
    figure(fh1)
    subplot(2,1,1);
    plot(theta,alpha);
    xlabel('Theta (water angle)');
    ylabel('Alpha (wing angle)');
    
    subplot(2,1,2);
    plot(theta,AOA)
    xlabel('Theta (water angle)');
    ylabel('Wing AOA (degrees)');
    
    figure(fh2)
    subplot(3,1,1);
    plot(theta,F(1,:))
    xlabel('Theta (water angle)');
    ylabel('Fx');
    subplot(3,1,2);
    plot(theta,F(2,:))
    xlabel('Theta (water angle)');
    ylabel('Fy');
    subplot(3,1,3);
    plot(theta,F(3,:))
    xlabel('Theta (water angle)');
    ylabel('M');

    
end

    linkaxes(ah1,'x');
    linkaxes(ah2,'x');

figure(fh1)
for k = 1:2
  subplot(2,1,k)
  legend(leg_str);
end

figure(fh2)
for k = 1:3
  subplot(3,1,k)
  legend(leg_str);
end

