% Teal Bathke
% Fixed Wing Engine Configuration - 4 engines
% April 12, 1999
% Last Modified April 25, 1999
% function takes the inputs of flight_condition (1=takeoff/max_climb,
% 2=max-cruise,3=cruise,4=flight_idle),
% altitude h (ft), mach number M, and thrust T (lb)


function [T_to_in,T_max_in,T_1_in,T_2_in,T_3_in,T_4_in,T_5_in,T_6_in,T_idle_in,...
   wf_matrix_to,wf_matrix_max,wf_matrix_1,wf_matrix_2,wf_matrix_3,wf_matrix_4,wf_matrix_5,...
   wf_matrix_6,wf_matrix_idle,h_matrix,M_matrix]=installed(W_to,T_to_un)
% Values at all flight conditions are known for the same heights and 
% Mach numbers, so these matrices are used for calculations in all cases
   h_matrix = [0 10000 20000 30000 36089 40000 45000 50000; 
      			0 10000 20000 30000 36089 40000 45000 50000;
      			0 10000 20000 30000 36089 40000 45000 50000;
      			0 10000 20000 30000 36089 40000 45000 50000];
   M_matrix = [0.3 0.3 0.3 0.3 0.3 0.3 0.3 0.3; 
      			0.4 0.4 0.4 0.4 0.4 0.4 0.4 0.4; 
      			0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5; 
      			0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.7]; 
%Uninstalled engine data from manufacturer, for several flight conditions            
            
    % Takeoff Condition / Max Climb
   wf_matrix_to = [2632 2051 1544 1088 852 696 538 417; 
      			 2702 2111 1596 1129 886 724 558 433; 
       			 2785 2189 1660 1182 929 760 587 453; 
       			 2997 2379 1820 1322 1046 856 661 508]; 
   T_matrix_to = [4450 3589 2762 1947 1517 1225 940 722; 
      			 4120 3339 2599 1852 1447 1168 894 685;                
      			 3833 3152 2477 1790 1404 1134 864 662; 
                3424 2901 2340 1762 1399 1131 858 650];
	% Max Cruise
   wf_matrix_max = [2378 1876 1421 1018 800 654 503 388; 
      			 2438 1930 1467 1056 831 679 522 403; 
      			 2513 1994 1523 1104 871 712 547 421; 
      			 2697 2156 1666 1225 978 799 612 470]; 
   T_matrix_max = [4016 3307 2575 1859 1452 1174 892 681; 
                3688 3071 2417 1761 1382 1114 845 645; 
                3419 2877 2290 1696 1335 1079 815 620; 
                3021 2613 2146 1645 1323 1066 799 604]; 
   % Cruise 1
   wf_matrix_1 = [2108 1681 1286 929 739 601 462 356; 
      			 2156 1724 1324 962 767 623 479 369; 
      	   	 2219 1777 1374 1004 803 653 501 385; 
      			 2384 1911 1493 1109 895 728 558 429]; 
   T_matrix_1 = [3535 2977 2363 1721 1368 1095 831 634; 
      			 3218 2747 2200 1626 1293 1038 786 598; 
      			 2962 2552 2079 1559 1251 1001 754 572; 
                2594 2272 1908 1493 1220 976 732 552]; 
   % Cruise 2
   wf_matrix_2 = [1731 1391 1086 796 639 520 399 307; 
      			 1769 1423 1116 823 639 538 413 317; 
      			 1820 1464 1152 855 691 562 431 331; 
      			 1956 1571 1240 937 764 622 477 365]; 
   T_matrix_2 = [2832 2446 2018 1508 1215 971 732 555; 
      			 2557 2226 1868 1413 1143 913 687 520; 
      			 2317 2051 1741 1343 1093 874 657 495; 
                1979 1789 1555 1256 1046 835 624 467];
   % Cruise 3
   wf_matrix_3 = [1378 1113 877 658 535 435 333 255; 
      			 1411 1138 898 677 553 450 343 262; 
      			 1453 1171 925 701 575 467 357 273; 
      			 1586 1257 994 760 628 511 392 299]; 
   T_matrix_3 = [2160 1901 1614 1258 1033 823 617 463; 
      			 1920 1714 1471 1165 968 823 575 430; 
                1713 1551 1357 1095 914 772 575 430; 
                1453 1319 1186 994 849 676 502 373];  
   % Cruise 4
   wf_matrix_4 = [1171 943 747 564 464 377 287 220; 
      			 1204 966 763 580 478 388 296 226; 
      			 1248 995 786 599 495 402 308 235; 
      			 1367 1080 846 648 539 438 336 256]; 
   T_matrix_4 = [1765 1561 1349 1068 896 712 529 396; 
                1557 1392 1219 983 829 659 489 364; 
                1378 1245 1109 916 778 618 459 340; 
                1142 1055 956 818 709 563 417 308];  
   % Cruise 5
   wf_matrix_5 = [927 607 388 243 179 150 121 98; 
      			 975 638 407 254 187 158 127 102; 
      			 1043 682 434 271 200 168 135 109; 
      			 1237 808 512 318 235 197 158 128]; 
   T_matrix_5 = [1305 889 589 374 277 229 181 142; 
                1159 790 524 331 245 202 160 364; 
                1048 714 472 299 221 192 144 114; 
		          951 645 425 270 199 164 129 103];         
	% Cruise 6
   wf_matrix_6 = [610 400 257 161 119 100 80 64; 
      			 626 411 263 165 122 102 82 66; 
      			 652 427 273 171 126 106 85 68; 
      			 726 475 302 188 139 117 94 76]; 
   T_matrix_6 = [469 320 211 135 99 82 66 51; 
                333 226 149 94 70 58 46 36; 
                197 132 86 53 39 32 26 21; 
                -38 -30 -22 -16 -13 -11 -6 -6];              
 	% Flight Idle Condition
   wf_matrix_idle = [464 305 197 124 91 77 62 50; 
      			 469 308 198 125 92 78 62 50; 
      			 482 316 203 127 94 79 64 51; 
     				 524 342 219 137 102 85 69 55]; 
   T_matrix_idle = [236 161 106 67 51 42 33 26; 
                122 82 54 32 23 20 16 12; 
                -8 -7 -6 -5 -4 -3 -2 -1; 
                -263 -188 -123 -81 -62 -51 -39 -31]; 


