Rodbourn's Blog

MATLAB Uncertainty Analysis

Performing an uncertainty analysis on a measurement can be tedious, taking the root sum square (RSS) of all of partials with their uncertainty. MATLAB's symbolic toolbox can speed things up, particularly when Excel cells are named the same as the symbols used. I wrote a quick MATLAB function to take the partials and perform the RSS.

The first input is the symbolic expression to perform the uncertainty analysis upon. The second two parameters are the symbolic variables in the expression and the symbolic variables for the corresponding uncertainties.

function [f_u_total f_u] = uncertAnalysis(f, vars, vars_u)

f_u = [];

% take the partials and square them

for i=1:length(vars)

f_u = [f_u; vars(i) (diff(f, vars(i))*vars_u(i))^2];

end

f_u_total = sqrt(sum(f_u(:,2)));

end

This is best demonstrated through an example. Below I use the function to perform an uncertainty analysis on an expression for the coefficient of friction of a pipe. The first set of symbolic declarations are the variables of the expression while the second set are the corresponding uncertainty variables. These symbols are placed into two arrays to make them accessible by the function, and are then passed with the expression, f. The output of the function has itemized sensitivities and the total uncertainty as a function of all of the uncertainties.

syms head_loss g pi_ Diameter Length VolumetricFlowRate;

syms head_loss_u g_u pi_u Diameter_u Length_u VolumetricFlowRate_;

vars = [head_loss g pi_ Diameter Length VolumetricFlowRate];

vars_u = [head_loss_u g_u pi_u Diameter_u Length_u VolumetricFlowRate_];