% User input:

%Ball screw and cylinder
pressure = 150; %pressure in psi
cyl_D = 2.5; %cylinder diameter in inches
Vol = 1; %working volume in liters
root_D = 12.38; %ball screw root diameter in mm

%ball screw material properties
%Using 4150 steel
E = 2.97e7; %modulus in psi
yield = 67000; %yield strength in psi
K = 1.0; %Length factor determined by ball screw end support conditions (see below)

%
%
% We are Rotation fixed and translation fixed on both ends so K = 0.65;  Seems too liberal so going with K=1.0;
% Switch to imperial units

Vol = Vol * 61.0237; %in^3
root_D  = root_D/25.4; %in 

%
% First calculate the expected max force in compression on the ball screw:

F = pressure * pi() * (cyl_D/2)^2  %compression force in lbs

%
% Calculate length of cylinder needed to achieve Vol

L = (Vol)/(pi() * (cyl_D/2)^2)  %in

% Calculate area of screw face

A = pi() * (root_D/2)^2  %in^2

% Calculate second moment of area for a cylinder using:
% I=\frac{\pi d^4 }{64}\;\;\;\;\;\;\left({\textrm{in}}^4 \right)
% where d = root_d

I = (pi()*root_D^4)/64  %in^4

%
% Radius of Gyration
% R=\sqrt{\frac{I}{A\;}}\;\;\;\;\;\left(\textrm{inches}\right)\;

R = (I/A)^.5  %in

% Calculate the critical slenderness ratio (Scr) using:
% \textrm{Scr}=\sqrt{\frac{2\pi^2 E}{\textrm{Yield}}}

Scr = sqrt( (2*pi()^2 * E)/ yield)

% Calculate the slenderness ratio (S) using:
% S=\frac{K\;L}{R}

S = K*L/R

% If S > Scr then use Eulers long column buckling equation:
% P=\frac{\pi^2 \;E\;I}{{\left(K\;L\right)}^2 }

if (S>Scr)
    P = (pi()^2 * E * I)/(K*L)^2;   %critical buckling load in lbs
    fprintf('Critical Buckling load = %d lbs\n',P);
else
    fprintf('Use formula for short columns\n');
end

%
%
%
%