%% Spring Calculations %% % % Authors: Garrett, Sabrina % Last Update: 4/12/18 % % Purpose: Read in pressure/displacement data for each plane % Calculate spring constants needed for each plane % Save spring constants in spreadsheet % %% Read in Spreadsheet Data %% display('--- Spring Calculations ---') display('Reading in data') max_press = 225% psi diameter = 3.25 % inches % spreadsheet info file = 'Data.xlsx'; % name of spreadsheet sheet = 4; % sheet number in spreadsheet first_row = 2; % first row with data incr = 4; % rows between each section of data % column in spreadsheet names_indx = 'A'; press_indx = 'B'; disp_indx = 'C'; % get names of planes names = {}; % matlab is dumb and doesnt allow arrays of strings. need cell array instead counter = first_row; max_counter = 0; current_name = 'placeholder'; s = size(current_name); while (s(1) > 0) index = strcat(names_indx, num2str(counter)); [~,current_name] = xlsread(file, sheet, index); % ~ ignores numeric field, current_name gets text field counter = counter + incr; max_counter = max_counter + 1; s = size(current_name); if (s(1) > 0) names{end+1} = char(current_name); end end % get pressures pressure = zeros(max_counter-1,3); counter = first_row; row = 1; for i = first_row:max_counter index = strcat(press_indx, num2str(counter), ':', press_indx, num2str(counter+2)); % the +2 here is hard coded data = xlsread(file, sheet, index); col = 1; for j = 1:length(data) pressure(row,col) = data(j); col = col + 1; end counter = counter + incr; row = row + 1; end % get displacements displacement = zeros(max_counter-1,3); counter = first_row; row = 1; for i = first_row:max_counter index = strcat(disp_indx, num2str(counter), ':', disp_indx, num2str(counter+2)); % the +2 here is hard coded data = xlsread(file, sheet, index); col = 1; for j = 1:length(data) displacement(row,col) = data(j); col = col + 1; end counter = counter + incr; row = row + 1; end %% Calculations %% display('Performing Calculations') % output spreadsheet variables file_out = 'SpringCalcs.xlsx'; % output file sheet = 1; xl_col = 'A'; xl_row = 1; delete SpringCalcs.xlsx; % delete spreadsheet % known values area = (pi/4)*diameter^2; %sq inches scale_factor = [1]; % [50, 80, 100, 200]; %[.005, .01, .05, .1, 1, 2]; % write constraints into spreadsheet xl_indx = strcat(xl_col, num2str(xl_row)); data = {'Max Pressure [psi]', max_press}; xlswrite(file_out,data,sheet,xl_indx); xl_row = xl_row + 1; xl_indx = strcat(xl_col, num2str(xl_row)); data = {'Piston diameter [in]', diameter}; xlswrite(file_out,data,sheet,xl_indx); xl_row = xl_row + 1; % loop through planes for i = 1:(max_counter-1) plane = names(i); % txt press = pressure(i,1:3); % psi disp = displacement(i,1:3); % in^3 norm_press = press(3)/max_press; % normalized max pressure sim_press = press/norm_press; % simulator pressures % write plane/scale info into output spreadsheet xl_indx = strcat(xl_col, num2str(xl_row)); data = {char(plane), 'lin_disp k1 [in]', 'lin_disp k2 [in]', 'k1a', 'k2a', 'k1b', 'k2b'}; % expand for more cases xlswrite(file_out,data,sheet,xl_indx); xl_row = xl_row + 1; % Spring constant calcs for each scale for j = 1:length(scale_factor) scale = scale_factor(j); force = sim_press*area; % force of piston lin_disp = (disp/area)/scale; % linear displacement of piston d_press = sim_press(3)-sim_press(2); % delta pressure d_disp = lin_disp(3)-lin_disp(2); % delta linear displacement % Case A: % two springs in series; spring 1 is compressed first k1a = sim_press(2)*area/lin_disp(2); k2a = d_press*area/d_disp; % Case B: % one spring inside another; spring 1 extends farther/is compressed first k1b = k1a; k2b = (d_press*area)/d_disp-k1b; % write constants into spreadsheet constants = [lin_disp(2), d_disp, k1a, k2a, k1b, k2b]; % expand for more cases scale_txt = strcat('Scale = ', num2str(scale)); data = {char(scale_txt)}; for k = 1:length(constants) %data{end+1} = round(constants(k),2); % round to 2 decimal points data{end+1} = constants(k); end xl_indx = strcat(xl_col, num2str(xl_row)); xlswrite(file_out,data,sheet,xl_indx); xl_row = xl_row + 1; end % end scale factor loop % write blank row xl_indx = strcat(xl_col, num2str(xl_row)); xlswrite(file_out,' ',sheet,xl_indx); xl_row = xl_row + 1; end display('--- Complete ---')