close all
clear all

xlsfilenm = '3551-whoi-stretch hoses-tensile-stretch data.xls';

Location = xlsread(xlsfilenm,'A10:A31');
Location(end+1) = 450;  %Assuming the flange is 10cm beyond last mark, need to check.

TensionLoc = {'B7:K7','M7:W7','Y7:AJ7','AM7:AV7'};
ElonLoc = {'B10:K32','M10:W32','Y10:AJ32','AM10:AV32'};

figure('Position',[1000   82    1205    1416],'PaperPositionMode','manual','PaperOrientation','portrait','PaperType','usletter','PaperPosition',[0 0 8.5 11]);
figure('Position',[1000   82    1205    1416],'PaperPositionMode','manual','PaperOrientation','portrait','PaperType','usletter','PaperPosition',[0 0 8.5 11]);

for i = 1:4
  Tension{i} = xlsread(xlsfilenm,TensionLoc{i});
  Elon{i} = xlsread(xlsfilenm,ElonLoc{i});
  Delta{i} = diff(Elon{i})-diff(Location)*ones(1,size(Elon{i},2));
  Strain{i} = Delta{i}./(diff(Location)*ones(1,size(Elon{i},2)));
  
%Switch the first two tensions in Cycle 3 to make tensions monotonically
%increaseing at first (it looks like the 500lb result was recorded, then
%the 200lb result, and then the 1000lb result)
  if(i == 3)
    Tension{3}(1:2) = Tension{3}(2:-1:1);
    Strain{3}(:,1:2) = Strain{3}(:,2:-1:1);
  end
  
  figure(1)
  ah1(i) = subplot(4,1,i);
  plot(Location(1:end),Elon{i},'-x');
  title(['Cycle ' num2str(i)]);
  %ylim([-1 3]);
  xlabel('Distance along unstretched hose (cm)');
  ylabel('Elongation');
  clear leg_str;
  for j = 1:length(Tension{i})
     leg_str{j} = ([num2str(Tension{i}(j)) ' lbs']); 
  end
  legend(leg_str,'Location','EastOutside');
  
  figure(2)
  ah2(i) = subplot(4,1,i);
  plot(Location(1:end-1)-diff(Location)/2,Strain{i},'-x');
  title(['Cycle ' num2str(i)]);
  ylim([-1 3]);
  xlabel('Distance along unstretched hose (cm)');
  ylabel('Strain (delta L/L)');
  clear leg_str;
  for j = 1:length(Tension{i})
     leg_str{j} = ([num2str(Tension{i}(j)) ' lbs']); 
  end
  legend(leg_str,'Location','EastOutside');
end

figure(1)
linkaxes(ah1,'x');
print('-dpdf','Elongation.pdf');

figure(2)
linkaxes(ah2,'x');
print('-dpdf','Strain.pdf');


%Compute stress/strain for middle three data points, i.e. complaint section
% The middle three points are points 10,11,12.
MiddlePoints = [10 11 12];
%Remove particularily bogus points
Strain{1}(MiddlePoints(1),5) = NaN;
Strain{3}(MiddlePoints(1),4) = NaN;
Strain{3}(MiddlePoints(2),4) = NaN;


ColorString = {'-kx','-rx','-bx','-gx'};
clear leg_str;
figure('Position',[1000   82    1205    1416],'PaperPositionMode','manual','PaperOrientation','portrait','PaperType','usletter','PaperPosition',[0 0 8.5 11]);
for i = 1:4;
plot(Strain{i}(MiddlePoints,:),Tension{i},ColorString{i})
hold on
leg_str{3*i-2} = ['Cycle ' num2str(i) ' Middle Pt 1'];
leg_str{3*i-1} = ['Cycle ' num2str(i) ' Middle Pt 2'];
leg_str{3*i} = ['Cycle ' num2str(i) ' Middle Pt 3'];
Strain{i} = Strain{i}(MiddlePoints,:);  %Simplify for saving later
end
%xlim([0 2.2]);
legend(leg_str,'Location','NorthWest');
xlabel('Strain based on original length');
ylabel('Tension (lbs)');
title('Tension-Strain Curves for Fully Compliant Section');
print('-dpdf','ComplaintTensionStrain.pdf');

save('CompliantSectionStrainFromTMTTests','Tension','Strain');

%Compute and Plot Measured Mean Strain and Estimated Mean Strain
L0 = 4.5;
a = 2.0;
StrainFactor = (1-4*a/(3*L0));
for i = 1:4;
   MeanStrain{i} = (Elon{i}(end,:)-Location(end))/Location(end);
end
ColorString = {'-kx','-rx','-bx','-gx'};
clear leg_str;
figure('Position',[1000   82    1205    1416],'PaperPositionMode','manual','PaperOrientation','portrait','PaperType','usletter','PaperPosition',[0 0 8.5 11]);
for i = 1:4;
plot(MeanStrain{i},Tension{i},ColorString{i})
leg_str{i} = ['Measured Mean Strain: cycle # ' num2str(i)];
hold all
end
ColorString = {'-ko','-ro','-bo','-go'};
for i = 1:4;
   epsilon_c = mean(Strain{i});  %Estimated complain section strain
   plot(StrainFactor*epsilon_c,Tension{i},ColorString{i})
   leg_str{4+i} = ['Estimated Mean Strain: cycle # ' num2str(i)];
end
legend(leg_str,'Location','NorthWest');

