Arrays have incompatible sizes for this operation

2 views (last 30 days)
clc;
clear;
%This function is will determine the rate of heat loss
%you will experience through one of our select pipes and
%piping insulation
pipe=questdlg('Choose your pipe material','Pipe Material','copper','stainless steel','aluminum', 'aluminum');
insulation=questdlg('Choose your type of insulation','insulation','glass wool','rigid foam','no insulation','no insulation');
%Below are the inputs for the pipe dimensions
inner=input('Enter pipe inner diameter in mm:');
thickness=input('Enter pipe thickness in mm:');
ithickness=input('Enter insulation thickness in mm:');
L=input('Enter pipe length in m:');
%The below section handles temperatures. All temps calculated within
%fuction are calculated using celcius.
t2=input('Enter approximate room temperature(Temperature outside of pipe and insulation):');
fprintf('This next input will be related to steam temperature. Enter either a single value or \nrange of values.\n')
fprintf('If entering a range of values, enter as a vector with incrementally equal values \nranging from 200 to 500 degrees celcius or 392 to 932 degrees Farenheit.\n')
pause(3)
t1=input('Input temperature of steam:');
unit=questdlg('Are the temperatures you entered in celcius or farenheit?','Temp Unit','F','C','C');
if unit=='F'
T1=(t1-32)*5/9;
T2=(t2-32)*5/9;
else unit=='C'
T1=t1;
T2=t2;
end
if T1 > 500 | T1 < 200
disp('Please enter a temperature between 200C and 500C')
pause(3)
tempfunction()
end
%The section below handles the coefficients of heat transfer for the
%equation to solve.
if pipe=='copper'
if T1 >= 200 & T1 <= 350
k = 386;
else T1 >= 351 & T1 <= 500
k = 379;
end
elseif pipe=='stainless steel'
k=50 %W/m K
else pipe=='aluminum'
if T1>=200 & T1<=350
k = 159;
elseif T1>=351 & T1<=450
k = 172;
else T1>450
k=185;
end
end
if insulation=='glass wool'
k2=.05;
elseif insulation=='rigid foam'
k2=.02;
else insulation=='no insulation'
k2=0;
end
%heat transfer coefficient inside the pipe = 60 W/m^2*K(From
h1=60;
%heat transfer coefficient outside the pipe = 18 W/m^2*K
h2=18;
%To get radii
r1=inner/2;
r2=(inner/2)+thickness;
r3=(inner/2)+thickness+ithickness;
%Now we will calculate the surface areas exposed to convection
A1=2*pi*r1*L;
A2=2*pi*r2*L;
%Now we will calculate the individual thermakl resistance network
Rconv1=1/(h1*A1);
R1=log(r2/r1)/(2*pi*k*L);
R2=log(r3/r2)/(2*pi*k2*L);
Rconv2=1/(h2*A2);
fprintf('Here are the values for your thermal resistance network')
pause(2);
fprintf('Rconv1= %6.3f C/W\n',Rconv1);
pause(2);
fprintf('R1= %6.3f C/W\n', R1);
pause(2);
fprintf('R2= %6.3f C/W\n',R2);
pause(2);
fprintf('Rconv2= %6.3f C/W\n',Rconv2);
%Now we will add up the total thermal resistance
R=Rconv1+R1+R2+Rconv2;
pause(5);
fprintf('Your total thermal resistance is %6.3f C/W.\n',R);
%Calculate the rate of heatloss
Qdot=(T1-T2)/R; %W
pause(3);
fprintf('Your rate of heat loss through our pipe, as spec''d, will\nbe %6.3f W per meter of pipe.\n',Qdot);
pause(3);
Qtotal=Qdot*L;
fprintf('Total heat loss through the pipe will be %6.3f W.\n', Qtotal);
Tdrop=Qdot*R1;
fprintf('The drop in temperature along the pipe will be %6.3f degrees C.\n',Tdrop);
figure(1)
plot(Qdot,T1,'bs:');
title('Qdot verses T1');
xlabel('Qdot');
ylabel('T1');
On my code, when we select anything other than copper for pipe, it gives me an error message,
Arrays have incompatible sizes for this operation.
Error in testfunction (line 55)
if pipe=='copper'
Why is it doing this?
P.S. This was a function but I turned it into a script to try and troubleshoot.

Answers (2)

Torsten
Torsten on 5 Dec 2022
You should look up characters and if-statements in the documentation.
clc;
clear;
%This function is will determine the rate of heat loss
%you will experience through one of our select pipes and
%piping insulation
pipe="stainless steel";%questdlg('Choose your pipe material','Pipe Material','copper','stainless steel','aluminum', 'aluminum');
insulation="glass wool";%questdlg('Choose your type of insulation','insulation','glass wool','rigid foam','no insulation','no insulation');
%Below are the inputs for the pipe dimensions
inner=100;%input('Enter pipe inner diameter in mm:');
thickness=5;%input('Enter pipe thickness in mm:');
ithickness=10;%input('Enter insulation thickness in mm:');
L=1;%input('Enter pipe length in m:');
%The below section handles temperatures. All temps calculated within
%fuction are calculated using celcius.
t2=25;%input('Enter approximate room temperature(Temperature outside of pipe and insulation):');
fprintf('This next input will be related to steam temperature. Enter either a single value or \nrange of values.\n')
This next input will be related to steam temperature. Enter either a single value or range of values.
fprintf('If entering a range of values, enter as a vector with incrementally equal values \nranging from 200 to 500 degrees celcius or 392 to 932 degrees Farenheit.\n')
If entering a range of values, enter as a vector with incrementally equal values ranging from 200 to 500 degrees celcius or 392 to 932 degrees Farenheit.
%pause(3)
t1=450;%input('Input temperature of steam:');
unit="C";%questdlg('Are the temperatures you entered in celcius or farenheit?','Temp Unit','F','C','C');
if unit=="F"
T1=(t1-32)*5/9;
T2=(t2-32)*5/9;
elseif unit=="C"
T1=t1;
T2=t2;
end
if T1 > 500 | T1 < 200
disp('Please enter a temperature between 200C and 500C')
%pause(3)
%tempfunction()
end
%The section below handles the coefficients of heat transfer for the
%equation to solve.
if pipe=="copper"
if T1 >= 200 & T1 <= 350
k = 386;
elseif T1 >= 351 & T1 <= 500
k = 379;
end
elseif pipe=="stainless steel"
k=50 ; %W/m K
elseif pipe=="aluminum"
if T1>=200 & T1<=350
k = 159;
elseif T1>=351 & T1<=450
k = 172;
elseif T1>450
k=185;
end
end
if insulation=="glass wool"
k2=.05;
elseif insulation=="rigid foam"
k2=.02;
elseif insulation=="no insulation"
k2=0;
end
%heat transfer coefficient inside the pipe = 60 W/m^2*K(From
h1=60;
%heat transfer coefficient outside the pipe = 18 W/m^2*K
h2=18;
%To get radii
r1=inner/2;
r2=(inner/2)+thickness;
r3=(inner/2)+thickness+ithickness;
%Now we will calculate the surface areas exposed to convection
A1=2*pi*r1*L;
A2=2*pi*r2*L;
%Now we will calculate the individual thermakl resistance network
Rconv1=1/(h1*A1);
R1=log(r2/r1)/(2*pi*k*L);
R2=log(r3/r2)/(2*pi*k2*L);
Rconv2=1/(h2*A2);
fprintf('Here are the values for your thermal resistance network')
Here are the values for your thermal resistance network
%pause(2);
fprintf('Rconv1= %6.3f C/W\n',Rconv1);
Rconv1= 0.000 C/W
%pause(2);
fprintf('R1= %6.3f C/W\n', R1);
R1= 0.000 C/W
%pause(2);
fprintf('R2= %6.3f C/W\n',R2);
R2= 0.532 C/W
%pause(2);
fprintf('Rconv2= %6.3f C/W\n',Rconv2);
Rconv2= 0.000 C/W
%Now we will add up the total thermal resistance
R=Rconv1+R1+R2+Rconv2;
%pause(5);
fprintf('Your total thermal resistance is %6.3f C/W.\n',R);
Your total thermal resistance is 0.532 C/W.
%Calculate the rate of heatloss
Qdot=(T1-T2)/R; %W
%pause(3);
fprintf('Your rate of heat loss through our pipe, as spec''d, will\nbe %6.3f W per meter of pipe.\n',Qdot);
Your rate of heat loss through our pipe, as spec'd, will be 798.472 W per meter of pipe.
%pause(3);
Qtotal=Qdot*L;
fprintf('Total heat loss through the pipe will be %6.3f W.\n', Qtotal);
Total heat loss through the pipe will be 798.472 W.
Tdrop=Qdot*R1;
fprintf('The drop in temperature along the pipe will be %6.3f degrees C.\n',Tdrop);
The drop in temperature along the pipe will be 0.242 degrees C.
figure(1)
plot(Qdot,T1,'bs:');
title('Qdot verses T1');
xlabel('Qdot');
ylabel('T1');

Voss
Voss on 5 Dec 2022
Edited: Voss on 5 Dec 2022
Don't use == for comparing character vectors; use strcmp (or strcmpi), e.g.:
if strcmp(pipe,'copper')
elseif strcmp(pipe,'stainless steel')
% etc.
end
if strcmp(insulation,'glass wool')
% etc.
end
Or use switch/case rather than if strcmp ... elseif strcmp ... elseif strcmp ...
switch pipe
case 'copper'
case 'stainless steel'
% etc.
end
switch insulation
case 'glass wool'
% etc.
end
  4 Comments
Voss
Voss on 6 Dec 2022
You're welcome! Let me know if you have any questions. Otherwise, please "Accept" this Answer. Thanks!

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!