Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

MATA2754 – Scientific Computing

Semester Test 1 of 2022


Marks: 40 Time: 110 minutes

Question 1 [3]
Create a vector, V, with exactly 97 components on the interval [-2, 50], including the
endpoints of the interval. Now delete every 2nd component of V in creating a new vector.
Lastly, use V again and create a new vector that automatically extracts all the integers
contained in V.

Question 2 [3]
Let M = rand(20,24); Assign all the elements in rows 2, 7 and 12 in columns 14 till 23 to a
new matrix called N. Assign the 3rd row of N to a variable called vec. Delete the 4th and 9th
columns of M simultaneously in forming matrix P.

Question 3 [8]
Consider the data points {(-3,4), (2,4), (-6,15), (8,12), (3,-3), (0,8)}. Plot these points as
magenta crosses. Now plot the function h(t) = 2t – sin(7t) + log2(t+10) on the same set of
axes. Add a title and axes labels as well as a legend. Now also add two thick straight lines to
the same set of axes to present the cartesian coordinate axes with the origin at (0,0).

Question 4 [6]
Copy and paste the following code into a new script file:

T = {'Jack'; 'Mpho'; 'Grace'; 'Thabo'};


T(:,2) = {1.85 1.72 1.65 1.7}';
T(:,3) = {87 62 71 91}';
T(:,4) = {21 22 31 27}'
Now use fprintf statements to create the following table displayed in the Command
Window:

Hint: You might find the info at the following webpage helpful:
https://www.mathworks.com/matlabcentral/answers/251592-printing-a-cell-array-of-
strings-and-numbers-with-fprintf

…turn over
1|P ag e
Question 5 [12]
1
Consider the series 1 + 1/4 + 1/9 + 1/16 + … +
𝑛2
Use a for-loop to determine the sum of this series for 𝑛 = 70 by initializing the sum and
then add one term to the sum during each iteration of the loop. Expand your code to test
𝜋2
after each new term is added, if the absolute difference between the current sum and is
6
less than 0.02. Let a message be displayed to the screen the first time this happens, but only
the first time. Put the number of terms that were added as the title of the plot. Also make a
plot that shows the 70 partial sums as red squares and also include on the plot the graph of
𝜋2
𝑦= in green and having a dashed style.
6

Question 6 [8]
Every time you go to the shop to buy bread, you have three choices of bread to buy. Let us
call them options A, B and C. You will always buy the most expensive one, but if more than
one has the same maximum price, you will make the final decision based on your general
order of preference. First choice is option C and 2nd choice would be option B.

Use conditional statements to make it possible to determine the outcome of going to the
shop if all the prices are known and the shop always have all three options available.

2|P ag e

You might also like