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

ENG1014: ENGINEERING NUMERICAL ANALYSIS

LAB 5 – WEEK 5

2022 S2

Welcome to lab 5. Remember that laboratories continuously build on previously learned concepts and lab tasks. Therefore, it is crucial that you complete all previous labs before attempting the current one.

Learning outcomes:

1. To understand and predict the output of statements that involve relational (< > == ~=) and logical (& | ~) operators

2. To differentiate between double and logical data types, and be able to filter matrices using logicals

3. To demonstrate the use of simple if statements and loops

4. To distinguish the usage between for loops and while loops

5. To be able to present an algorithm plan in succinct graphical form

6. To use the debugging tool with various breakpoints, and develop debugging skills while solving problems involving loops

7. To apply if statements and loops to physical problems


Submission requirements:

The lab submission links can be found on Moodle under the weekly sections. The submission box will only accept one .zip file. Zipping general instructions aredescribed here.

Deadline:

The lab tasks are due weekly at 11.55pm on Friday - check the submission box for details.



Lab 5 – Assessed questions


Note: Team tasks are designed for students to recall material that they should be familiar with through the workshops and practice of the individual questions prior to this lab session.

Students will be split into groups of 3-4 for the team tasks. Students in each group must explain aspects of the question below to receive the marks. Ensure that everyone has equal learning opportunities. Additionally, ask your table for help.

if statements:

Consider the following codes, which is supposed to  return y = 1 if x is  negative, y = 2 if x is  positive, and y = 3 if x  ==  5 (in that order):

Code A:

x = 5;

if x < 0

y = 1;

elseif x > 0

y = 2;

elseif x == 5

y = 3;

end

Code B:

x = 5;

if x < 0

y = 1;

if x > 0

y = 2;

if x ==5

y = 3;

end

end

end


Complete the following for both Codes A and B:

1.    Copy the code in the slide.

2.    Identify/highlight the lines of code that are executed by MATLAB and determine the output of y .

3.    Describe the differences between Code A and Code B.

4.    Discuss the task and explore any misunderstandings.

5.    Have a demonstrator assess your understanding.

Logicals:

Consider the Titanic data stored in a structure variable named T, such that the numerical field of the structure is accessed through T.data.


Passenger ID

Survived 1=Yes, 2=No

Gender 1=Male, 2=Female

Age

Fare ($)

121

2

1

22

7.25

243

1

2

38

71.28

432

1

2

26

7.93

564

1

2

35

53.1

856

2

1

35

8.05


Complete the following you may want to have a couple of students checking the syntax and output on MATLAB.

1.    Copy-paste an image of the table above.

2.   Annotate and provide the syntax to exact the:

a.    1st  column and assign it to the variable named id

b.    3rd  column and assign it to the variable named gender

c.    4th  column and assign it to a variable named age

3.    Provide the output of:

a.    index = age>30

b.   old_age = age(index)

c.    old_age_id = id(index)

4.    Provide syntax that will return a vector containing all female passenger IDs

5.    Create a logical named age38 that contains a 1 (true) where the age of the passenger is 38 and a 0 (false) where the passenger age doesn’t equal to 38.

1.    Provide syntax to extract the 2nd  and 3rd  columns of information for the passenger aged 38, using the variable created in step 5

2.    Discuss the task and explore any misunderstandings.

3.    Have a demonstrator assess your understanding.

Loops:

Consider the following codes:


Code A:

data = [3, 7, 8, 1, 4];

for i = 1:length(data)

x = data.^2;

end

Code C:

data = [3, 7, 8, 1, 4];

for i = 1:length(data)

x = data(i).^2;

end

Code B:

data = [3, 7, 8, 1, 4];

for i = 1:length(data)

x(i) = data(i).^2;

end

Code D:

data = [3, 7, 8, 1, 4];

for i = data

x(i) = data(i).^2;

end


You will be assigned two of the above Codes.

1.    Copy the code in the slide.

2.    Provide the values of i and x for each iteration – use the debugger tool if required. Example table shown below.

i

x

1

??

2

??

3.   Write a while  loop that triples the value of x with each  iteration, with x starting as 5. Create a counter to determine the number of times x is tripled before it is greater than 1000.

4.    Discuss the task and explore any misunderstandings. Also, browse the work of other teams related to the other Codes and ensure that you have understood it as concepts from all sets may be required for the individual tasks.

5.    Have a demonstrator assess your understanding

Remember good programming practices for all tasks even if not specifically stated. This includes, but is not limited to:

•    using clc, close all, and clear all, where appropriate

•    suppressing outputs where appropriate

•    labelling all plots, and providing a legend where appropriate

•    fprintf statements containing relevant answers


As price house sore across Melbourne, Mr ENG1014 is concerned and wants to start save in order to buy his dream        house worth $1,100,000. He currently has 5% of the house price saved in his savings account and his net income is          $84,000 per year. Each year, he saves 31% of his net income and adds it to his savings. In order to buy his dream house, he wants to have 10% of the total house cost. He knows that house prices and wages are also increasing. To account for this, Mr ENG 1014 makes an educated guess that house prices will increase 10% per year and his net income increases   4% per year.

NOTE: At the end of 2022, he will have the 5% saved and the house price will be $1,100,000 and his salary will be $84,000. Ie, 2022 is the start value and does not include an iteration. 2023 will be iteration 1.

How many years will it take for Mr ENG1014 to save the 10% deposit required to buy his dream house. Store the history of deposit and house prices in vector and make a plot that shows the yearly deposit and the house price changes over    the years.

HINT: Use a while loop.