%% HOW DO I DO THAT IN MATLAB SERIES?
% In this series, I am answering questions that students have asked
% me about MATLAB.  Most of the questions relate to a mathematical 
% procedure.

%% TOPIC
% How do I use the concepts of regression,integration and nonlinear
% equations to solve an engineering problem
%% SUMMARY
% Language : Matlab 2010a; 
% Authors : Autar Kaw and Sri Harsha Garapati; 
% Mfile available at
% http://numericalmethods.eng.usf.edu/blog/shrinkfit_blog.m
% Last Revised : January 30, 2012; 
% Abstract: This program shows you how to apply the concepts of
% regression, integration and nonlinear equations in solving an
% practical engineering problem
clc
clear all
%% INTRODUCTION

disp('ABSTRACT')
disp('   This program shows you how to apply mathematical concepts of')
disp('   regression, integration and solving nonlinear equations')
disp('   to solve practical engineering problems')

disp(' ')
disp('AUTHOR')
disp('   Autar Kaw and Sri Harsha Garapati of http://autarkaw.wordpress.com')
disp(' ')
disp('MFILE SOURCE')
disp('   http://numericalmethods.eng.usf.edu/blog/shrinkfit_blog.m')
disp(' ')
disp('LAST REVISED')
disp('   January 30, 2012')
disp(' ')

%% PROBLEM HANDOUT
% Link to Problem Description:
% http://numericalmethods.eng.usf.edu/EML3035/homework/shrink_fit_applied_problem.pdf
% PDF

%% INPUTS
  
% Outer diameter of the trunnion in inches
Dia=12.363;
% Coefficient of thermal expansion
alpha_room=6.47E-6;
% Temperature of the cooling fluid (Dry-Ice alcohol mixture)
Tfluid=-108;
% Room temperature
Troom=80;
% Temperature Array
temp=[80 0 -80 -100 -160  -180  -260  -280  -300 -340];
% coefficient of thermal expansion array
alpha=[6.47 6.0 5.43 5.28 4.72 4.52 3.58 3.33 3.07  2.45]*1E-6;
% Contraction needed
con_needed=0.015;

%% DISPLAYING INPUTS
disp('  ')
disp('INPUTS')

% printing the outer diameter of the trunnion
fprintf('  Outer diameter of the trunnion = %g\n',Dia)
% printing the coefficient of thermal expansion of steel at room temperature
fprintf('The coefficent of thermal expansion at room temperature =%g\n',alpha_room)
% printing the coefficient of cooling fluid temperature
fprintf('Temperature of cooling medium =%g\n',Tfluid)
% printing the room temperature
fprintf('Temperature of the ambient air =%g\n',Troom)
% printing the alpha as a function of temperature(Table)
format short g
disp(' ')
disp('       Alpha          Temp')
disp('       -----          ----')
data=[alpha; temp]';
disp(data)
% printing the target contraaction needed
fprintf('Contraction needed =%g"\n',con_needed)
%% THE CODE

% 1) Contraction of the trunnion outer diamter
deltat=Tfluid-Troom;
deltaD=Dia*alpha_room*deltat;

% 2) Regressing the the data alpha vs temperature as a second order
% polynomial (Eqn 3 from the the problem statement handout
coeff=polyfit(temp,alpha,2);
syms T
alphapoly=coeff(1)*T^2+coeff(2)*T+coeff(3);
alphapoly=vpa(alphapoly,6);

% 3) Find the contraction (Hint: Use Eqn 2 in the problem handout
deltaD3=Dia*int(alphapoly,T,Troom,Tfluid);
deltaD3=double(deltaD3);

% 4) If the contraction needed is -0.015, Find the minimum temperature that
% would be needed
syms Tf
rhs1=Dia*int(alphapoly,T,Troom,Tf);
lhs1=-con_needed;
eqn1=[char(rhs1) '=' num2str(lhs1)];
soln=solve(eqn1,Tf);
soln=double(soln);

%% DISPLAYING OUTPUTS 
disp('  ')
disp('OUTPUTS')
% Printing the change in outer diamter of trunnion
fprintf ('\nChange in diameter prob1 i.e. taking alpha to be constant=%g in',deltaD) 
% Printing the regression curve alpha vs temp
fprintf('\nPolynomial approx of alpha is %s',char(alphapoly))
% printing the actual change in dia (Taking alpha as a function of temp)
fprintf ('\nChange in diameter prob3=%g in',deltaD3)
% printing the temperature required to acheive the target contraction
fprintf ('\nFluid temp needed=%g F\n',soln(2))