T_to_engine = 4*5000;	% Turbofan uninstalled thrust--from manufacturer             
ESF =  T_to_un / T_to_engine;

% scale all data
   wf_matrix_to = 0.85 * ESF * wf_matrix_to; 
   T_matrix_to = ESF * T_matrix_to;
   wf_matrix_max = 0.85 * ESF * wf_matrix_max;
   T_matrix_max = ESF * T_matrix_max;
   wf_matrix_1 = 0.85 * ESF * wf_matrix_1;
   T_matrix_1 = ESF * T_matrix_1;
 	wf_matrix_2 = 0.85 * ESF * wf_matrix_2;
   T_matrix_2 = ESF * T_matrix_2;
	wf_matrix_3 = 0.85 * ESF * wf_matrix_3;
   T_matrix_3 = ESF * T_matrix_3;
	wf_matrix_4 = 0.85 * ESF * wf_matrix_4;
   T_matrix_4 = ESF * T_matrix_4;
	wf_matrix_5 = 0.85 * ESF * wf_matrix_5;
   T_matrix_5 = ESF * T_matrix_5;
	wf_matrix_6 = 0.85 * ESF * wf_matrix_6;
   T_matrix_6 = ESF * T_matrix_6;
	wf_matrix_idle = 0.85 * ESF * wf_matrix_idle;
   T_matrix_idle = ESF * T_matrix_idle;

% define inputs
% atmospheric data - commented out because universally declared for optimization
rho_o=2.377*10^-3;
mu_o=3.737*10^-7;
To=518.69;
T_o=To;
gamma=1.405;
g=32.2;
R=1716;
at=0.00357;

%constant installation factors
%W_to=50000;	% assumed constant originally, now imput to function
m_flow_eng=3000;	% need in slugs/sec
m_flow_bleed=0.03*m_flow_eng;	%(slugs/sec)
P_el =0.0007*W_to;	% (shp) for twin engine turbofan Table 6.1 pg 144
P_mech=0.0006*W_to;	% (shp) for twin engine turbofan Table 6.1 pg 144  
M_kt = [.2 .4 .6 .7 .8 .9];
kt_kt = [.08 .17 .29 .35 .43 .5]; 
kt_poly = polyfit(M_kt,kt_kt,3);
ninl=.99; % assumption for jet engine -- should be in 6.2.3.3

% change uninstalled takeoff data to installed data
M=[0.3 0.4 0.5 0.7];
h=[0 10000 20000 30000 36089 40000 45000 50000];

