In: Advanced Math
Write a MATLAB code to obtain the following. Keep your code commented whenever required. Copy your source code and command widow outcomes and screen shots of any plots in your solution.
Develop three functions for temperature-conversion.
Use the following equations to achieve these conversions.
1 degree Fahrenheit =255.928 Kelvin
1 degree Celsius =493.47 Rankine
1 degree Celsius =33.8 degrees Fahrenheit
Measurements |
Input Temperature |
Output of F_to_K |
Output of C_to_R |
Output of C_to_F |
Mean |
||||
Median |
||||
Variance |
||||
Standard Deviation |
Assumption:
The input file format is not provided. So I used a .txt file with integers in each line.
Assuming inputs to be in celsius I used direct input file for C_to_R function and C_to_F function and for the input of F_to_K function I used the output of C_to_F function.(Check below-Marked in RED)
Program:
function kel=F_to_K(fahr_A) %initiate function
kel=zeros(length(fahr_A),1); % create a output variable to store
converted values
for i=1:1:length(fahr_A) % loop to run over all the values
kel(i)= (fahr_A(i)+459.67) * (5/9); % fahr to kelvin
conversion
output_file=fopen('F_to_K2.txt','w'); % create a text file to write
the output
fprintf(output_file, '%f\n',kel); % store the converted kelvin
values in to the files
fclose(output_file); %close the opened files
end
function rankine=C_to_R(Cel_A)
rankine=zeros(length(Cel_A),1);
for i=1:1:length(Cel_A)
rankine(i)= (Cel_A(i)+273.15)*(9/5);
output_file=fopen('C_to_R2.txt','w');
fprintf(output_file, '%f\n',rankine);
fclose(output_file);
end
function fahr=C_to_F(Cel_A)
fahr=zeros(length(Cel_A),1);
for i=1:1:length(Cel_A)
fahr(i)= (Cel_A(i)*1.8) + 32;
output_file=fopen('C_to_F2.txt','w');
fprintf(output_file, '%f\n',fahr);
fclose(output_file);
end
Main function:
clc;
close all;
file_name=fopen('Tempr2.txt', 'r'); %Open the input file
A=fscanf(file_name,'%f'); % read the values in the files to matrix
A
fahr=C_to_F(A); % function to convert Celsius to Fahrenheit
%creating table to display conversion
VarNames = {'Celsius','Fahrenheit'}; % Header column for
table
T1 = table(A(1:20,:),fahr(1:20,:), 'VariableNames',VarNames) %
Values to fill for corresponding column
rankine=C_to_R(A); % function to convert Celsius to Rankine
%creating table to display conversion
VarNames = {'Celsius','Rankine'};
T2 = table(A(1:20,:),rankine(1:20,:),
'VariableNames',VarNames)
kel=F_to_K(fahr); %
function to convert Fahrenheit to kelvin
%creating table to display conversion
VarNames = {'Fahrenheit', 'kelvin'};
T3 = table(fahr(1:20,:),kel(1:20,:),
'VariableNames',VarNames)
%%creating table to display properties of output.
VarNames = {'Measurements', 'InputTempurature','OutputofF_to_K',
'OutputofC_to_R', 'OutputofC_to_F'};
measurements=["Mean"; "Median" ; "Variance"; "Standard
Deviation"];
input_temp=[mean(A) ; median(A); var(A); std(A)]; %Properties of
input data
op_f_k=[mean(kel) ; median(kel); var(kel); std(kel)]; %Properties
of output f_to_k
op_c_r=[mean(rankine) ; median(rankine); var(rankine);
std(rankine)]; %Properties of output of c_to_r
op_c_f=[mean(fahr) ; median(fahr); var(A); std(fahr)]; %Properties
of output of c_to_f
T4 = table(measurements,input_temp, op_f_k, op_c_r, op_c_f,
'VariableNames',VarNames)
Outputs: