Question

In: Mechanical Engineering

Mat Lab Question Mat Lab Question: %% Q1 (60%) % Follow the steps below to complete...

Mat Lab Question

Mat Lab Question:

%% Q1 (60%)
% Follow the steps below to complete this question.
%
% Step 1: (40%)
% Create a function to perform calculations: +, -, *, or /
% 1.1 The function name should be calc and saved in its file.
% 1.2 The function has three parameters a, b, and op. The op is an
% operator.  
% 1.3 The function returns variable r for the calculation result.
% 1.4 The function should provide the following help text:
% "The calc is a simple calculator. It has three parameters.
% The first two parameters are numerical operands, and the third one
% is an operator +, -, *, or / "
% 1.5 Use the if conditional statement in the funciton to check the operator op
% and compute accordingly.
% Note: Example about the syntax to check a single character: if(op == '+')
% Note: You can test the calc function in the Command Window using the syntax:
% >> calc(5, 3, '+')
% You can display the help text:
% >> help calc
%
% You can check if a division is divided by 0, but this is not required. Hint: use Inf or NaN
%

%% Q2 (15%)
% This question is similar to the requirement in Step 1 of Q1, except the
% following:
% Function name: calc_switch.m
% Here you want to use the switch statement instead of the if statement to check the operator.
% You are not required to do Steps 2-4 in Q1. But you can test this function in the same way.

Solutions

Expert Solution

Answer to question 1:

CODE:

function r=calc(a,b,op)
display('The calc is a simple calculator. It has three parameters.');
display('The first two parameters are numerical operands and the third one is an operator +, -, *, /');
if op=='+'
r=a+b;
else
if op=='-'
r=a-b;
else
if op=='*'
r=a*b;
else if op=='/'
r=a/b;
end
end
end
end


OUTPUT 1:

>> calc(5,3,'+')
The calc is a simple calculator. It has three parameters.
The first two parameters are numerical operands and the third one is an operator +, -, *, /

ans =

8

OUTPUT 2:

>> calc(3,0,'/')
The calc is a simple calculator. It has three parameters.
The first two parameters are numerical operands and the third one is an operator +, -, *, /

ans =

Inf

Answer to question 2:

CODE:
function r=calc_switch(a,b,op)
display('The calc_switch is a simple calculator. It has three parameters.');
display('The first two parameters are numerical operands and the third one is an operator +, -, *, /');
switch op
case '+'
r=a+b;
case '-'
r=a-b;
case '*'
r=a*b;
case '/'
r=a/b;
end

OUTPUT :

calc_switch(5,3,'+')
The calc_switch is a simple calculator. It has three parameters.
The first two parameters are numerical operands and the third one is an operator +, -, *, /

ans =

8


Related Solutions

Follow the steps below to complete your SPSS homework assignment: At the bottom of this document...
Follow the steps below to complete your SPSS homework assignment: At the bottom of this document are 20 questionnaires that were completed by undergraduate students at University XYZ. Open a new SPSS file and save it. Leave the output window open. You will save the output in just a moment. Use the questionnaire questions to correctly define each variable in an SPSS file. Further directions can be found at the following YouTube link: https://www.youtube.com/watch?v=MoKDcPpRa_0 Generate a “file information” output of...
Complete the problems below. Show some of your steps and explain the answer, too. Question #1...
Complete the problems below. Show some of your steps and explain the answer, too. Question #1 (6 points) A 95% confidence interval for a population proportion yielded the interval (.345, .455). Compute the margin of error. Compute the sample proportion. Will a 90% confidence interval be wider or narrower? Explain. Question #2 (6 points) According to recent figures, 6% of college graduates (aged 21-24) are unemployed, which is the lowest rate since before the recession. Suppose we take a random...
Steps to Complete the Week 6 Lab Find this article in the Chamberlain Library. Once you...
Steps to Complete the Week 6 Lab Find this article in the Chamberlain Library. Once you click each link, you will be logged into the Library and then click on "PDF Full Text". First Article: Confidence Intervals, ( I copied and pasted both articles at the bottom of this question). 1. Consider the use of confidence intervals in health sciences with these articles as inspiration and insights. 2. Describe how you could use confidence intervals to help make a decision...
Consider the scenario below, then follow the instructions underneath it to complete the discussion. If appropriate,...
Consider the scenario below, then follow the instructions underneath it to complete the discussion. If appropriate, support your position with credible resources/examples/evidence and provide APA references. Mr. B Mr. B, a 70-year-old male client, presented to his primary care physician with complaints of blurred vision and headaches over the last two months. On several visits, Mr. B's blood pressure was found to be elevated, so the physician started him on hydrochlorothiazide 25 mg by mouth daily. One month later, Mr....
Consider the scenario below, then follow the instructions underneath it to complete the discussion. If appropriate,...
Consider the scenario below, then follow the instructions underneath it to complete the discussion. If appropriate, support your position with credible resources/examples/evidence and provide APA references. Mr. B Mr. B, a 70-year-old male client, presented to his primary care physician with complaints of blurred vision and headaches over the last two months. On several visits, Mr. B's blood pressure was found to be elevated, so the physician started him on hydrochlorothiazide 25 mg by mouth daily. One month later, Mr....
Read the overview below and complete the activities that follow. For medical malpractice to be proven,...
Read the overview below and complete the activities that follow. For medical malpractice to be proven, all 4 elements of negligence must be present: Duty, Breach of Duty, Proximal Cause, and Harm. Defendants may deny wrongdoing, or take an affirmative or technical defense. Review this case and consider the elements of negligence and potential defenses. Case: In a case filed in the state of New York, the plaintiffs, a married couple - Mr. and Mrs. A - sought reproductive assistance...
Consider the scenario below, then follow the instructions underneath it to complete the discussion. If appropriate,...
Consider the scenario below, then follow the instructions underneath it to complete the discussion. If appropriate, support your position with credible resources/examples/evidence and provide APA references. Mr. D Mr. D is a 90-year-old man who was admitted to the hospital with complaints of nausea, vomiting, left arm pain, and chest pain. An electrocardiogram (ECG) is performed, and he is diagnosed as having a myocardial infarction. Mr. D has a long history of comorbidities including hypertension, diabetes, and congestive heart failure...
Use SPSS to follow the steps below and conduct a simple linear regression of the following...
Use SPSS to follow the steps below and conduct a simple linear regression of the following data: Calories (Xi) Sodium (Yi) 186 495 181 477 176 425 149 322 184 482 190 587 158 370 139 322 175 479 148 375 State your hypotheses (e.g. HA: “calories will significantly predict sodium”) Create a scatterplot of the data. State if the scatterplot appears to contain a linear relationship. Conduct the analysis in SPSS. Include all of the important outputs (e.g. ANOVA...
E-commerce Question Study the scenario and complete the question(s) that follow: Tshepiso Florists is a small...
E-commerce Question Study the scenario and complete the question(s) that follow: Tshepiso Florists is a small flower business located in the Glen Austin neighbourhood. There are two main types of clients that patronise Tshepiso Florists – gardening enthusiasts and people who need to send flowers to their loved ones on special occasions. Tshepiso is starting a gardening class online and she would like to send a notification via email to a particular group of clients. Create a database of at...
Strategy and a Company's Business Model Read the overview below and complete the activities that follow....
Strategy and a Company's Business Model Read the overview below and complete the activities that follow. While the company’s strategy sets forth an approach to offering superior value, a company’s business model is management’s blueprint for delivering a valuable product or service to customers in a manner that will yield an attractive profit. In this exercise you will be asked to evaluate the business model for SiriusXM. The two elements of a company’s business model are (1) its customer value...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT