Why are my variables saving only Nan entries?
1 次查看(过去 30 天)
显示 更早的评论
Cal = struct;
Cal.data = readtable('Lab_4_Data.txt');
Cal.data_uE1 = Cal.data.Var3;
Cal.data_uE2 = Cal.data.Var4;
Cal.data_uE3 = Cal.data.Var5;
Cal.data_E1 = Cal.data_uE1 * 1000000;
Cal.data_E2 = Cal.data_uE2 * 1000000;
Cal.data_E3 = Cal.data_uE3 * 1000000;
masses = [0 50 100 200 300 500]; % grams
xN = (masses ./ 1000) * 9.81; % in newtons
% Calculate shear strain (𝛾xy)
Cal.data_gamma_xy = 2 * Cal.data_E2 - (Cal.data_E1 + Cal.data_E3);
% Calculate principal strains (Ɛ1, Ɛ2, Ɛ3)
Cal.data_epsilon_1 = Cal.data_E1;
Cal.data_epsilon_2 = 0.5 * Cal.data_gamma_xy;
Cal.data_epsilon_3 = Cal.data_E3;
% Calculate Poisson's ratio (v)
Cal.data_nu = abs(Cal.data_epsilon_2) ./ Cal.data_epsilon_1;
% Calculate angle of rotation (𝜃𝜃p)
Cal.data_theta_p = atand(Cal.data_gamma_xy ./ (Cal.data_epsilon_1 - Cal.data_epsilon_3));
% Calculate principal stresses (Txx, Tyy)
E = 69000; % Young's Modulus for aluminum in MPa
Cal.data_Txx = ((E) / (1 - Cal.data_nu.^2)) .* (Cal.data_epsilon_1 + Cal.data_nu .* Cal.data_epsilon_2);
Cal.data_Tyy = ((E) / (1 - Cal.data_nu.^2)) .* (Cal.data_epsilon_2 + Cal.data_nu .* Cal.data_epsilon_1);
% Calculate longitudinal stress from the beam flexure equation
P = xN; % applied load in Newtons
L = 0.241; % length of the beam in meters (replace with your actual value)
b = 0.025; % width of the beam in meters (replace with your actual value)
h = 0.00379; % thickness of the beam in meters (replace with your actual value)
c = h / 2;
% distance between gage's mounting line and dent at the end of the beam in meters
L_adjusted = 0.051;
I = (b * h^3) / 12;
Cal.data_sigma_L = (6 * P * L_adjusted) / (b * h^2);
% Calculate load cell calibration factor
Calibration_Factor = max(abs(Cal.data_epsilon_1)) / max(P);
% Plot 1: Principal Strains vs. Applied Load
figure;
plot(xN, Cal.data_epsilon_1(1:6), '-o', 'DisplayName', 'Ɛ1');
hold on;
plot(xN, Cal.data_epsilon_2(1:6), '-o', 'DisplayName', 'Ɛ2');
plot(xN, Cal.data_epsilon_3(1:6), '-o', 'DisplayName', 'Ɛ3');
xlabel('Applied Load (N)');
ylabel('Principal Strains (\mu\epsilon)');
title('Principal Strains vs. Applied Load');
legend('Location', 'Best');
grid on;
% Plot 2: Principal Stresses vs. Applied Load
figure;
plot(xN, Cal.data_Txx(1:6), '-o', 'DisplayName', 'Txx');
hold on;
plot(xN, Cal.data_Tyy(1:6), '-o', 'DisplayName', 'Tyy');
xlabel('Applied Load (N)');
ylabel('Principal Stresses (MPa)');
title('Principal Stresses vs. Applied Load');
legend('Location', 'Best');
grid on;
% Plot 3: Comparison of Measured and Theoretical Longitudinal Stress
figure;
plot(xN, Cal.data_sigma_L(1:6), '-o', 'DisplayName', 'Measured Longitudinal Stress');
hold on;
plot(xN, Cal.data_Txx(1:6), '-o', 'DisplayName', 'Theoretical Longitudinal Stress');
xlabel('Applied Load (N)');
ylabel('Stress (MPa)');
title('Comparison of Measured and Theoretical Longitudinal Stress');
legend('Location', 'Best');
grid on;
% Display Load Cell Calibration Factor
disp('Load Cell Calibration Factor (N/Ɛ):');
disp(Calibration_Factor);
The .txt file is attached.
This code runs, however the variables data_nu, data_theta_p, data_Txx, and data_Tyy within the Cal struct all have Nan values. All other created variables contain the correct data pulled from the .txt file. Why is this happening?
2 个评论
Stephen23
2023-12-9
编辑:Stephen23
2023-12-9
"Why is this happening?"
Because the first eighty-five rows of channel data are zeros. What do you expect to get when you divide zero by zero?
Date Time 200245-Ch 1 ue 200245-Ch 2 ue 200245-Ch 3 ue
11/11/21 04:09:57:5492 0 0 0
11/11/21 04:09:57:6792 0 0 0
11/11/21 04:09:57:7992 0 0 0
11/11/21 04:09:57:9293 0 0 0
11/11/21 04:09:58:0593 0 0 0
11/11/21 04:09:58:1894 0 0 0
11/11/21 04:09:58:3194 0 0 0
11/11/21 04:09:58:4494 0 0 0
11/11/21 04:09:58:5795 0 0 0
11/11/21 04:09:58:7095 0 0 0
11/11/21 04:09:58:8395 0 0 0
11/11/21 04:09:58:9594 0 0 0
11/11/21 04:09:59:0896 0 0 0
11/11/21 04:09:59:2197 0 0 0
11/11/21 04:09:59:3596 0 0 0
11/11/21 04:09:59:4897 0 0 0
11/11/21 04:09:59:6197 0 0 0
11/11/21 04:09:59:7498 0 0 0
11/11/21 04:09:59:8799 0 0 0
... etc
回答(1 个)
Stephen23
2023-12-9
编辑:Stephen23
2023-12-9
Cal.data_Txx = ((E) ./ (1 - Cal.data_nu.^2)) .* (Cal.data_epsilon_1 + Cal.data_nu .* Cal.data_epsilon_2);
Cal.data_Tyy = ((E) ./ (1 - Cal.data_nu.^2)) .* (Cal.data_epsilon_2 + Cal.data_nu .* Cal.data_epsilon_1);
% ^^ you used the wrong operator here
The first 85 NaN are expected, as I explained in my comment.
7 个评论
Sam Chak
2023-12-10
Hi @hyu34gyd5
Since the code is provided, most of the time, we can only advise on what went wrong in the code. If you are very new to MATLAB and coding, you may have difficulty understanding some technical aspects that we explain.
Therefore, I suggest that you provide a very clear picture (graphically) of what you intend to do with the data. Also, provide a sample outcome or plot that you expected.
Furthermore, some of us are not experts in the stress and strain field. Thus, we cannot verify if the formulas in the code are correct or not. You are advised to attach some images of the formulas so that we can check them.
Stephen23
2023-12-10
编辑:Stephen23
2023-12-10
You could use K-means clustering to detect the groups:
after which it is very simple to take the mean of each group. First import the data from file:
fnm = 'Lab_4_Data.txt';
opt = detectImportOptions(fnm, 'Delimiter','\t', 'VariableNamingRule','preserve');
tbl = readtable(fnm,opt)
Then identify each group using K-means algorithm and take the mean of each channel in each group:
masses = [0,50,100,200,300,500]; % grams
mat = tbl{:,digitsPattern+wildcardPattern};
idx = kmeans(mat,numel(masses));
[~,~,idy] = unique(idx,'stable');
tmp = splitapply(@mean,mat,idy)
Cal = struct;
Cal.data_uE1 = tmp(:,1);
Cal.data_uE2 = tmp(:,2);
Cal.data_uE3 = tmp(:,3);
After that comes the rest of your code:
Cal.data_E1 = Cal.data_uE1 * 1000000;
Cal.data_E2 = Cal.data_uE2 * 1000000;
Cal.data_E3 = Cal.data_uE3 * 1000000;
xN = (masses ./ 1000) * 9.81; % in newtons
% Calculate shear strain (𝛾xy)
Cal.data_gamma_xy = 2 * Cal.data_E2 - (Cal.data_E1 + Cal.data_E3);
% Calculate principal strains (Ɛ1, Ɛ2, Ɛ3)
Cal.data_epsilon_1 = Cal.data_E1;
Cal.data_epsilon_2 = 0.5 * Cal.data_gamma_xy;
Cal.data_epsilon_3 = Cal.data_E3;
% Calculate Poisson's ratio (v)
Cal.data_nu = abs(Cal.data_epsilon_2) ./ Cal.data_epsilon_1;
% Calculate angle of rotation (𝜃𝜃p)
Cal.data_theta_p = atand(Cal.data_gamma_xy ./ (Cal.data_epsilon_1 - Cal.data_epsilon_3));
% Calculate principal stresses (Txx, Tyy)
E = 69000; % Young's Modulus for aluminum in MPa
Cal.data_Txx = ((E) ./ (1 - Cal.data_nu.^2)) .* (Cal.data_epsilon_1 + Cal.data_nu .* Cal.data_epsilon_2);
Cal.data_Tyy = ((E) ./ (1 - Cal.data_nu.^2)) .* (Cal.data_epsilon_2 + Cal.data_nu .* Cal.data_epsilon_1);
% Calculate longitudinal stress from the beam flexure equation
P = xN; % applied load in Newtons
L = 0.241; % length of the beam in meters (replace with your actual value)
b = 0.025; % width of the beam in meters (replace with your actual value)
h = 0.00379; % thickness of the beam in meters (replace with your actual value)
c = h / 2;
% distance between gage's mounting line and dent at the end of the beam in meters
L_adjusted = 0.051;
I = (b * h^3) / 12;
Cal.data_sigma_L = (6 * P * L_adjusted) / (b * h^2);
% Calculate load cell calibration factor
Calibration_Factor = max(abs(Cal.data_epsilon_1)) / max(P);
% Plot 1: Principal Strains vs. Applied Load
figure;
plot(xN,Cal.data_epsilon_1, '-o', 'DisplayName', 'Ɛ1');
hold on;
plot(xN,Cal.data_epsilon_2, '-o', 'DisplayName', 'Ɛ2');
plot(xN,Cal.data_epsilon_3, '-o', 'DisplayName', 'Ɛ3');
xlabel('Applied Load (N)');
ylabel('Principal Strains (\mu\epsilon)');
title('Principal Strains vs. Applied Load');
legend('Location', 'Best');
grid on;
% Plot 2: Principal Stresses vs. Applied Load
figure;
plot(xN,Cal.data_Txx, '-o', 'DisplayName', 'Txx');
hold on;
plot(xN,Cal.data_Tyy, '-o', 'DisplayName', 'Tyy');
xlabel('Applied Load (N)');
ylabel('Principal Stresses (MPa)');
title('Principal Stresses vs. Applied Load');
legend('Location', 'Best');
grid on;
% Plot 3: Comparison of Measured and Theoretical Longitudinal Stress
figure;
plot(xN,Cal.data_sigma_L, '-o', 'DisplayName', 'Measured Longitudinal Stress');
hold on;
plot(xN,Cal.data_Txx, '-o', 'DisplayName', 'Theoretical Longitudinal Stress');
xlabel('Applied Load (N)');
ylabel('Stress (MPa)');
title('Comparison of Measured and Theoretical Longitudinal Stress');
legend('Location', 'Best');
grid on;
% Display Load Cell Calibration Factor
disp('Load Cell Calibration Factor (N/Ɛ):');
disp(Calibration_Factor);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Stress and Strain 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!