Word

advertisement
MAT-07B Conditional Statements HW
MATLAB 5th Edition: Chapter 6, Problems 26 Part A, Problem A, and Problem B
Do this assignment in one Script File. The first six lines should be:
clc
clear
format compact
disp ('First Name, Last Name')
disp ('ENGR1181, Seat #')
disp ('Assignment 7')
Then for
each problem
disp ('Problem #')
prob# = <Problem Solution>
Special Instructions:
Problem A:
Write a program that calculates the value of the summation using m as a random number
between 5 and 20 ( m = randi([5,20],1,1) ). Display the number m in the command
window with fprintf. The program will then carry out the calculation of the estimate given
below for all numbers n = [0:1:m].
Then use the sum function to perform a summation of the estimates. This series should converge
to π. If the difference between the summation and the actual value of π is greater than 10-7 then
display the message ‘need longer array’ with fprintf.
Problem B:
Write a program that creates a 16 element array of integers between 0 and 20 using the command
randi([0 20],1,16). Have the program complete the following tasks
a) Check the number of elements greater than 10 and the number of elements less than 10.
If there are more elements greater than 10 than less than 10 print the following message
There are XX elements greater than 10
If there are more elements less than 10 than greater than 10, print the following message
There are XX elements less than 10
otherwise display the following message:
There are XX elements greater than 10 and also XX
elements less than 10
where XX is the actual number elements
Use fprintf to display all the messages.
b) transfer the elements of the array into two separate vectors:
tenplus(contains all elements greater than ten) and tenminus(contains all elements less than
ten) and use disp to display the one with more elements. DO NOT type the elements into
the array.
Problem 26 a:
Solve Problem 26a using the given flowchart. Follow the flowchart as a programming strategy.
Comment your code to explain how you used the flow chart at every step. Do only part a.
However, you’re program should be able to provide the classification for a person of any given
height and weight.
START
Input :
Height,
Weight
Calculate:
BMI = 703*W/H2
If BMI < 18.5
False
If BMI<25
True
False
If BMI<30
Else
True
True
Underweight
Normal
Overweight
Obese
End
A good practice when debugging a script file is to test each portion of the code separately. Instead
of creating separate files to test code, one of the nice things in MATLAB is the ability to highlight a
block of code, right click, and select comment. The commented code will be ignored on subsequent
runs. So one can debug problem A, comment it, and work on problem B. When problem B is
working, the code for problem A can be highlighted again, only this time select uncomment. One
final run can be done to create a copy of the command window to turn in.
Beginning with this assignment and for all subsequent assignments, you need to include units
whenever you have them. If you do not understand the units used, ask. If you do not add units,
points will be deducted.
Submit printouts of both the Script File and the Command Window
Download