hw2.pdf

ENGR 160 Algorithms and Applications Summer I 2015 Homework Assignment #2 Due 5:00pm May 29 (F) Read Chapters 3, 4 and 5 in the book. Work through the examples and practice problems. You do not have to submit any of this work. Problem 2.1 Create a MATLAB script file to solve Exercise 18 of Chapter 4. Test your program by running it at least twice, one with an even number, and another with an odd number. Attach the results at the end of your program as comments. Problem 2.2 Create a MATLAB script file to solve Exercise 21 of Chapter 4, with the following additions: Hurricanes can be categorized based on storm surges. The following table shows the Category number for hurricanes with varying storm surge. Category number storm surge range (feet above normal) 1 4-5 2 6-8 3 9-12 4 13-18 5 >18 Write a script that will prompt the user for the wind speed, and will print what type of storm it is. If it is a hurricane, then your program should prompt the user to enter the storm surge, and will print which category the hurricane is. Your program should print an error message if the user inputs are not in the correct range. Test your program by running it through all possible cases, including an invalid wind speed, and an invalid storm surge. Attach the results at the end of your program as comments. Problem 2.3 Create and run a MATLAB script file to plot the following function for x over the range of [- 2π, 2π] with step size of 0.01π. Show your program and result figure.

  

 





2 ,1

2 ,1)(sin2

,1

)( 2

 

x

xx

x

xy

Problem 2.4 Create a MATLAB program named find_max_N to find the largest integer N that satisfies the following inequality, where M is the upper bound that can be input by user.

M N

N N 

 

 12

12 )2(...

3

5 4

1

3 2 222

Use the following examples: M=0; 1; 10; 100 and 1000 to test your program. Attach the results at the end of your program as comments. Problem 2.5 A magic matrix is a square matrix (with N rows and N columns) constructed from the integers 1 through N2, and the sums along each row, each column, and the two diagonal lines are all

equal. For example,

  

  

294

753

618

is a 3-by-3 magic matrix. The sum of each row, column, and

diagonal line is 15. Write a MATLAB script file called ismagic that takes a square matrix as input and determine if this matrix is magic. Test your program using the following matrices:

1. A =  

  

 625

143

2. B =

   

   

115144

12679

810115

133216

3. C= reshape(randperm(16),4,4)

Attach your results as comments. Note that not only do you need to check if the sums along rows, columns, and diagonal lines are equal, but also need to check if all the elements are from 1 to N2.