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

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

 Akzeptierte Antwort

Birdman
Birdman am 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 Kommentare

would I be able to use an array instead of an integer for the temperature in this function?
Birdman
Birdman am 20 Apr. 2018
Bearbeitet: Birdman am 20 Apr. 2018

Yes but the function needs to be changed. I erased else parts because of irrelevant disp function.

function [alloy,k] = ThCond(alloy,T)
k=zeros(1,numel(T));
for i=1:numel(T)
      if (298 <= T(i)) && (T(i) <= 840) && strcmp(alloy,'Al2')
         k(i) = 149.7 + 0.0809.*T(i) - (1.*10^(-4)).*(T(i).^2);
         fprintf('\nThe thermal conductivity(k_s) is %6.2f\n', k(i)');
      elseif (298 <= T(i)) && (T(i) <= 773)
         k(i) = 76.64 + 0.2633.*T(i) - (2.*10^(-4)).*(T(i)^2);
         fprintf('\nThe thermal conductivity(k_s) is %6.2f\n', k(i)')
      end
      if (293 <= T(i)) && (T(i) <= 890) && strcmp(alloy,'Al3')
          k(i) = 124.7 + 0.56.*T(i) + (1*10^(-5)).*(T(i)^2);
          fprintf('\nThe thermal conductivity(k_s) is %6.2f\n', k(i)')
      end
      if (100 <= T(i)) && (T(i) <= 1200) && strcmp(alloy,'Cu1')
          k(i) = 453.9 - 0.1054.*T(i);
          fprintf('\nThe thermal conductivity(k_s) is %6.2f\n', k(i)')
      end
      if (460 <= T(i)) && (T(i) <= 1188) && strcmp(alloy,'Cu2')
          k(i) = 140.62 + (112.14.*10^(-4)).*T(i);
          fprintf('\nThe thermal conductivity(k_s) is %6.2f\n', k(i)')
      end
      if  T(i) <= 1443 && strcmp(alloy,'Cu3')
          k(i) = 16.041 + (438.9.*10^(-4)).*T(i);
          fprintf('\nThe thermal conductivity(k_s) is %6.2f\n', k(i)')
      end
      if (400 <= T(i)) && (T(i) <= 1000) && strcmp(alloy,'St1')
          k(i) = 76.63 - 0.0459.*T(i);
          fprintf('\nThe thermal conductivity(k_s) is %6.2f\n', k(i)')
      end
      if (298 < T(i)) && (T(i) < 1573) && strcmp(alloy,'St2')
          k(i) = 6.31 + ((27.2.*10^(-3)).*T(i)) - (7.*10^(-6)).*(T(i).^2);
          fprintf('\nThe thermal conductivity(k_s) is %6.2f\n', k(i)')
      end
      if  T(i) <= 1727 && strcmp(alloy,'St3')
          k(i) = 20 + (61.5.*10^(-4)).*T(i);
          fprintf('\nThe thermal conductivity(k_s) is %6.2f\n', k(i)')
      end  
end
end

and call it like:

alloy='Al2';
T=[300 600 800];
[Alloy,Temp]=ThCond(alloy,T)

Sterling's answer moved here:

Would there be a way to make the function accept both integers and arrays into the same function for T?

It does accept with its last shape.
Thanks for all of your help!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Modeling finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 20 Apr. 2018

Kommentiert:

am 20 Apr. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by