www.gusucode.com > stats 源码程序 matlab案例代码 > stats/ComputeStudentsTCdfExample.m

    %% Compute Student's _t_ cdf
%

% Copyright 2015 The MathWorks, Inc.


%%
mu = 1;     % Population mean
sigma = 2;  % Population standard deviation
n = 100;    % Sample size

rng default   % For reproducibility
x = normrnd(mu,sigma,n,1);  % Random sample from population

xbar = mean(x);  % Sample mean
s = std(x);      % Sample standard deviation
t = (xbar - mu)/(s/sqrt(n))

%%
p = 1-tcdf(t,n-1) % Probability of larger t-statistic

%%
% This probability is the same as the _p_ value returned by a _t_ test of
% the null hypothesis that the sample comes from a normal population with
% mean $\mu$
[h,ptest] = ttest(x,mu,0.05,'right')