write a function which takes temperature t as an array and the alloy name as a strung and returns the thermal conductivity k as an array of the same size as t in matlab

main.m
function k = getThermalConductivity(t, alloy)
% This function returns the thermal conductivity k as an array of the same size as t for a given alloy
% t: array of temperatures
% alloy: string indicating the type of alloy (e.g. 'steel', 'aluminum')
% k: array of thermal conductivity values for the given alloy at each temperature in t

    % define the thermal conductivity values in W/(m.K) for the specified alloy
    if strcmp(alloy,'steel')
        k_values = [45, 40, 36, 33, 30, 27, 24, 22, 20];
    elseif strcmp(alloy,'aluminum')
        k_values = [220, 215, 210, 205, 200, 195, 190, 185, 180];
    else
        error('Invalid alloy name. Choose from steel or aluminum');
    end
    
    % interpolate the thermal conductivity values at each temperature in t
    k = interp1([0:80:640], k_values, t, 'linear', 'extrap');
end
835 chars
19 lines

In this function, t is an array of temperatures and alloy is a string indicating the type of alloy. The function first checks the alloy string to see whether it is 'steel' or 'aluminum', and sets the k_values array accordingly. It then interpolates the thermal conductivity values at each temperature in t using the interp1 function, with linear interpolation and extrapolation for values outside the range of k_values. The resulting k array has the same size as t and contains the interpolated thermal conductivity values for the specified alloy at each temperature.

gistlibby LogSnag