代写 algorithm matlab Questions:

Questions:
Assignment 4
EECS 1570 Winter 2019
(Due Monday March 11th, 2019 by 11:55pm)
1. (8 points) Use a for-loop to define a function to convert a text representing a binary number to decimal integer, and then test it as described below.
(1) Write a user-defined MATLAB function (named mybin2dec) that takes one single valued text input argument representation of a binary number and converts it to a decimal integer. You are not allowed to use MATLAB bin2dec function in your function definition, but can use it to check whether your function works correctly.
(2) Write a MATLAB script file that calls mybin2dec function and prints to the screen the values of binary numbers in the range of ‘0’ to ‘1100100’ (i.e. 0 to 100 in decimal).
Hint: you can use a for loop and the MATLAB function dec2bin to generate the binary numbers in the range specified above.
What to submit for this question: Please submit a plain text file (named a4q1.txt) that contain the contents of both the function file and the script file. Note that, to run the program, you need to have two separate files. But to submit them, you need to combine them (by copy and paste) into the a4q1.txt file to save paper when the TA prints them out.
2. (5 points) Write a MATLAB function, called myfx, that takes a real valued argument, say 𝑥, and returns the value of the function 𝑓(𝑥) computed according to the following rules:
𝑒𝑥 𝑖𝑓𝑥≤0
𝑓(𝑥)= √2𝑥2+3𝑥 𝑖𝑓2≤𝑥≤10
1 𝑥 𝑜𝑡h𝑒𝑟𝑤𝑖𝑠𝑒 9
The function should not output anything to the screen. Test your function and make sure it works correctly.
What to submit for this question: please submit myfx.m.
3. (8 points) The Babylonian method for approximating √𝑆 is based on the idea that if we guess a value, say 𝑥, and 𝑥2 > 𝑆, then 𝑆 will be an underestimate of √𝑆, and vice versa. Hence the
average of these two numbers provide a better estimate. One can repeat this averaging to get a close approximation to √𝑆 within some required accuracy. The algorithm is presented below:
𝑥

x=1
while(|𝑥2 −𝑆|>𝑒𝑝𝑠)
𝑥 = 1 (𝑥 + 𝑆) 2𝑥
end return x
(1) Write a MATLAB user-defined function (named mysqrt) that takes a single positive
real valued argument, say S, and computes √𝑆 using the Babylonian method described above. For 𝑒𝑝𝑠 you can use eps(single(1)). Use “help eps” to learn more about this function.
(2) Write a MATLAB script file that uses a while loop to repeatedly prompt the user to enter a positive real value number, say S, and
a) if S is negative it outputs “Invalid input!” and asks the user to re-enter the number,
b) if S is positive it outputs:
The square root of S is x.
where x is mysqrt(S)
c) if S is 0 it stops and outputs “Goodbye!”.
What to submit for this question: Please submit a plain text file (named a4q3.txt) that contain the contents of both the function file and the script file in the given order.
4. (8 points) The monthly payment M of a loan of amount P for N years and an annual interest rate r (in %) can be calculated by the formula:
𝑟
𝑀=𝑃 1200 −12𝑁
1−(1+ 𝑟 ) 1200
(1) Write a MATLAB user-defined function (named amort) that calculates the monthly payment of a loan. The input arguments of the function are P (the loan amount), r (the annual interest rate in percent), and N (the length of the loan in years). You can assume that all the input arguments are single-valued.
(2) Write a MATLAB script file that
a) prompts the user to enter a value for the loan amount, a value for the annual interest
rate in percent, and a value for the length of the loan in years
b) calls the amort function to calculate the monthly payment
c) prints the calculated monthly payment on the command window as follows:
The monthly payment is $x.

5.
where x is the calculated monthly payment amount with two digits after the decimal point. For example, if the user enters 260000 as the loan amount, 6.75 as the interest rate (in percent), and 15 as the length of the loan (in years), you program should output the following to the command window:
The monthly payment is $2300.76.
What to submit for this question: Please submit a plain text file (named a4q4.txt) that
contains the contents of both the function file and the script file.
(5 points) Write a user-defined MATLAB function with two input arguments and two output arguments, which determines the height in centimetres and weight in kilograms of a person from his/her height in inches and weight in pounds. The function should be named as IPtoCK. The input arguments are the height in inches and weight in pounds, and the output arguments are height in centimetres and weight in kilograms. Note that one inch equals to 2.54 centimetres, and one pound equals to 0.4536 kilograms.
What to submit for this question: Please submit your function file (named IPtoCK.m) . (16 points) You are given a text file (named HeightWeightIP.txt) that contains the
heights and weights of 50 people in the following format:
58.27 99.65
67.32 158.73
69.69 147.27
63.78 167.33
66.54 155.20
68.11 149.03
:: ::
where the first number in a row is a person’s height in inches and the second number is the person’s weight in pounds. The file can be downloaded at the page where you downloaded this assignment file. Write a MATLAB script file (named a4q5.m) that
(1) reads the data in the HeightWeightIP.txt file
(2) computes the body mass index (BMI) of each person based on his/her height and
weight. For this step, your program should call the IPtoCK function that you wrote for the last question to convert the height in inches and weight in pounds to the corresponding centimetres and kilograms. After the conversion, your program should calculate the BMI value of the person using the following formula:
𝐵𝑀𝐼 = 𝑘𝑔 (0.01 × 𝑐𝑚)2
where kg is the person’s weight in kilogram and cm is the person’s height in
centimetres.
(3) determines the BMI category that each person belongs to by using the following
information:
6.

BMI category
BMI range
underweight
𝐵𝑀𝐼 < 18.5 normal 18.5 ≤ 𝐵𝑀𝐼 < 25 overweight 25 ≤ 𝐵𝑀𝐼 < 30 obese 𝐵𝑀𝐼 ≥ 30 (4) outputs the following information to a text file named BMI.txt: Height(cm) Weight(kg) BMI Category 148.0 45.2 171.0 72.0 177.0 66.8 162.0 75.9 169.0 70.4 173.0 67.6 20.6 normal 24.6 normal 21.3 normal 28.9 overweight 24.6 normal 22.6 normal :::: :::: The output file should contain 23 rows and four columns. The first row is the heading. Each row after that describes the height (in centimetres), weight (in kilograms), the BMI value, and the BMI category of the person in the corresponding row of the input file. (Hint: you can create such a file with fprintf and a loop statement.) What to submit for this question: Please submit your script file (named a4q6.m) and the BMI.txt file generated by your program. What to hand in You need to submit your assignment electronically. Please submit seven files: • a4q1.txt (for Question 1) • myfx.m (the function file for Question 2) • a4q3.txt (for Question 3) • a4q4.txt (for Question 4) • IPtoCK.m (the function file for Question 5) • a4q6.m (the script file for Question 6) • BMI.txt (the output text file that your program generates for Question 6) This assignment is worth 6% of the final mark.