In: Computer Science
MATLAB
1. Introduce some if-statements into your energy.m code by considering the following items:
When you are done with the above mentioned steps, then create a larger if-statement, giving the option to the user to select either Metric or US customary units. Ask the user which unit system they want to work in. Hint: Copy and paste what you have for two units and just change the appropriate units. For one, the user enters mass in Kg, for other one, they enter Slug.
This code can have so many other things (such as weeding out the texts and matrices that the user might enter by mistake), but we would like to make it simple for now. For this assignment, you will have at least one small if- statement (scalar-vector choices) inside the larger if-statement (unit choices).
HERE IS THE ENERGY.M FILE CODE:
function[E]=energy
% This program accepts a vector of masses and calculates the energy
for
% them. It displays a linear and three logarithmic plots and
provides a
% table of values for mass entered and resulting energy from the
row vector
% entered.
%
clc
close all
help energy
c=2.9979e8;
m=input('Please enter a vector of masses in Kg=');
E=m*c^2;
subplot(2,2,1)
plot(m,E,'b-p')
xlabel('MASS (kg)')
ylabel('ENERGY (J)')
title('E vs m-NORMAL')
grid
subplot(2,2,2)
semilogx(m,E,'m-p')
xlabel('MASS (kg) IN LOG SCALE')
ylabel('ENERGY (J)')
title('E vs m-SEMILOGX')
grid
subplot(2,2,3)
semilogy(m,E,'r-p')
xlabel('MASS (kg)')
ylabel('ENERGY (J) IN LOG SCALE')
title('E vs m-SEMILOGY')
grid
subplot(2,2,4)
loglog(m,E,'g-p')
xlabel('MASS (kg) IN LOG SCALE')
ylabel('ENERGY (J) IN LOG SCALE')
title('E vs m-LOGLOG')
grid
filename = 'energy.xlsx';
A = {'Mass (kg)';'Energy (J)'};
xlswrite('energy.xlsx', A, 1, 'A1');
xlswrite('energy.xlsx', [m; E], 1, 'B1');
end
function[E]=energy
% This program accepts a vector of masses and calculates the energy for
% them. It displays a linear and three logarithmic plots and provides a
% table of values for mass entered and resulting energy from the row vector
% entered.
%
clc
close all
help energy
c=2.9979e8;
unt_choice = input('Enter your choice 1.Metric 2.US customary units : ');
if unt_choice == 1
vctr = input('Enter your choice 1.Vector 2.Scaler : ');
if vctr == 2
m=input('Please enter mass in Kg=');
E=m*c^2
fprintf(E)
else
ln = input('Enter the length of the vector : ');
m = zeros(1,ln);
q = 1
fprintf('Start Entering your vector of masses in Kg')
while q<ln
m(q) = input('...');
q += 1;
end
E=m.*c^2;
subplot(2,2,1)
plot(m,E,'b-p')
xlabel('MASS (kg)')
ylabel('ENERGY (J)')
title('E vs m-NORMAL')
grid
subplot(2,2,2)
semilogx(m,E,'m-p')
xlabel('MASS (kg) IN LOG SCALE')
ylabel('ENERGY (J)')
title('E vs m-SEMILOGX')
grid
subplot(2,2,3)
semilogy(m,E,'r-p')
xlabel('MASS (kg)')
ylabel('ENERGY (J) IN LOG SCALE')
title('E vs m-SEMILOGY')
grid
subplot(2,2,4)
loglog(m,E,'g-p')
xlabel('MASS (kg) IN LOG SCALE')
ylabel('ENERGY (J) IN LOG SCALE')
title('E vs m-LOGLOG')
grid
filename = 'energy.xlsx';
A = {'Mass (kg)';'Energy (J)'};
xlswrite('energy.xlsx', A, 1, 'A1');
xlswrite('energy.xlsx', [m; E], 1, 'B1');
end
else
vctr = input('Enter your choice 1.Vector 2.Scaler : ');
if vctr == 2
m=input('Please enter mass in slug=');
E=m*14.5939*c^2
fprintf(E)
else
ln = input('Enter the length of the vector : ');
m = zeros(1,ln);
q = 1
fprintf('Start Entering your vector of masses in slug')
while q<ln
m(q) = input('...');
q += 1;
end
m = m.*14.5939
E=m.*c^2;
subplot(2,2,1)
plot(m,E,'b-p')
xlabel('MASS (kg)')
ylabel('ENERGY (J)')
title('E vs m-NORMAL')
grid
subplot(2,2,2)
semilogx(m,E,'m-p')
xlabel('MASS (kg) IN LOG SCALE')
ylabel('ENERGY (J)')
title('E vs m-SEMILOGX')
grid
subplot(2,2,3)
semilogy(m,E,'r-p')
xlabel('MASS (kg)')
ylabel('ENERGY (J) IN LOG SCALE')
title('E vs m-SEMILOGY')
grid
subplot(2,2,4)
loglog(m,E,'g-p')
xlabel('MASS (kg) IN LOG SCALE')
ylabel('ENERGY (J) IN LOG SCALE')
title('E vs m-LOGLOG')
grid
filename = 'energy.xlsx';
A = {'Mass (kg)';'Energy (J)'};
xlswrite('energy.xlsx', A, 1, 'A1');
xlswrite('energy.xlsx', [m; E], 1, 'B1');
end
end
Sample Input:
Sample output: