Question

In: Computer Science

MATLAB 1. Introduce some if-statements into your energy.m code by considering the following items: Ask the...

MATLAB

1. Introduce some if-statements into your energy.m code by considering the following items:

  • Ask the user to enter the mass of the object (mention that they can either enter a scalar or a vector). Then the if-statement defines if the input is a scalar or a vector. If it is a scalar, then use fprintf to show the calculated energy in Joule.
  • If it is a vector, sort the mass vector and then plot energy versus mass. Plot for them the subplots that you considered before (better to have 4 subplots).

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

Solutions

Expert Solution

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:


Related Solutions

Write a MATLAB code to obtain the following. Keep your code commented whenever required. Copy your...
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. Create a function called F_to_K that converts and return temperatures in Fahrenheit to Kelvin and store results in ‘F_to_K2.txt’. Create a function called C_to_R that converts and return temperatures in Celsius to Rankine and store results in ‘C_to_R2.txt’. Create a function called C_to_F that...
Write a MATLAB code to obtain the following. Keep your code commented whenever required. Copy your...
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. Write a user defined function ‘My_FunctionGen’. It accepts, the time vector ‘t’ with 8000 uniformly spaced values within the range of 0 to 8, Frequecy scalars ‘f1<100H’ and ‘f2<80Hz’ and Amplitude scalars ‘A1’, ‘A2’ and ‘A3’ as input arguments. It delivers x1, x2 and x3 and x4 as outputs...
Write a MATLAB code to obtain the following. Keep your code commented whenever required. Copy your...
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. Generate the following vectors and also implement the Table-1 operations-map by employing the switch statement and an appropriate loop. Generate a vector ‘DEC1’ as follow. DEC1 = 5     1     3     0     2    5     0     2 Concatenate vector ‘DEC1’ eight times in order to obtain a bigger row vector ‘DEC’...
Write a MATLAB code to obtain the following. Keep your code commented whenever required. Copy your...
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. Create a function called F_to_K that converts and return temperatures in Fahrenheit to Kelvin and store results in ‘F_to_K2.txt’. Create a function called C_to_R that converts and return temperatures in Celsius to Rankine and store results in ‘C_to_R2.txt’. Create a function called C_to_F that...
Matlab assignment. The objectives of this project are (1) to introduce the students to scripting applied...
Matlab assignment. The objectives of this project are (1) to introduce the students to scripting applied to solution of mechanical engineering problems and (2) to create a Matlab script that allows the computation of principal stresses and strains starting from a generic state of stress and that automates the drawing of 3D Mohr circles Assignment 1) Read from input a stress tensor (3D); 2) For any state of 3D stress compute the principal stress values (σ1, σ2, σ3) with σ1...
This is a Matlab Exercise problem. Please create the Matlab code and figure for the following...
This is a Matlab Exercise problem. Please create the Matlab code and figure for the following problem using problem specifications: Plot x vs y when y=sin(x), y=cos(x), y=sin (2*x), and y=2*sin(x) when x = 1:0.1:10. Use 2 by 2 subplot, sin(x) is in location 1, cos(x) is in location 2, sin(2*x) is in location 3 and 2*sin(x) is in location 4. The plot should have: (1) x label = ‘x value’, y label = ‘y value’, legend ‘y=sin(x)’,’ y=cos(x)’,’ y=sin...
Use Matlab to write the following 1. Write code to determine whether a year is a...
Use Matlab to write the following 1. Write code to determine whether a year is a leap year. Use the mod function. The rules for determining leap years in the Gregorian calendar are as follows: All years evenly divisible by 400 are leap years. Years evenly divisible by 100, but not by 400, are not leap years. Years divisible by 4, but not by 100, are leap years. All other years are not leap years. For example, the years 1800,...
(1)Using the Matlab code developed in Software Assignment #1: a. Convert the code that generates the...
(1)Using the Matlab code developed in Software Assignment #1: a. Convert the code that generates the random number (H,T) with equal probabilities into a function called myBernolli(p, S) that takes as an input the probability of success p and S is the outcome defined as success (either T or H) and returns the outcome of the trial (either T or H). b. Test that your function is actually producing the successful outcome with probability p by running the function in...
The following code must be written using matlab How to loop through a vector in matlab...
The following code must be written using matlab How to loop through a vector in matlab and assigning a value to every 4th entry. The vector could be of any length. Thanks
Deep leraning/LSTM/Matlab There is a Matlab code that is doing the following steps for deep learning...
Deep leraning/LSTM/Matlab There is a Matlab code that is doing the following steps for deep learning and applying LSTM, I need to change first three steps to use our dataset to train this model and you don't need to change other. I need to apply that for .ogg audio files so Create and Use some audio files with .ogg format as sample data and give me the code. The following steps is for your information: Three classes of audio signals...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT