Have a function take a string and an number as an input?

3 views (last 30 days)
I am trying to create a function that can take a name of an alloy of metal and a temperature and calculate the thermal conductivity, I'm having issues getting the function to accept the script. This is the code I have so far.
function [alloy,T] = ThCond(alloy,T)
switch alloy
case 'Al2'
if (298 <= T) && (T <= 840)
k = 149.7 + 0.0809.*T - (1.*10^(-4)).*(T.^2);
fprintf('\nThe thermal conductivity(k_s) is %6.2f\n', k')
elseif (298 <= T) && (T <= 773)
k = 76.64 + 0.2633.*T - (2.*10^(-4)).*(T^2);
fprintf('\nThe thermal conductivity(k_s) is %6.2f\n', k')
else
disp('Invalid Temperature Input!');
end
case 'Al3'
if (293 <= T) && (T <= 890)
k = 124.7 + 0.56.*T + (1*10^(-5)).*(T^2);
fprintf('\nThe thermal conductivity(k_s) is %6.2f\n', k')
else
disp('Invalid Temperature Input!');
end
case 'Cu1'
if (100 <= T) && (T <= 1200)
k = 453.9 - 0.1054.*T;
fprintf('\nThe thermal conductivity(k_s) is %6.2f\n', k')
else
disp('Invalid Temperature Input!');
end
case 'Cu2'
if (460 <= T) && (T <= 1188)
k = 140.62 + (112.14.*10^(-4)).*T;
fprintf('\nThe thermal conductivity(k_s) is %6.2f\n', k')
else
disp('Invalid Temperature Input!');
end
case 'Cu3'
if T <= 1443
k = 16.041 + (438.9.*10^(-4)).*T;
fprintf('\nThe thermal conductivity(k_s) is %6.2f\n', k')
else
disp('Invalid Temperature Input!');
end
case 'St1'
if (400 <= T) && (T <= 1000)
k = 76.63 - 0.0459.*T;
fprintf('\nThe thermal conductivity(k_s) is %6.2f\n', k')
else
disp('Invalid Temperature Input!');
end
case 'St2'
if (298 < T) && (T < 1573)
k = 6.31 + ((27.2.*10^(-3)).*T) - (7.*10^(-6)).*(T.^2);
fprintf('\nThe thermal conductivity(k_s) is %6.2f\n', k')
else
disp('Invalid Temperature Input!');
end
case 'St3'
if T <= 1727
k = 20 + (61.5.*10^(-4)).*T;
fprintf('\nThe thermal conductivity(k_s) is %6.2f\n', k')
else
disp('Invalid Temperature Input!');
end
end
end

Accepted Answer

Birdman
Birdman on 20 Apr 2018

If you call it as follows, it works perfectly:

>>alloy='Al2';
T=300;
[Alloy,Temp]=ThCond(alloy,T)
The thermal conductivity(k_s) is 164.97
 Alloy =
    'Al2'
 Temp =
   300
  5 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!