% Delivered to the Government in Accordance with Contract FA8650-10-C-7080
% 
%  (c) 2011 United Technologies Corp.
% Developed with the sponsorship of the Defense Advanced Research Projects Agency (DARPA).
%  
% Permission is hereby granted, free of charge, to any person obtaining a copy of this data, including any software or models in source or binary form, as well as any drawings, specifications, and documentation (collectively "the Data"), to deal in the Data without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Data, and to permit persons to whom the Data is furnished to do so, subject to the following conditions:
%  
% The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Data.
%  
% THE DATA IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS, SPONSORS, DEVELOPERS, CONTRIBUTORS, OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE DATA OR THE USE OR OTHER DEALINGS IN THE DATA.
% 

function [f] = equation(iteration_var, iteration_vector,T_amb, P_amb, Mach, Req_Thrust)
    
    global PR_Fan_Tr PR_LPC_Tr PR_HPC_Tr PR_HPT_Tr PR_LPT_Tr
    global Mass_Fan_Tr Mass_LPC_Tr Mass_HPC_Tr Mass_HPT_Tr Mass_LPT_Tr
    global Efficiency_Fan_Tr Efficiency_LPC_Tr Efficiency_HPC_Tr Efficiency_HPT_Tr Efficiency_LPT_Tr
    global D_Fan D_LPC D_HPC D_HPT D_IPT D_LPT D_N D_BN 
   
    warning off all;
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    eval(iteration_vector); 

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Part 2: Primary Gas Path Model (Almost identical to AMPL)

    % Diffuser
    [Gas_Diffuser] = Diffuser(T_amb,P_amb, Mach);

    % Fan Equations
    [Gas_Fan, Mech_Fan] = Fan(Gas_Diffuser, Speed_Fan, Beta_Fan, Mass_Fan_Tr, PR_Fan_Tr, Efficiency_Fan_Tr, D_Fan);
    
    % LPC equations
    [Gas_LPC, Mech_LPC] = LPC(Gas_Fan, Speed_LPC, Beta_LPC, Mass_LPC_Tr, PR_LPC_Tr, Efficiency_LPC_Tr, D_LPC);

    % HPC equations
    [Gas_HPC, Mech_HPC] = HPC(Gas_LPC, Speed_HPC, Beta_HPC, Mass_HPC_Tr, PR_HPC_Tr, Efficiency_HPC_Tr, D_HPC);

    % Burner equations
    [Gas_Burner] =  Burner(Gas_HPC, T_max);

    % HPT
    [Gas_HPT, Mech_HPT] = HPT(Gas_Burner, Speed_HPT, Beta_HPT, Mass_HPT_Tr, PR_HPT_Tr, Efficiency_HPT_Tr, D_HPT);

    % IPT
    [Gas_IPT, Mech_IPT] = IPT(Gas_HPT, Speed_IPT, Beta_IPT, Mass_LPT_Tr, PR_LPT_Tr, Efficiency_LPT_Tr, D_IPT);

    % LPT
    [Gas_LPT, Mech_LPT] = LPT(Gas_IPT, Speed_LPT, Beta_LPT, Mass_LPT_Tr, PR_LPT_Tr, Efficiency_LPT_Tr, D_LPT);

    % Core Nozzle          
    [Gas_Core, Thrust_core] = Nozzle(Gas_LPT, T_amb, P_amb, Mach, D_N);

    % Bypass Nozzle 
    [Gas_Bypass, Thrust_bypass] = Bypass_Nozzle(Gas_Fan, T_amb, P_amb, Mach, D_BN);

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Part 3: Residual Vector
    
    % Jay already has the essence of this
    % Sum Mass Flows into a Component = 0 (Gas Domain Kirchhoff's Current Law)
    f(1) = Gas_Bypass(3)+Gas_Core(3)-Gas_Fan(3);
    f(2) = Gas_Core(3) - Gas_LPC(3);
    f(3) = Gas_Core(3) - Gas_HPC(3);
    f(4) = Gas_Core(3) - Gas_HPT(3);
    f(5) = Gas_Core(3) - Gas_IPT(3);
    f(6) = Gas_Core(3) - Gas_LPT(3);
    
    % Jay already has the essence of this
    % Sum Power on A Shaft = 0
    f(7) = Mech_HPT(2) + Mech_HPC(2);
    f(8) = Mech_IPT(2) + Mech_LPC(2);
    f(9) = Mech_LPT(2) + Mech_Fan(2);
    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Equating RPM-s (How to Take Into Account Gearbox)
    f(10) = Mech_Fan(1) - Mech_LPT(1);
    f(11) = Mech_LPC(1) - Mech_IPT(1);
    f(12) = Mech_HPC(1) - Mech_HPT(1);
    
    % This should be easy to implement
    f(13) = Thrust_core+Thrust_bypass-Req_Thrust;
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

end

%     iteration_structure = {'Beta_Fan';'Beta_LPC';'Beta_HPC';'Beta_HPT';...
%         'Beta_IPT';'Beta_LPT';'Speed_Fan';'Speed_LPC';'Speed_HPC';...
%         'Speed_HPT'; 'Speed_IPT';'Speed_LPT'};

