Standard deviation error bars for each interpolated value of temperature
5 views (last 30 days)
Show older comments
I am trying to produce standard deviation error bars for the interpolated data ranging from 10 to 35 degrees with a a 1 degree interval, being plotted on the averaged interpolated data line. However, I can't seem to produce seperate standard deviations for each of the interpolated temperatures.
Below is the code I have used for one of the 4 oils.
Oilviscositytest10cst = readmatrix('Oil viscosity test 10cst.xlsx','Range','A6:O65');
xq = linspace(10,35,25);
% 10 cSt oil
% run 1
figure(1)
% run 1 ramp up
temp_1_rup_10 = Oilviscositytest10cst(2:27,3);
viscosity_1_rup_10 = Oilviscositytest10cst(2:27,5);
vq_1_up_10 = interp1(temp_1_rup_10,viscosity_1_rup_10,xq);
plot(xq,vq_1_up_10,'r')
hold on
plot(temp_1_rup_10,viscosity_1_rup_10,'ko')
title('10 cSt oil')
% run 1 ramp down
temp_1_rdown_10 = Oilviscositytest10cst(35:60,3);
viscosity_1_rdown_10 = Oilviscositytest10cst(35:60,5);
vq_1_down_10 = interp1(temp_1_rdown_10,viscosity_1_rdown_10,xq);
plot(xq,vq_1_down_10,'b')
hold on
plot(temp_1_rdown_10,viscosity_1_rdown_10,'ko')
% run 2
% run 2 ramp up
temp_2_rup_10 = Oilviscositytest10cst(2:27,11);
viscosity_2_rup_10 = Oilviscositytest10cst(2:27,13);
vq_2_up_10 = interp1(temp_2_rup_10,viscosity_2_rup_10,xq);
plot(xq,vq_2_up_10,'c')
hold on
plot(temp_2_rup_10,viscosity_2_rup_10,'ko')
% run 2 ramp down
temp_2_rdown_10 = Oilviscositytest10cst(35:60,11);
viscosity_2_rdown_10 = Oilviscositytest10cst(35:60,13);
vq_2_down_10 = interp1(temp_2_rdown_10,viscosity_2_rdown_10,xq);
plot(xq,vq_2_down_10,'g')
hold on
plot(temp_2_rdown_10,viscosity_2_rdown_10,'ko')
% run 3
% run 3 ramp up
temp_3_rup_10 = Oilviscositytest10cst(2:27,3);
viscosity_3_rup_10 = Oilviscositytest10cst(2:27,5);
vq_3_up_10 = interp1(temp_3_rup_10,viscosity_3_rup_10,xq);
plot(xq,vq_3_up_10,'r')
hold on
plot(temp_3_rup_10,viscosity_3_rup_10,'ko')
% run 3 ramp down
temp_3_rdown_10 = Oilviscositytest10cst(35:60,3);
viscosity_3_rdown_10 = Oilviscositytest10cst(35:60,5);
vq_3_down_10 = interp1(temp_3_rdown_10,viscosity_3_rdown_10,xq);
plot(xq,vq_3_down_10,'b')
hold on
plot(temp_3_rdown_10,viscosity_3_rdown_10,'ko')
% average viscosity
six = 6*(ones(1,1));
addition_vq_10 = vq_1_up_10 + vq_1_down_10 + vq_2_up_10 + vq_2_down_10 + vq_3_up_10 + vq_3_down_10;
average_vq_10 = addition_vq_10/six;
plot(xq,average_vq_10,'k')
plot(xq, average_vq_10, 'ko')
err_10 = (std(average_vq_10,[],2)/sqrt(size(average_vq_10,2)))
e_10 = errorbar(xq,average_vq_10,err_10);
figure(2)
plot(xq,average_vq_10,'m')
hold on
plot(xq, average_vq_10, 'ko')
0 Comments
Answers (2)
Torsten
on 16 Dec 2023
You mean the graphics if you add the line
errorbar(xq,average_vq_10,err_10,"blue")
at the end of your code from above ?
9 Comments
Torsten
on 17 Dec 2023
Edited: Torsten
on 17 Dec 2023
It seems we computed the same.
But still I'm not convinced whether it's statistically correct to interpolate the six experiments to equal values of temperature and then compute errorbars for the interpolated values.
Oilviscositytest10cst = readmatrix('Oil viscosity test 10cst.xlsx','Range','A6:W65');
xq = linspace(10,35,25);
% 10 cSt oil
% run 1
figure(1)
% run 1 ramp up
temp_1_rup_10 = Oilviscositytest10cst(2:27,3);
viscosity_1_rup_10 = Oilviscositytest10cst(2:27,5);
vq_1_up_10 = interp1(temp_1_rup_10,viscosity_1_rup_10,xq);
plot(xq,vq_1_up_10,'r')
hold on
plot(temp_1_rup_10,viscosity_1_rup_10,'ko')
title('10 cSt oil')
% run 1 ramp down
temp_1_rdown_10 = Oilviscositytest10cst(35:60,3);
viscosity_1_rdown_10 = Oilviscositytest10cst(35:60,5);
vq_1_down_10 = interp1(temp_1_rdown_10,viscosity_1_rdown_10,xq);
plot(xq,vq_1_down_10,'b')
hold on
plot(temp_1_rdown_10,viscosity_1_rdown_10,'ko')
% run 2
% run 2 ramp up
temp_2_rup_10 = Oilviscositytest10cst(2:27,11);
viscosity_2_rup_10 = Oilviscositytest10cst(2:27,13);
vq_2_up_10 = interp1(temp_2_rup_10,viscosity_2_rup_10,xq);
plot(xq,vq_2_up_10,'c')
hold on
plot(temp_2_rup_10,viscosity_2_rup_10,'ko')
% run 2 ramp down
temp_2_rdown_10 = Oilviscositytest10cst(35:60,11);
viscosity_2_rdown_10 = Oilviscositytest10cst(35:60,13);
vq_2_down_10 = interp1(temp_2_rdown_10,viscosity_2_rdown_10,xq);
plot(xq,vq_2_down_10,'g')
hold on
plot(temp_2_rdown_10,viscosity_2_rdown_10,'ko')
% run 3
% run 3 ramp up
temp_3_rup_10 = Oilviscositytest10cst(2:27,19);
viscosity_3_rup_10 = Oilviscositytest10cst(2:27,21);
vq_3_up_10 = interp1(temp_3_rup_10,viscosity_3_rup_10,xq);
plot(xq,vq_3_up_10,'r')
hold on
plot(temp_3_rup_10,viscosity_3_rup_10,'ko')
% run 3 ramp down
temp_3_rdown_10 = Oilviscositytest10cst(35:60,19);
viscosity_3_rdown_10 = Oilviscositytest10cst(35:60,21);
vq_3_down_10 = interp1(temp_3_rdown_10,viscosity_3_rdown_10,xq);
plot(xq,vq_3_down_10,'b')
hold on
plot(temp_3_rdown_10,viscosity_3_rdown_10,'ko')
% average viscosity
addition_vq_10 = vq_1_up_10 + vq_1_down_10 + vq_2_up_10 + vq_2_down_10 + vq_3_up_10 + vq_3_down_10;
average_vq_10 = addition_vq_10/6;
err_10 = sqrt(((vq_1_down_10-average_vq_10).^2 +...
(vq_1_up_10-average_vq_10).^2+...
(vq_2_down_10-average_vq_10).^2 +...
(vq_2_up_10-average_vq_10).^2+...
(vq_3_down_10-average_vq_10).^2+...
(vq_3_up_10-average_vq_10).^2)/5);
figure(2)
plot(xq,average_vq_10,'m')
hold on
plot(xq, average_vq_10, 'ko')
errorbar(xq,average_vq_10,err_10);
Star Strider
on 16 Dec 2023
Edited: Star Strider
on 17 Dec 2023
It is difficult to follow your code, so I created my own based on my best guess as to what you want.
Try this —
% readcell('Oil viscosity test 10cst.xlsx')
% opts = spreadsheetImportOptions('VariableNamesRange','A2:W2', 'VariableUnitsRange','B6:W6', 'VariableNamingRule','preserve')
T1 = readtable('Oil viscosity test 10cst.xlsx', 'VariableNamingRule','preserve')
Mv = all(ismissing(T1(:,1:5)),2);
T1(Mv,:) = [];
ColIdx = [1 3 5] + [0; 8; 16]
xq = linspace(10,35,25); % Temperature Vector For Interpolation
for k = 1:size(ColIdx,1)
% k
idx = cumsum([true; diff(T1{:,ColIdx(k,1)})<0]);
TempVisc = accumarray(idx, (1:numel(idx)).', [], @(x){[T1{x,ColIdx(k,2)} T1{x,ColIdx(k,3)}]}); % Accumulate Results
% TempVisc
Temp{k} = [TempVisc{1}(:,1) flipud(TempVisc{2}(:,1))];
Visc{k} = [TempVisc{1}(:,2) flipud(TempVisc{2}(:,2))]; % unique Values For Interpolation
Viscic{k,1} = interp1(Temp{k}(:,1), Visc{k}(:,1), xq(:)); % Interpolate
Viscic{k,2} = interp1(Temp{k}(:,2), Visc{k}(:,2), xq(:));
end
Temp
Visc
Viscic
Visci = cell2mat(reshape(Viscic, 1, []))
Tempm = cell2mat(Temp)
Viscm = cell2mat(Visc)
figure
plot(Tempm, Viscm)
grid
xlabel('Temperature (°C)')
ylabel('Viscosity (mPa\cdots)')
title('Viscosity vs. Temperature')
axis('padded')
ViscMean = mean(Visci, 2); % Mean Of Interpolated Viscosity Vectors
ViscSE = std(Visci, [], 2) / sqrt(size(Visci,2)); % Standared Error Of Interpolated Viscosity Vectors
figure
errorbar(xq, ViscMean, ViscSE)
grid
xlabel('Temperature (°C)')
ylabel('Viscosity (mPa\cdots)')
title('Mean Viscosity vs. Temperature With Errors')
axis('padded')
return
Oilviscositytest10cst = table2array(T1(:, [1:6 8:14 16:end-1]))
T1(25:28,:)
xq = linspace(10,35,25);
% 10 cSt oil
% run 1
figure(1)
% run 1 ramp up
temp_1_rup_10 = Oilviscositytest10cst(2:27,3);
viscosity_1_rup_10 = Oilviscositytest10cst(2:27,5);
vq_1_up_10 = interp1(temp_1_rup_10,viscosity_1_rup_10,xq);
plot(xq,vq_1_up_10,'r')
hold on
plot(temp_1_rup_10,viscosity_1_rup_10,'ko')
title('10 cSt oil')
% run 1 ramp down
temp_1_rdown_10 = Oilviscositytest10cst(35:60,3);
viscosity_1_rdown_10 = Oilviscositytest10cst(35:60,5);
vq_1_down_10 = interp1(temp_1_rdown_10,viscosity_1_rdown_10,xq);
plot(xq,vq_1_down_10,'b')
hold on
plot(temp_1_rdown_10,viscosity_1_rdown_10,'ko')
% run 2
% run 2 ramp up
temp_2_rup_10 = Oilviscositytest10cst(2:27,11);
viscosity_2_rup_10 = Oilviscositytest10cst(2:27,13);
vq_2_up_10 = interp1(temp_2_rup_10,viscosity_2_rup_10,xq);
plot(xq,vq_2_up_10,'c')
hold on
plot(temp_2_rup_10,viscosity_2_rup_10,'ko')
% run 2 ramp down
temp_2_rdown_10 = Oilviscositytest10cst(35:60,11);
viscosity_2_rdown_10 = Oilviscositytest10cst(35:60,13);
vq_2_down_10 = interp1(temp_2_rdown_10,viscosity_2_rdown_10,xq);
plot(xq,vq_2_down_10,'g')
hold on
plot(temp_2_rdown_10,viscosity_2_rdown_10,'ko')
% run 3
% run 3 ramp up
temp_3_rup_10 = Oilviscositytest10cst(2:27,3);
viscosity_3_rup_10 = Oilviscositytest10cst(2:27,5);
vq_3_up_10 = interp1(temp_3_rup_10,viscosity_3_rup_10,xq);
plot(xq,vq_3_up_10,'r')
hold on
plot(temp_3_rup_10,viscosity_3_rup_10,'ko')
% run 3 ramp down
temp_3_rdown_10 = Oilviscositytest10cst(35:60,3);
viscosity_3_rdown_10 = Oilviscositytest10cst(35:60,5);
vq_3_down_10 = interp1(temp_3_rdown_10,viscosity_3_rdown_10,xq);
plot(xq,vq_3_down_10,'b')
hold on
plot(temp_3_rdown_10,viscosity_3_rdown_10,'ko')
% average viscosity
six = 6*(ones(1,1));
addition_vq_10 = vq_1_up_10 + vq_1_down_10 + vq_2_up_10 + vq_2_down_10 + vq_3_up_10 + vq_3_down_10;
average_vq_10 = addition_vq_10/six;
plot(xq,average_vq_10,'k')
plot(xq, average_vq_10, 'ko')
err_10 = (std(average_vq_10,[],2)/sqrt(size(average_vq_10,2)))
e_10 = errorbar(xq,average_vq_10,err_10);
figure(2)
plot(xq,average_vq_10,'m')
hold on
plot(xq, average_vq_10, 'ko')
This is not easy data to works with, however I believe this does essentially what you requested.
EDIT — (17 Dec 2023 at 13:25)
I found an error in my earlier code. This version works correctly.
.
2 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!