Calibrating Sensors: Hi does anyone know how to create a loop that will run through all of this code for multiple column vectors? I am having trouble creating a script to carry this out while taking out outliers, then running those vectors through
2 views (last 30 days)
Show older comments
%Tau method
for i=1:18
B=variable(ampboil5,amphand14,amphand5, ampice14, ampice5, amproom14, amproom5, ampwarm14, ampwarm5,
roseboil5, rosehand14, rosehand5, roseice14, roseice5, roseroom14, roseroom5, rosewarm14, rosewarm5); %file, this file, this_file, data?
N=length(B);
nu=N-1;
Mx=mean(B);
Sx=std(B);
P=0.95;
tnup = sqrt(nu/betaincinv(1-P,nu/2,1/2)-nu);
xL=Mx-tnup(length(filename))*Sx;
xU=Mx+tnup(length(filename))*Sx;
%<xL and >xU outliers
%precision uncertainty
al=(V*Q')/(V*V');
e=al*V-Q;
N=length(Q);
Sxy=sqrt(e*e'/(N-2));
nu=N-1;
P=0.95;
tnup=sqrt(nu/betaincinv(1-p,nu/2,1/2)-nu);
Dp=tnup*Sxy/sqrt(N);
%zero error
Sa0=Sxy*sqrt(V*V'/(N*(V*V')-sum(V)^2))
Dz=tnup*Sa0/sqrt(N);
%Sensitivity error
Sal=Sxy*sqrt(N/(N*(V/V')-sum(V)^2));
ro=max(Q);
rx=ro/al;
Ds=tnup*Sal*rx/sqrt(N);
%accuracy error
Da=sqrt((Dz^2)+(Ds^2))
%Total Calibration error
Dc=sqrt((Da^2)+(Dp^2))
0 Comments
Answers (1)
Zuber Khan
on 24 Sep 2024
Edited: Zuber Khan
on 24 Sep 2024
Hi,
In order to create a loop that processes multiple column vectors, you can encapsulate your existing code within a function and then iterate over each vector you want to process.
Firstly, you need to define the column vectors outside the code block.
% Define column vectors and store in a variable.
vectors = {ampboil5, amphand14, amphand5, ampice14, ampice5, ...
amproom14, amproom5, ampwarm14, ampwarm5, ...
roseboil5, rosehand14, rosehand5, roseice14, ...
roseice5, roseroom14, roseroom5, rosewarm14, rosewarm5};
Then, you can pass these vectors to a main function, for instance, calibrate_sensors as follows:
% Pass these vectors to calibrate_sensors() function to get the desired
% output
calibrate_sensors(vectors);
Lastly, you need to define the main and helper functions where you can substitute your code implementation.
% Define main and helper functions
function calibrate_sensors(vectors)
% Loop through each vector
for i = 1:length(vectors)
B = vectors{i};
B = remove_outliers(B);
calculate_uncertainties(B);
end
end
function B = remove_outliers(B)
N = length(B);
nu = N - 1;
Mx = mean(B);
Sx = std(B);
P = 0.95;
tnup = sqrt(nu / betaincinv(1 - P, nu / 2, 1 / 2) - nu);
xL = Mx - tnup * Sx;
xU = Mx + tnup * Sx;
% Remove outliers since <xL and >xU outliers
B = B(B >= xL & B <= xU);
end
function calculate_uncertainties(B)
N = length(B);
V = 1:N; % Example V, replace with actual values if different
Q = B; % Assuming Q is the same as B, replace if needed
al = (V * Q') / (V * V');
e = al * V - Q;
Sxy = sqrt(e * e' / (N - 2));
nu = N - 1;
P = 0.95;
tnup = sqrt(nu / betaincinv(1 - P, nu / 2, 1 / 2) - nu);
Dp = tnup * Sxy / sqrt(N);
% Zero error
Sa0 = Sxy * sqrt(V * V' / (N * (V * V') - sum(V)^2));
Dz = tnup * Sa0 / sqrt(N);
% Sensitivity error
Sal = Sxy * sqrt(N / (N * (V / V') - sum(V)^2));
ro = max(Q);
rx = ro / al;
Ds = tnup * Sal * rx / sqrt(N);
% Accuracy error
Da = sqrt((Dz^2) + (Ds^2));
% Total Calibration error
Dc = sqrt((Da^2) + (Dp^2));
end
I hope it will address your query.
Regards,
Zuber
0 Comments
See Also
Categories
Find more on MATLAB Parallel Server in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!