% 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.
% 

clear all ; clc ; close all

load('AutoLoad.mat');
load('Design_3SP_Turbofan.mat')
global Req_Thrust
global T_amb
global P_amb
global Mach

global guess

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Part 1: Iteration Variable Vector Consisting of Beta and Speed
    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'};

    get_min_max_values
    
    num = size(iteration_structure,1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


    for count = 1:num
        current_variable = iteration_structure{count,1};
        if strcmp(current_variable(1:4), 'Beta')> 0.5   
            guess(count) = 0.5;
        elseif strcmp(current_variable(1:5), 'Speed')> 0.5
            guess(count) = 1.0; 
        end
    end

    % Burner exit temperature
    guess(num+1) = 1600;
    var_min_max(num+1,1) = guess(num+1) - 200;
    var_min_max(num+1,2) = guess(num+1) + 200;
 
 % Create the iteration Vector
 iteration_vector = [];
 
 for counter =1:num
     
           iteration_vector = strcat(iteration_vector,(iteration_structure(counter,:)),'= iteration_var(',num2str(counter),');');
 end
 
 
 iteration_vector=strcat(iteration_vector,'T_max = iteration_var(',num2str(num+1),');');
  
iteration_vector = char(iteration_vector);
 
P_amb_v = [103000 50000 22729 20000];
T_amb_v = [288 260 216.5 216.5];
Mach_v = [0 0.5 0.8 0.82];
Thrust_v = [160000 80000 39000 37000];
n_points = size(Thrust_v);
n_points = n_points(2);

n_points = 1;

for count = 1:n_points
    
    T_amb = T_amb_v(count);            % Ambient conditions
    P_amb = P_amb_v(count);
    Mach = Mach_v(count);
    Req_Thrust = Thrust_v(count); 
    [sol,fval,exitflag,output,jacobian_ss] = fsolve(@(iteration_var) equation(iteration_var, iteration_vector, T_amb, P_amb, Mach, Req_Thrust),...
        guess, optimset('Algorithm','trust-region-reflective', 'Display', 'iter', 'MaxIter', 200));
    start = 0;

end 












