What am I doing wrong here? I Keep getting "All table variables must have the same number of rows." error.

39 visualizzazioni (ultimi 30 giorni)
data = load('therm_con.dat.dat');
temperature = data(:, 1);
conductivity = data(:, 2);
% Define the model equations
equation1 = @(c, T) 1 ./ (c(1)./T + c(2)*T.^2);
equation2 = @(c, T) 1 ./ (c(1)./T + c(2)*T + c(3)*T.^2);
% Perform curve fitting using GLLS method and Equation 1
c1_eq1_glls = lsqcurvefit(equation1, [1, 1], temperature, conductivity);
fit_eq1_glls = equation1(c1_eq1_glls, temperature);
% Perform curve fitting using fminsearch method and Equation 1
c1_eq1_fmin = fminsearch(@(c) norm(equation1(c, temperature) - conductivity), [1, 1]);
fit_eq1_fmin = equation1(c1_eq1_fmin, temperature);
% Perform curve fitting using GLLS method and Equation 2
c_eq2_glls = lsqcurvefit(equation2, [1, 1, 1], temperature, conductivity);
fit_eq2_glls = equation2(c_eq2_glls, temperature);
% Perform curve fitting using fminsearch method and Equation 2
c_eq2_fmin = fminsearch(@(c) norm(equation2(c, temperature) - conductivity), [1, 1, 1]);
fit_eq2_fmin = equation2(c_eq2_fmin, temperature);
% Calculate curve fit quantities
S_R_eq1_glls = sum((fit_eq1_glls - conductivity).^2);
S_R_eq1_fmin = sum((fit_eq1_fmin - conductivity).^2);
S_t_eq1_glls = sqrt(S_R_eq1_glls / (length(temperature) - 2));
S_t_eq1_fmin = sqrt(S_R_eq1_fmin / (length(temperature) - 2));
R2_eq1_glls = 1 - S_R_eq1_glls / sum((conductivity - mean(conductivity)).^2);
R2_eq1_fmin = 1 - S_R_eq1_fmin / sum((conductivity - mean(conductivity)).^2);
S_R_eq2_glls = sum((fit_eq2_glls - conductivity).^2);
S_R_eq2_fmin = sum((fit_eq2_fmin - conductivity).^2);
S_t_eq2_glls = sqrt(S_R_eq2_glls / (length(temperature) - 3));
S_t_eq2_fmin = sqrt(S_R_eq2_fmin / (length(temperature) - 3));
R2_eq2_glls = 1 - S_R_eq2_glls / sum((conductivity - mean(conductivity)).^2);
R2_eq2_fmin = 1 - S_R_eq2_fmin / sum((conductivity - mean(conductivity)).^2);
% Generate the plot
figure;
plot(temperature, conductivity, 'bo', 'MarkerSize', 5);
hold on;
plot(temperature, fit_eq1_glls, 'r-', 'LineWidth', 1.5);
plot(temperature, fit_eq1_fmin, 'g--', 'LineWidth', 1.5);
plot(temperature, fit_eq2_glls, 'm-', 'LineWidth', 1.5);
plot(temperature, fit_eq2_fmin, 'c--', 'LineWidth', 1.5);
hold off;
title('Curve Fitting of Thermal Conductivity Data');
xlabel('Temperature (K)');
ylabel('Thermal Conductivity (W/m/K)');
legend('Experimental Data', 'Equation 1 (GLLS)', 'Equation 1 (fminsearch)', 'Equation 2 (GLLS)', 'Equation 2 (fminsearch)');
grid on;
% Generate the tables
methodNames = {'GLLS', 'fminsearch'};
equation1Coefficients = [c1_eq1_glls; c1_eq1_fmin];
equation2Coefficients = [c_eq2_glls; c_eq2_fmin];
standardErrors = [S_t_eq1_glls, S_t_eq1_fmin, S_t_eq2_glls, S_t_eq2_fmin];
rSquaredValues = [R2_eq1_glls, R2_eq1_fmin, R2_eq2_glls, R2_eq2_fmin];
table1 = table(methodNames', standardErrors', rSquaredValues', 'VariableNames', {'Method', 'Standard Error', 'R^2'});
table2 = table(methodNames', equation1Coefficients(:, 1), equation1Coefficients(:, 2), ...
equation2Coefficients(:, 1), equation2Coefficients(:, 2), equation2Coefficients(:, 3), ...
'VariableNames', {'Method', 'c1 (Equation 1)', 'c2 (Equation 1)', 'c1 (Equation 2)', 'c2 (Equation 2)', 'c3 (Equation 2)'});
% Display the tables
disp('Table 1:');
disp(table1);
disp('Table 2:');
disp(table2);
Error using table
All table variables must have the same number of rows.
Error in Project_2 (line 62)
table1 = table(methodNames', standardErrors', rSquaredValues', 'VariableNames', {'Method', 'Standard Error', 'R^2'});
Therm_con.dat
12.2900000000000 2535
13.7500000000000 2788
14.8200000000000 2993
16.1200000000000 3042
18.0400000000000 3100
18.6700000000000 3196
20.5200000000000 3247
22.6800000000000 3033
25.1500000000000 3114
27.7200000000000 2746
30.2400000000000 2329
33.2100000000000 2072
36.4800000000000 1724
39.8600000000000 1471
50.4000000000000 950

Risposta accettata

Walter Roberson
Walter Roberson il 25 Mag 2023
methodNames = {'GLLS', 'fminsearch'};
That is a row vector of length 2
standardErrors = [S_t_eq1_glls, S_t_eq1_fmin, S_t_eq2_glls, S_t_eq2_fmin];
Unless there are some empty variables in that list, we can predict that has at least 4 columns.
table1 = table(methodNames', standardErrors', rSquaredValues', 'VariableNames', {'Method', 'Standard Error', 'R^2'});
methodNames is 1 x 2, methodNames' is 2 x 1.
standardErrors is at least N x 4 (N might be 1), standardErrors' is at least 4 x N (N might be 1)
2 rows is not the same number of rows as 4 or more rows.

Più risposte (0)

Prodotti


Release

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by