% 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=cruise,4=flight_idle),
% altitude h (ft), mach number M, and thrust T (lb)


function [fuelflow,T]=interpolation(flight_condition,h,M,T,...
		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)
   
%takeoff/max climb
if flight_condition == 1
	wf_to = interp2(h_matrix,M_matrix,wf_matrix_to,h,M);
	T = interp2(h_matrix,M_matrix,T_to_in,h,M);
   fuelflow=wf_to;
%cruise   
elseif flight_condition == 2
	wf = interp2(h_matrix,M_matrix,wf_matrix_max,h,M);
	T_cr = interp2(h_matrix,M_matrix,T_max_in,h,M);
   
  	wf_1 = interp2(h_matrix,M_matrix,wf_matrix_1,h,M);
	T_1 = interp2(h_matrix,M_matrix,T_1_in,h,M);
   
   wf_2 = interp2(h_matrix,M_matrix,wf_matrix_2,h,M);
	T_2 = interp2(h_matrix,M_matrix,T_2_in,h,M);

 	wf_3 = interp2(h_matrix,M_matrix,wf_matrix_3,h,M);
	T_3 = interp2(h_matrix,M_matrix,T_3_in,h,M);

  	wf_4 = interp2(h_matrix,M_matrix,wf_matrix_4,h,M);
	T_4 = interp2(h_matrix,M_matrix,T_4_in,h,M);
   
   wf_5 = interp2(h_matrix,M_matrix,wf_matrix_5,h,M);
 	T_5 = interp2(h_matrix,M_matrix,T_5_in,h,M);      

 	wf_6 = interp2(h_matrix,M_matrix,wf_matrix_6,h,M);
   T_6 = interp2(h_matrix,M_matrix,T_6_in,h,M);
   %interpolation between cruise conditions
   wf=[wf,wf_1,wf_2,wf_3,wf_4,wf_5,wf_6];
   T_vec=[T_cr,T_1,T_2,T_3,T_4,T_5,T_6];
   [wf_poly,s]=polyfit(T_vec,wf,3);
   wf_cruise=polyval(wf_poly,T);
   %c_j_cruise=T/(wf_cruise*4);
	fuelflow=wf_cruise;
   
else
	wf = interp2(h_matrix,M_matrix,wf_matrix_idle,h,M);
   T = interp2(h_matrix,M_matrix,T_idle_in,h,M);
   fuelflow=wf;
end	% if statement





