load('AutoLoad.mat');
global G_fuel
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Part 1: Iteration Variable Vector Consisting of Beta and Speed
    iteration_structure = $_it
    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;                             % Automatic Initial Guessing 
        elseif strcmp(current_variable(1:5), 'speed')> 0.5
            guess(count) = 1.0;                             % Automatic Initial Guessing 
        end
    end

    % Burner exit temperature
    guess(num+1) = 1600;
    
 
 % 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 103000 75000 50000 22729 21000 20000];        % Input Pressure Vector     (Pascal)
T_amb_v = [288  288 270 250 216.5 214 210];                     % Input Temperature Vector (Kelvin)
Mach_v = [0 0.3 0.5 0.5 0.8 0.81 0.82];                         % Input Mach number Vector 
Thrust_v = [160000 110000 100000 80000 39000 39000 39000];      % Input Thrust Vector (Newtons)
Distance_v = [1e3 10*1e3 20*1e3 1000*1e3 1000*1e3 1000*1e3];    % Input Distance Vector (meter) 
n_points = size(Thrust_v);
n_points = n_points(2);

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);
    Distance = Distance_v(count);
    sol = fsolve(@(iteration_var) equation(iteration_var, iteration_vector, T_amb, P_amb, Mach, Req_Thrust), guess, optimset('Algorithm','trust-region-reflective', 'TolFun',1e-3,'TolX',1e-3,'Display', 'iter','MaxIter', 200));
    guess = sol;
    total_fuel_consumption(count) = G_fuel*(Distance)/(Mach*sqrt(1.4*287*T_amb));
end   

total_fuel_consumption = sum(total_fuel_consumption);
