function [tvalue, df, p] = t_test1(gv, gm)

%% One-sample t-test

% Inputs: gv = given vector
%         gm = assumed mean
% Outputs: t = t-test
%          df = degrees of freedom 

% number of elements in vector
r = length(gv);
% sum of elements
v = sum(gv);
% sum of squared elements
d = sum(gv.^2);
% mean of current vector
m = mean(gv); 

% calculation of t-test and degrees of freedom
% Case 1: one vector with assumed mean
av = (d - v^2 / r) / (r - 1);
tvalue = (m - gm) * sqrt(r/av);
df = r - 1;

%% Use right tail value algorithm from same authors to calculate p-value


% Asks the user for input
t = tvalue;
d = df;

x = 1;
y = 1;
t = t^2; 

% Computes using inverse for small T-values
if t < 1
    s = d;
    r = y;
    z = 1/t;
else
    s = y;
    r = d;
    z = t;
end

j = 2/(9*s);
k = 2/(9*r); 

% Computes using approximation formulas
l = abs((1 - k)*z^(1/3) - 1 + j)/sqrt(k*z^(2/3) + j); 

if r < 4
    l = l*(1 + 0.08*l^4/r^3);
end 

a1 = 0.196854;
a2 = 0.115194;
a3 = 0.000344;
a4 = 0.019527;
x = 0.25/(1 + l*(a1 + l*(a2 + l*(a3 + l*a4))))^4;
x = floor(x*10000 + 0.5)/10000; 

% Adjusts if inverse was calculated
if t < 1
    x = 1 - x;
end 

p = x;

% % Displays result
% tvalue = num2str(tvalue);
% str = ['t-value: ' tvalue];
% disp(str);
% 
% p = num2str(x);
% str = ['p-value: ' p];
% disp(str)
% 
% df = num2str(df);
% str=['df: ' df];
% disp(str);









