MATLAB: How to calculate the uncertainty of an

calculate uncertainty vectorfor loop in syms

Hello men and women,
the next problem has got me pinned down. Following an experiment I have to calculate the uncertainty of the values and of a coupple of equations, but I just can't get it right. I already calculated the mean values of the measurement data, and that went into a new vector. However, when I use the next code, I will get error after error, particularly because I can't use vectors in it. Basically, my question is:
Is there a way to use vectors for the variables in "syms" or do I have to write an all new function? Please help me and thanks in advance!
The example:
T = big matrix;
s = vector;
g = 9.81;
ug = 0.01;
udeltas = 0.005;
for i=1:10
t(i) = mean(T(i,:));
ut(i) = std(T(i,:))/sqrt(length(T(i,:)));
for k=1:10
deltas(k) = s(k) - 0.5*g*(t(k))^2;
syms Bm uBm g ug deltas udeltas t ut
for j=1:10
Bm(j) = -(12/(g^2))*(deltas(j)-deltas(1))/(t(j)^4 - t(1)^4);
uBg(j) = ug*diff(Bm,g);
uBdeltas(j) = udeltas(j)*diff(Bm,deltas);
uBt(j) = ut(j)*diff(Bm,t);
uBm(j) = sqrt(uBg(j)^2 + uBdeltas(j)^2 + uBt(j)^2 + uBdeltas(1)^2);
And, upon running I receive the following errors:
Error using mupadmex Error in MuPAD command: Index exceeds matrix dimensions.
Error in sym/subsref (line 1577) B = mupadmex('symobj::subsref',A.s,inds{:});

Best Answer

  • I suggest you avoid the symbolic loop entirely and instead do something like this:
    syms Bm uBm g ug deltas deltas1 udeltas ubdeltas1 t t1 ut
    g = 9.81;
    ug = 0.01;
    udeltas = 0.005;
    Bm = -(12/(g^2))*(deltas-deltas1)/(t^4 - t1^4);
    uBg = ug*diff(Bm,g);
    uBdeltas = udeltas*diff(Bm,deltas);
    uBt = ut*diff(Bm,t);
    uBm = sqrt(uBg^2 + uBdeltas^2 + uBt^2 + ubdeltas1^2);
    % Create anonymous functions (DO NOT put semicolons on the ends of the statements, because you want them to print out in the command window):
    fn_Bm = matlabFunction(Bm)
    fn_uBg = matlabFunction(uBg)
    fn_uBdeltas = matlabFunction(uBdeltas)
    fn_uBt = matlabFunction(uBt)
    fn_uBm = matlabFunction(uBm)
    I replaced t(1) with t1 and udeltas(1) with udeltas1 for convenience.
    Then you would set up your loop as before, calling the functions as necessary. For instance, I get:
    fn_Bm = @(deltas,deltas1,t,t1)-(deltas.*1.246933323357867e-1-deltas1.*1.246933323357867e-1)./(t.^4-t1.^4);
    so the loop would be:
    for k1 = 1:10 % PLEASE do not use ā€˜iā€™ or ā€˜jā€™ as variables or loop indices
    Bm(k1) = fn_Bm(deltas(k1),deltas(1),t(k1),t(1));
    So you only have to use the Symbolic Math Toolbox once to derive your functions.
    Also, rather than using the Symbolic Math Toolbox to derive them each time you run your script, you can copy them from the command window after the first time you run it, paste them to your function, re-format them slightly so each is on one line (as I did here), and then make them a permanent part of your script. That seems to me to be much faster and more convenient.