Error - I am new to Matlab and I just need help to graph this data I am getting a strange error.
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
clear all;
close all;
clc;
x = 0; % [m]
t = [0:300]; % [s]
h = 100; % [W/m^2*K]
% k = 2; % [W/m*K]
alpha = 5.6*10^-6; % [m^2/s]
% Temperatures
T_i = 325; % [Degrees Celcius]
T_infinity = 15; % [Degrees Celcius]
for k = [2;20;200]
T_xt = ((erfc((x)/(2*sqrt(alpha*t)))) - (exp(((h*x/k))+(h^2*alpha*t/k^2)))*erfc((x/(2*sqrt(alpha*t)))+((h*sqrt(alpha*t))/k))) * (T_infinity - T_i) + T_i;
%fprintf('Temperature in Degrees Celcius: %0.3f\n\n', T_xt);
plot(t,T_xt, '-','DisplayName', ['k' num2str(k)]);
hold on;
end
hold off;
fprintf('Temperature in Degrees Celcius: %0.3f\n\n', T_xt);
Error is in line 17 of code for the T_xt formula it says:
Error using /
Matrix dimensions must agree.
3 commentaires
Pooja Kumari
le 27 Juin 2022
This code will run for three values only.
clear all;
close all;
clc;
x = 0; % [m]
t = [0:300]; % [s]
h = 100; % [W/m^2*K]
% k = 2; % [W/m*K]
alpha = 5.6*10^-6; % [m^2/s]
% Temperatures
T_i = 325; % [Degrees Celcius]
T_infinity = 15; % [Degrees Celcius]
for k = [2,20,200]
T_xt = ((erfc((x)./(2*sqrt(alpha*t)))) - (exp(((h*x/k))+(h^2*alpha*t/k^2))).*erfc((x./(2*sqrt(alpha*t)))+((h*sqrt(alpha*t))/k))) * (T_infinity - T_i) + T_i;
% T_xt = ((erfc((x)/(2*sqrt(alpha*t)))) - (exp(((h*x/k))+(h^2*alpha*t/k^2)))*erfc((x/(2*sqrt(alpha*t)))+((h*sqrt(alpha*t))/k))) * (T_infinity - T_i) + T_i;
%fprintf('Temperature in Degrees Celcius: %0.3f\n\n', T_xt);
plot(t,T_xt, '-','DisplayName', ['k' num2str(k)]);
hold on;
end
hold off;
Réponses (2)
Walter Roberson
le 27 Juin 2022
for k = [2;20;200]
An obscure fact is that "for" iterates along columns of the expression. Your right hand side is a column vector so all of it is copied to k. And then you have /k but k is a vector...
You should change your right hand side of the for to have a row vector.
2 commentaires
Walter Roberson
le 27 Juin 2022
t = [0:300];
That is a vector
T_xt = ((erfc((x)/(2*sqrt(alpha*t)))) - (exp(((h*x/k))+(h^2*alpha*t/k^2)))*erfc((x/(2*sqrt(alpha*t)))+((h*sqrt(alpha*t))/k))) * (T_infinity - T_i) + T_i;
Notice the /(2*sqrt(alpha*t)) . But t is a vector, so you have / vector
When you have / and a vector on the right hand side, the vector must be a column vector and the left side must be something that has the same number of rows. The result will be a vector with the same number of columns as the left side.
It is a common mistake to think that / is the division operator. It is not. A/B is a fitting operator similar to A*pinv(B) where * is algebraic matrix multiplication (inner product). The division operator is ./ not /
Voss
le 27 Juin 2022
t is a vector, so you need to use element-wise operations with it, e.g., .* ./ .^
Also, the way you defined your for loop, k is a vector in each iteration:
iteration = 0;
for k = [2;20;200] % for loop with column vector
iteration = iteration+1
disp(k); % k is a column vector of all values, loop iterates once
end
iteration = 0;
for k = [2 20 200] % for loop with row vector
iteration = iteration+1
disp(k); % k takes one element at a time, loop iterates 3 times
end
iteration = 0;
for k = 2:20:200 % this row vector may be what was intended ( [] not necessary )
iteration = iteration+1
disp(k);
end
clear all;
close all;
clc;
x = 0; % [m]
t = [0:300]; % [s]
h = 100; % [W/m^2*K]
% k = 2; % [W/m*K]
alpha = 5.6*10^-6; % [m^2/s]
% Temperatures
T_i = 325; % [Degrees Celcius]
T_infinity = 15; % [Degrees Celcius]
% for k = [2;20;200]
for k = 2:20:200
% T_xt = ((erfc((x)/(2*sqrt(alpha*t)))) - (exp(((h*x/k))+(h^2*alpha*t/k^2)))*erfc((x/(2*sqrt(alpha*t)))+((h*sqrt(alpha*t))/k))) * (T_infinity - T_i) + T_i;
T_xt = ((erfc((x)./(2*sqrt(alpha*t)))) - (exp(((h*x/k))+(h^2*alpha*t/k^2))).*erfc((x./(2*sqrt(alpha*t)))+((h*sqrt(alpha*t))/k))) * (T_infinity - T_i) + T_i;
%fprintf('Temperature in Degrees Celcius: %0.3f\n\n', T_xt);
plot(t,T_xt, '-','DisplayName', ['k' num2str(k)]);
hold on;
end
hold off;
% fprintf('Temperature in Degrees Celcius: %0.3f\n\n', T_xt);
Voir également
Catégories
En savoir plus sur Title dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!