grouped_time_derivatives.m 7.55 KB
Newer Older
Femi Kadri's avatar
Femi Kadri committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
function grouped_time_derivatives(Independent_Variable,Bin,Dependent_Variable,dependent_variable_name_string,...
    derivative_name_string,bin_name_string,Dependent_Variable_Units,Derivative_Units,...
    Bin_Units,current_directory_path,...
    current_results_directory_path,output_fid) 
%THIS FUNCTION WILL CREATE A STRUCTURE FROM GROUPED DATA AND THEN PLOT THE
%DERIVATIVES AS A FUNCTION OF BIN NUMBER

%turn off the latex character interpreter
%set(0,'DefaultTextInterpreter','none')

%make the structure
Variable_Structure = struct('Ind_Var',Independent_Variable,'bin', Bin,'Dep_Var', Dependent_Variable);
unique_reaction_bins = unique(Variable_Structure.bin); %obtain the unique reaction bins
maximum_unique_reaction_bin = find( unique_reaction_bins == max(unique_reaction_bins) );
minimum_unique_reaction_bin = find( unique_reaction_bins == min(unique_reaction_bins) );

total_number_of_unique_rxn_bins = size(unique_reaction_bins,1);  %figure out the total number of unique reaction bins

AVG_Dep_Var_Derivative_fid = fopen([current_results_directory_path 'MATLAB_LST_AVG_' derivative_name_string '_vs_' bin_name_string '.txt'],'w');
fprintf(AVG_Dep_Var_Derivative_fid,'Derivatives are calculated by averaging slopes between all points \n\n');
fprintf(AVG_Dep_Var_Derivative_fid,[bin_name_string ' [' Bin_Units '] Bin Mid Point, AVG ' derivative_name_string  ' [' Derivative_Units '] \n\n']);


% Handle storage
figure_handles = zeros(1, total_number_of_unique_rxn_bins);




%hold the plot to have all the bins on the same graph
hold on

% Prepare the string cell array
legend_string_array = cell(1, total_number_of_unique_rxn_bins);
title(derivative_name_string)

%pre-allocate memory to this variable to increase speed
AVG_Dep_Var_Derivative = zeros(total_number_of_unique_rxn_bins,1);

  for current_reaction_bin_number = 1:total_number_of_unique_rxn_bins 
      
    %GET THE VALUE OF THE CURRENT RXN BIN  
    current_reaction_bin_number_value = ...
        unique_reaction_bins(current_reaction_bin_number);
    
    %get the current bin locations in the structure
    current_rxn_bin_locations_in_struct = find(Variable_Structure.bin == ...
    unique_reaction_bins(current_reaction_bin_number));

    Independent_Variable_Current_Bin = ...
        Variable_Structure.Ind_Var(current_rxn_bin_locations_in_struct);
    Dependent_Variable_Current_Bin = ...
        Variable_Structure.Dep_Var(current_rxn_bin_locations_in_struct);
    
    
    cd(current_directory_path)
    
    %FIND THE MIDDLE OF THE ARRAY TO NEGLECT 1ST HALF OF THE STEPS
    [dimension_one_size] = size(Dependent_Variable_Current_Bin,1);
    array_dimension_one_middle_location = round(dimension_one_size / 2);
    
    %calculate approximate derivatives for current reaction bin by doind a
    %linear fit to the Dependent_Variable and taking the slope of that
    linear_fit_derivative(dependent_variable_name_string,...
    derivative_name_string,Dependent_Variable_Units,Derivative_Units,output_fid,...
    Independent_Variable_Current_Bin(array_dimension_one_middle_location:end),...
    Dependent_Variable_Current_Bin(array_dimension_one_middle_location:end),...
    current_reaction_bin_number_value,bin_name_string,Bin_Units,...
    current_results_directory_path)
    
    %PLOT TIME DERIVATIVES FOR CURRENT BIN
    %DERIVATIVE VALUES ARE PLOTTED AT THE MIDPOINTS WHICH ARE CALCULATED
    %HERE
    Ind_Var_Current_Bin_midpoints = (Independent_Variable_Current_Bin(1:end-1) + Independent_Variable_Current_Bin(2:end))/2;
    %calculate approximate derivatives for current reaction bin by
    %averaging slopes at every point
    Dep_Var_Derivatives = diff(Dependent_Variable_Current_Bin) ./ diff(Independent_Variable_Current_Bin);

    if( current_reaction_bin_number == maximum_unique_reaction_bin )  %backup maxreactive bin for later use
    
        Ind_Var_Current_Bin_midpoints_Bin_Max_Value = (Independent_Variable_Current_Bin(1:end-1) + Independent_Variable_Current_Bin(2:end)) / 2;
        Dep_Var_Derivatives_Bin_Max_Value = diff(Dependent_Variable_Current_Bin) ./ diff(Independent_Variable_Current_Bin);
        
    elseif( current_reaction_bin_number == minimum_unique_reaction_bin )  %backup maxreactive bin for later use 
        
        Ind_Var_Current_Bin_midpoints_Bin_Min_Value = (Independent_Variable_Current_Bin(1:end-1) + Independent_Variable_Current_Bin(2:end)) / 2;
        Dep_Var_Derivatives_Bin_Min_Value = diff(Dependent_Variable_Current_Bin) ./ diff(Independent_Variable_Current_Bin);
    
    end; 

    %PLOT THE current_reaction_bin_number DATA
    %Dep_Var_Derivatives(:) = current_reaction_bin_number %for debugging
    figure_handles(current_reaction_bin_number) = scatter(Ind_Var_Current_Bin_midpoints,Dep_Var_Derivatives);
    
    AVG_Dep_Var_Derivative(current_reaction_bin_number) = mean(Dep_Var_Derivatives);
    
    fprintf(AVG_Dep_Var_Derivative_fid,'%6f                                         %6f\n',...
        current_reaction_bin_number_value,...
       AVG_Dep_Var_Derivative(current_reaction_bin_number));
    
    %RECORD THE LEGEND ENTRY FOR THE current_reaction_bin_number DATA 
    legend_string_array{current_reaction_bin_number} = sprintf([bin_name_string ' %.2f'], unique_reaction_bins(current_reaction_bin_number) );
    
    
  end; %current_reaction_bin_number = 1:total_number_of_unique_rxn_bins
  
  fclose(AVG_Dep_Var_Derivative_fid);
  % Select the plots to include in the legend
  %included_plots = (1:total_number_of_unique_rxn_bins);
  % Create legend for the selected plots
  %set(legend_string_array,'Interpreter','none')
  %[L,Obj] = legend(figure_handles(included_plots), legend_string_array{included_plots});
  %this is to get rid of latex character interpetation
  %t = findobj(Obj,'Type','Text');
  %set(t,'Interpreter','none')
  
