Matlab Matrix calculation coming wrong

1 view (last 30 days)
Hiril Patel
Hiril Patel on 26 Oct 2022
Commented: Hiril Patel on 27 Oct 2022
Hello,
I am working on a sensor data. I have three csv file from which I do voltage and strain calculation. After calculation of strain, when its comes to calculation of angle theta here is following condtion happening:
Condition 1: the one with correct calculations
If I take individual value(for example take value no 110 of from each variable i.e gauge1 , gauge2 and gauge3) and apply formula for performing the calculation of angle of strain I get the correct answer.
Conditon 2: the one with wrong calculated answer
However, If I apply this formula to entire matrix gauge1,2,3 I get differenent value for calculated angle. I know this one is wrong since I have calculated the values by hand and they don't match up with this condition.
I have attached csv file in order if one can help.
Thank you
Hiril
%%
clc;
clear;
% ADC and strain gauge settings
vref = 3300; % ADC reference volts
ADC_resolution = 2^24; % 2^23 24bit of resolution
A = 4; % if fsr = +/-2
pga = 128; % ADS1234 amplifier gain setting
E = 3300; % Bridge excitation in millivolts
K = 2.04; % Gauge factor
% Importing adc values
ch1 = readmatrix('C:\Users\Hiril Patel\PycharmProjects\writetofile\correct strain gauge -0.8\ch0.csv');
ch2 = readmatrix('C:\Users\Hiril Patel\PycharmProjects\writetofile\correct strain gauge -0.8\ch1.csv');
ch3 = readmatrix('C:\Users\Hiril Patel\PycharmProjects\writetofile\correct strain gauge -0.8\ch2.csv');
% Conversion to proportional millivoltage
FSR = vref/pga;
LSB = FSR/(ADC_resolution);
Channel1 = LSB*ch1;
Channel2 = LSB*ch2;
Channel3 = LSB*ch3;
Channel1mean = mean(Channel1(1:50));
Channel2mean = mean(Channel2(1:50));
Channel3mean = mean(Channel3(1:50));
Channel1 = Channel1 - Channel1mean;
Channel2 = Channel2 - Channel2mean;
Channel3 = Channel3 - Channel3mean;
% Strain calculation
gauge1 = (4/K)*(Channel1/E);
gauge2 = (4/K)*(Channel2/E);
gauge3 = (4/K)*(Channel3/E);
fig = figure;
subplot(3,1,1);
plot(gauge1,'.-');
title('Gauge1');
subplot(3,1,2);
plot(gauge2,'.-');
title('Gauge2');
subplot(3,1,3);
plot(gauge3,'.-');
title('Gauge3');
han=axes(fig,'visible','off');
han.Title.Visible='on';
han.XLabel.Visible='on';
han.YLabel.Visible='on';
ylabel(han,'Micro-strain');
xlabel(han,'Samples');
a = ((sqrt(2))/3)*(sqrt(((gauge1-gauge2).^2) + ((gauge2 - gauge3).^2) + ((gauge3-gauge1).^2)));
e1 = ((gauge1 + gauge2 + gauge3)/3) + a;
e2 = ((gauge1 + gauge2 + gauge3)/3) - a;
% Uptill here answers from calculation are correct.
%The problem starts after this point where entire matrix is considered for
%calculation
b = (sqrt(3))*(gauge3-gauge2);
c = ((2*gauge1) - gauge2 - gauge3);
T = b/c;
theta = (-0.5)*(atand(T));
figure;
if gauge1 > ((gauge2+gauge3)/2)
plot(theta);
elseif gauge1 < ((gauge2+gauge3)/2)
theta = theta - 90;
plot(theta)
elseif gauge1 == ((gauge2+gauge3)/2) & gauge2<gauge1
theta = - 45;
plot(theta);
elseif gauge1 == ((gauge2 + gauge3)/2) & gauge2>gauge1
theta = 45;
plot(theta);
else
plot(theta)
end

Answers (2)

Alex Hanes
Alex Hanes on 26 Oct 2022
In Line 65, you are using mrdivide (/), when it seems you are trying to element-wise division using rdivide (./). mrdivide solves the system of linear equations for , which is why your array for T = b/c has dimension (630 x 630).
T = b./c;
  4 Comments
