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

조회 수: 2 (최근 30일)
Porgs
Porgs 2018년 4월 20일
댓글: Porgs 2018년 4월 20일
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

채택된 답변

Birdman
Birdman 2018년 4월 20일

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
Birdman
Birdman 2018년 4월 20일
It does accept with its last shape.
Porgs
Porgs 2018년 4월 20일
Thanks for all of your help!

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by