Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Fall 2022 Engineering 7 Midterm Project

Questions and Instructions

You may start to work with your midterm exam at 8:00 am Friday October 7th, and you must finish it and submit your report and MATLAB code online before Monday October 17th 11:59 pm.

Problem 1. (25 points)

Write a MATLAB live code to sort a general M M (M is an odd integer) matrix in an ascending order, and then re-arrange it in a ascending spiral pattern such that the smallest element is in the center of the matrix (you may have multiple entries that have the same smallest value, but one of them is in the middle), and the largest elements are in the boundary of the matrix. For example, if you are given the following matrix (test-case example),

15 2 8 12    10

11 9 3 14 5

6 4 1 7 13

7 11    14 5 7

1 2 4 5 6

your code must be able to sort the matrix in ascending order in terms of row

1 1 2 2 3

 

6 6 7 7 7

8 9 10     11    11

12    13    14    14    15

and then re-arrange it into the following spiral pattern ascending from the mid- dle in counter-clock wise direction

  .

 

 

Then  generate a 11 11 matrix A = randi(100, 11), and sort and re-arrange it into the spiral pattern and visualize it by using the MATLAB command:

surf (A).

Note: Use of built-in search (find) or sort functions are prohibited.

Problem 2. (25 points)

Consider the piece-wise continuous function

x2 ,   x < 0

(1) Plot the function in the range [ 3, 3];

(2) Integrate the function in the interval [-3, 3] analytically, i.e.

3

(3) Write  a MATLAB  code to integrate the function from 3 to 3 by using the Riemann sum, i.e.

n

R = f (xi )∆xi

i=1

where  a x0 < x1 < x2 < · · · < xn b,  and  xi   [xi1, xi]  (Note  that  there n+1 points here, and ∆xi = (b  a)/n).  Choose (I) xi  xi; (II) xi  xi1, and

(III) xi  =  1 (xi1 + xi).

(4) Choose a = 3 and b = 3. Calculate R and compare the value with the

exact solution, i.e. calculate the relative numerical integration error as follows,

e = R Rexact

Rexact

where you are asked to choose n = 100 and n = 1000, and

(4) In your midterm report, you must present the numerical integration errors

e for the above three different cases, i.e.,

(1)xi  xi; (2)xi  xi1, and (3)xi  =  1 (xi1 + xi).

Discuss your findings in your report.

 

Problem 3. (25 points)

Write a general binary search code that can find a target with multiple appear- ances in an array. You can only use binary search algorithm. Use the following array to test your code to see if your code works:

A = [2, 4, 0, 1, 1, 2, 3, 3, 4, 4, 4, 5, 2, 8, 1, 10, 10, 1];

and choose the test targets as 1. Your code must return the indices of the target number in the original un-sorted array.

In your MATLAB code, no MATLAB built-in search or sort functions are allowed to be used.

Problem 4. (25 points) [Collatz problem] Consider the following function,

f (n) =

n

, if n is even;

2

3n + 1, if n is odd; where n is a natural number or positive integer.

 

Now write a MATLAB recursive function by performing the following oper- ation repeatedly, beginning with any positive integer, n, and taking the result at each step as the input for the next step. That is

ai =

 f (ai1), i = 1, 2, 3, · · · k


where k is a finite value, and n can be any positive integer. For the test case, choose a0 = n = 1234567890. Find out: how many iteration steps k are needed in order to reach ak = 1 for the given initial integer: n = 1234567890.

Remark:

Once you finish writing your code, provided it is correct, if you can find a positive integer n that cannot converge to 1, i.e. you cannot find a finite number k 1, such that ak = 1, you would prove that the Collatz conjecture is false.  In that

case, you will not only get an A+ in the class (forget the rest of HWs and final), and you will also be the winner of the next Fields Medal in mathematics ! In other words, you will become world-famous instantaneously. This is because the  Collatz  conjecture  “is  an  extraordinarily  difficult  problem,  completely   out

of reach of present day mathematics.”  This problem was posted more than 80

years, no one has been able to prove it or disprove it to this date.

 

Midterm Exam Logistic Information

• You are supposed to submit two files electronically to your Lab GSIs: a single live editor file, .mlx file, that contains the description of the problem, your MATLAB code, numerical solutions, and the discussions for each problem. In addition, you must also save the same .mlx file into the pdf format, in which the GSIs will grade and return it to you.

For example, you may name them as Shaofan midterm.mlx. and Shao- fan midterm.pdf.

All the functions, test case example inputs, execution line commands, and plotting commands should be included in a single .mlx-file for all four problems.

• You are supposed to write a short midterm exam report that includes  the outputs of your calculation for testing examples, graphs or pictures, comments, accuracy or error analysis, etc., and

• You must complete this midterm by yourself only. You cannot copy class- mates project file, and you should not allow anyone copy your midterm exam project file. If it is found that two submissions of .mlx files have identical codings, it will be viewed as an act of plagiarism. We have a code to check the similarity of the two codes, even if the variable names may have been changed. If two MATLAB codes from two individuals are identically the same, they will both receive failing grades.