for i = 1:4,  % loop through Mach numbers
      for j = 1:8,  % loop through altitudes
      % calculate new atmospheric properties
      rho = rho_o * (1-at*h(j)/T_o)^(g/(at*R)-1);
		temp = T_o - at*h(j); %temp at various altitudes
      a = sqrt(gamma*R*temp);
      V = a * M(i);
		% find available installed thrust -- Roskam 'Airplane Design Part VI' 6.4.2.1 pg 198		
		P_pneum=(m_flow_bleed/m_flow_eng)*T_matrix_to(i,j)* V /550;	% (shp) for twin engine turbofan Table 6.1 pg 144
		P_extr = P_el + P_mech + P_pneum; % electrical, mechanical and pneumatic extraction 6.1.2 pg 145
      T_to_in(i,j) = T_matrix_to(i,j) * (1-0.35*polyval(kt_poly,M(i))*M(i)*(1-ninl))-550*P_extr/V; 
	   end	% inner loop
end	% outer loop   


% change uninstalled max cruise data to installed data
for i = 1:4,  % loop through Mach numbers
      for j = 1:8,  % loop through altitudes
      % calculate new atmospheric properties
      rho = rho_o * (1-at*h(j)/T_o)^(g/(at*R)-1);
      temp = T_o - at*h(j); %temp at various altitudes
      a = sqrt(gamma*R*temp);
      V = a * M(i);
		% find available installed thrust -- Roskam 'Airplane Design Part VI' 6.4.2.1 pg 198		
		P_pneum=(m_flow_bleed/m_flow_eng)*T_matrix_max(i,j)* V /550;	% (shp) for twin engine turbofan Table 6.1 pg 144
		P_extr = P_el + P_mech + P_pneum; % electrical, mechanical and pneumatic extraction 6.1.2 pg 145
      T_max_in(i,j) = T_matrix_max(i,j) * (1-0.35*polyval(kt_poly,M(i))*M(i)*(1-ninl))-550*P_extr/V; 
	   end	% inner loop
end	% outer loop   


% change uninstalled cruise1 data to installed data
for i = 1:4,  % loop through Mach numbers
      for j = 1:8,  % loop through altitudes
      % calculate new atmospheric properties
      rho = rho_o * (1-at*h(j)/T_o)^(g/(at*R)-1);
      temp = T_o - at*h(j); %temp at various altitudes
      a = sqrt(gamma*R*temp);
      V = a * M(i);
		% find available installed thrust -- Roskam 'Airplane Design Part VI' 6.4.2.1 pg 198		
		P_pneum=(m_flow_bleed/m_flow_eng)*T_matrix_1(i,j)* V /550;	% (shp) for twin engine turbofan Table 6.1 pg 144
		P_extr = P_el + P_mech + P_pneum; % electrical, mechanical and pneumatic extraction 6.1.2 pg 14
      T_1_in(i,j) = T_matrix_1(i,j) * (1-0.35*polyval(kt_poly,M(i))*M(i)*(1-ninl))-550*P_extr/V; 
	   end	% inner loop
end	% outer loop   


% change uninstalled cruise2 data to installed data
for i = 1:4,  % loop through Mach numbers
      for j = 1:8,  % loop through altitudes
      % calculate new atmospheric properties
      rho = rho_o * (1-at*h(j)/T_o)^(g/(at*R)-1);
      temp = T_o - at*h(j); %temp at various altitudes
      a = sqrt(gamma*R*temp);
      V = a * M(i);
		% find available installed thrust -- Roskam 'Airplane Design Part VI' 6.4.2.1 pg 198		
		P_pneum=(m_flow_bleed/m_flow_eng)*T_matrix_2(i,j)* V /550;	% (shp) for twin engine turbofan Table 6.1 pg 144
		P_extr = P_el + P_mech + P_pneum; % electrical, mechanical and pneumatic extraction 6.1.2 pg 14
      T_2_in(i,j) = T_matrix_2(i,j) * (1-0.35*polyval(kt_poly,M(i))*M(i)*(1-ninl))-550*P_extr/V; 
	   end	% inner loop
end	% outer loop 


% change uninstalled cruise3 data to installed data
for i = 1:4,  % loop through Mach numbers
      for j = 1:8,  % loop through altitudes
      % calculate new atmospheric properties
      rho = rho_o * (1-at*h(j)/T_o)^(g/(at*R)-1);
      temp = T_o - at*h(j); %temp at various altitudes
      a = sqrt(gamma*R*temp);
      V = a * M(i);
		% find available installed thrust -- Roskam 'Airplane Design Part VI' 6.4.2.1 pg 198		
		P_pneum=(m_flow_bleed/m_flow_eng)*T_matrix_3(i,j)* V /550;	% (shp) for twin engine turbofan Table 6.1 pg 144
		P_extr = P_el + P_mech + P_pneum; % electrical, mechanical and pneumatic extraction 6.1.2 pg 14
      T_3_in(i,j) = T_matrix_3(i,j) * (1-0.35*polyval(kt_poly,M(i))*M(i)*(1-ninl))-550*P_extr/V; 
	   end	% inner loop