cd(current_results_directory_path)


xlabel('Time [sec]')
ylabel( [derivative_name_string ' [ ' Derivative_Units ' ] '],'Interpreter','none')
title(['Legend is ' bin_name_string ' [ ' Bin_Units ' ] '],'Interpreter','none')
print('-djpeg',[derivative_name_string '_' bin_name_string '_vs_Time'])
close %currrent figure
hold off

%plot just the non-reacting bin 
scatter(Ind_Var_Current_Bin_midpoints_Bin_Min_Value,Dep_Var_Derivatives_Bin_Min_Value);
xlabel('Time [sec]')
ylabel( [derivative_name_string ' [ ' Derivative_Units ' ] '],'Interpreter','none')
title(['Non Reactive ' bin_name_string  ' [ ' Bin_Units ' ] '],'Interpreter','none')
print('-djpeg',[derivative_name_string '_Non_Reactive_' bin_name_string '_vs_Time'])
close %currrent figure

%plot just the max-reacting bin
scatter(Ind_Var_Current_Bin_midpoints_Bin_Max_Value,Dep_Var_Derivatives_Bin_Max_Value);
xlabel('Time [sec]')
ylabel( [derivative_name_string ' [ ' Derivative_Units ' ] '],'Interpreter','none')
title(['Max Reactive ' bin_name_string  ' [ ' Bin_Units ' ] '],'Interpreter','none')
print('-djpeg',[derivative_name_string '_Max_Reactive_' bin_name_string '_vs_Time'])
close %currrent figure



%plot AVG_Dep_Var_Derivative vs unique_reaction_bins
scatter(unique_reaction_bins,AVG_Dep_Var_Derivative);
xlabel([bin_name_string  ' [ ' Bin_Units ' ] '])
ylabel(['AVG ' derivative_name_string ' [ ' Derivative_Units ' ] '],'Interpreter','none')
title(['AVG ' derivative_name_string  ' [ ' Derivative_Units ' ] ' ' vs ' bin_name_string  ' [ ' Bin_Units ' ] '],'Interpreter','none')
print('-djpeg',['AVG_' derivative_name_string '_vs_' bin_name_string])
close %currrent figure

%set the character interpreter back to default
%set(0,'defaulttextinterpreter','latex') 

end %function grouped_time_derivatives