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

조회 수: 5 (최근 30일)
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
David
David 2022년 6월 27일
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 2022년 6월 27일
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;

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2022년 6월 27일
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
David
David 2022년 6월 27일
So I changed it to k = [2,20,200] and I'm still getting the same answer. :(
Thank you for the help!
Walter Roberson
Walter Roberson 2022년 6월 27일
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
Voss 2022년 6월 27일
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
David
David 2022년 6월 27일
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 2022년 6월 27일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Title에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by