end	% outer loop


% change uninstalled cruise4 data to installed data
for i = 1:4,  % loop through Mach numbers
      for j = 1:8,  % loop through altitudes
      % calculate new atmospheric properties
      rho = rho_o * (1-at*h(j)/T_o)^(g/(at*R)-1);
      temp = T_o - at*h(j); %temp at various altitudes
      a = sqrt(gamma*R*temp);
      V = a * M(i);
		% find available installed thrust -- Roskam 'Airplane Design Part VI' 6.4.2.1 pg 198		
		P_pneum=(m_flow_bleed/m_flow_eng)*T_matrix_4(i,j)* V /550;	% (shp) for twin engine turbofan Table 6.1 pg 144
		P_extr = P_el + P_mech + P_pneum; % electrical, mechanical and pneumatic extraction 6.1.2 pg 14
      T_4_in(i,j) = T_matrix_4(i,j) * (1-0.35*polyval(kt_poly,M(i))*M(i)*(1-ninl))-550*P_extr/V; 
	   end	% inner loop
end	% outer loop 


% change uninstalled cruise5 data to installed data
for i = 1:4,  % loop through Mach numbers
      for j = 1:8,  % loop through altitudes
      % calculate new atmospheric properties
      rho = rho_o * (1-at*h(j)/T_o)^(g/(at*R)-1);
      temp = T_o - at*h(j); %temp at various altitudes
      a = sqrt(gamma*R*temp);
      V = a * M(i);
		% find available installed thrust -- Roskam 'Airplane Design Part VI' 6.4.2.1 pg 198		
		P_pneum=(m_flow_bleed/m_flow_eng)*T_matrix_5(i,j)* V /550;	% (shp) for twin engine turbofan Table 6.1 pg 144
		P_extr = P_el + P_mech + P_pneum; % electrical, mechanical and pneumatic extraction 6.1.2 pg 14
      T_5_in(i,j) = T_matrix_5(i,j) * (1-0.35*polyval(kt_poly,M(i))*M(i)*(1-ninl))-550*P_extr/V; 
	   end	% inner loop
end	% outer loop 


% change uninstalled cruise6 data to installed data
for i = 1:4,  % loop through Mach numbers
      for j = 1:8,  % loop through altitudes
      % calculate new atmospheric properties
      rho = rho_o * (1-at*h(j)/T_o)^(g/(at*R)-1);
      temp = T_o - at*h(j); %temp at various altitudes
      a = sqrt(gamma*R*temp);
      V = a * M(i);
		% find available installed thrust -- Roskam 'Airplane Design Part VI' 6.4.2.1 pg 198		
		P_pneum=(m_flow_bleed/m_flow_eng)*T_matrix_6(i,j)* V /550;	% (shp) for twin engine turbofan Table 6.1 pg 144
		P_extr = P_el + P_mech + P_pneum; % electrical, mechanical and pneumatic extraction 6.1.2 pg 14
      T_6_in(i,j) = T_matrix_6(i,j) * (1-0.35*polyval(kt_poly,M(i))*M(i)*(1-ninl))-550*P_extr/V; 
	   end	% inner loop
end	% outer loop 


% change uninstalled idle data to installed data
for i = 1:4,  % loop through Mach numbers
      for j = 1:8,  % loop through altitudes
      % calculate new atmospheric properties
      rho = rho_o * (1-at*h(j)/To)^(g/(at*R)-1);
      temp = To - at*h(j); %temp at various altitudes
      a = sqrt(gamma*R*temp);
      V = a * M(i);
		% find available installed thrust -- Roskam 'Airplane Design Part VI' 6.4.2.1 pg 198		
		P_pneum=(m_flow_bleed/m_flow_eng)*T_matrix_idle(i,j)* V /550;	% (shp) for twin engine turbofan Table 6.1 pg 144
		P_extr = P_el + P_mech + P_pneum; % electrical, mechanical and pneumatic extraction 6.1.2 pg 14
      T_idle_in(i,j) = T_matrix_idle(i,j) * (1-0.35*polyval(kt_poly,M(i))*M(i)*(1-ninl))-550*P_extr/V; 
	   end	% inner loop
end	% outer loop 