Hiril Patel
Hiril Patel on 26 Oct 2022
Actually no, so here is the thing in detail, I am measuring strain gauge output and there are different types of strain induced on the specimen and in order to identify where is the angle of the strain (theta) there is one of the 4 condition which needs to be met. SO each set of data from all the gauges should pass from the those condtion in order to identify what is the angle of strain.
I hope i have explained it clearly, but in case this is the literature that i am following:
check on page 5 below the equation 5: that states the reason of the condtion being present(but they are for rectangular rossette) and on the page six the condition mentioned are the ones that I am using(because I am using delta rosette)
The way i added data in this vairables
gauge1 = [700;300;500;500]
gauge2 = [700;300;300;700]
gauge3 = [300;700;700;300]
So the first element of the gauge1,2,3 satisfy first if statment and this gives me with the original value of theta which was derived from
b = (sqrt(3))*(gauge3-gauge2);
c = ((2*gauge1) - gauge2 - gauge3);
T = b./c;
theta = (-0.5)*(atand(T));
The second elements of gauge 1,2,3 satisfy second if statement(this is where I get wrong value if you run all three arrays using the formula above and pass them from my if statement then the plot gives me (-30.-30,-45,45) values of theta however this is not correct. The correct values should be (-30, -60, -45, 45), so clearly the I have made logical error of applying the condtions.
I hope I was able to explain clearly
Torsten
Torsten on 26 Oct 2022
I hope I was able to explain clearly
Yes, but you didn't get my point (see below).

Sign in to comment.


Torsten
Torsten on 26 Oct 2022
Edited: Torsten on 26 Oct 2022
% Strain calculation
gauge1 = [700;300;500;500];
gauge2 = [700;300;300;700];
gauge3 = [300;700;700;300];
fig = figure;
subplot(3,1,1);
plot(gauge1,'.-');
title('Gauge1');
subplot(3,1,2);
plot(gauge2,'.-');
title('Gauge2');
subplot(3,1,3);
plot(gauge3,'.-');
title('Gauge3');
han=axes(fig,'visible','off');
han.Title.Visible='on';
han.XLabel.Visible='on';
han.YLabel.Visible='on';
ylabel(han,'Micro-strain');
xlabel(han,'Samples');
a = ((sqrt(2))/3)*(sqrt(((gauge1-gauge2).^2) + ((gauge2 - gauge3).^2) + ((gauge3-gauge1).^2)));
e1 = ((gauge1 + gauge2 + gauge3)/3) + a;
e2 = ((gauge1 + gauge2 + gauge3)/3) - a;
% Uptill here answers from calculation are correct.
%The problem starts after this point where entire matrix is considered for
%calculation
b = (sqrt(3))*(gauge3-gauge2);
c = ((2*gauge1) - gauge2 - gauge3);
T = b./c;
theta = (-0.5)*(atand(T))
theta = 4×1
30.0000 30.0000 -45.0000 45.0000
figure;
for i = 1:numel(gauge1)
if gauge1(i) > ((gauge2(i)+gauge3(i))/2)
disp(theta(i));
elseif gauge1(i) < ((gauge2(i)+gauge3(i))/2)
theta(i) = theta(i) - 90;
disp(theta(i))
elseif gauge1(i) == ((gauge2(i)+gauge3(i))/2) & gauge2(i)<gauge1(i)
theta(i) = - 45;
disp(theta(i));
elseif gauge1(i) == ((gauge2(i) + gauge3(i))/2) & gauge2(i)>gauge1(i)
theta(i) = 45;
disp(theta(i));
else
disp(theta(i))
end
end
30.0000
-60
-45
45
  3 Comments
Hiril Patel
Hiril Patel on 26 Oct 2022
Indexing, didn't thought about that. thank you the solution.
Hiril Patel
Hiril Patel on 27 Oct 2022
Hello, sorry to be bother again but I would like to know if I can plot the value of the angle theta only concerning with acutal strain not the noise so currently I am using solution provided by Steven Lord but the problem arises when I zero down the offset of the signal. Here is the image. I though of plotting only section of angle value once strain is acted up it but it would be nice if I could remove noise somehow. Since the eqution considers everything as strain value and plot angles for it as well

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!