Error - I am new to Matlab and I just need help to graph this data I am getting a strange error.

4 views (last 30 days)
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 Comments
David
David on 27 Jun 2022
Certainly. My objective here is to graph time (t) and temperature (T_xt) at serveral different values of k.
The formula:
I hope this helps.
Pooja Kumari
Pooja Kumari on 27 Jun 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;

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 27 Jun 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 Comments
Walter Roberson
Walter Roberson on 27 Jun 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 /

Sign in to comment.


Voss
Voss on 27 Jun 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 = 1
2 20 200
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 = 1
2
iteration = 2
20
iteration = 3
200
iteration = 0;
for k = 2:20:200 % this row vector may be what was intended ( [] not necessary )
iteration = iteration+1
disp(k);
end
iteration = 1
2
iteration = 2
22
iteration = 3
42
iteration = 4
62
iteration = 5
82
iteration = 6
102
iteration = 7
122
iteration = 8
142
iteration = 9
162
iteration = 10
182
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);
  2 Comments
David
David on 27 Jun 2022
Thank you Voss, this looks very close. I'm only wanting the output only those three k values 2, 20, and 200 however.
It should look something like this...
David
David on 27 Jun 2022
Ahh, I think I have it. Your first two comments seem to have solve the problem. Thank you for the tutelage @Voss and @Walter Roberson. :)
This was very helpful.
Best,
D

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!