Assignment 1 Answers PDF
Assignment 1 Answers PDF
Engineering
MATLAB ASSIGNMENT 1
Then we invoke the function A1Q1 on dy/dt = 2t from [0 10] with a step size of 0.2 and the
initial condition being 0.
t values:
y values:
10y + z = 2
x + 3y – z = 6
2x + 4y + z = 5
The Gauss elimination file (named A1Q2) is as below, with clear explanation on what each
line means.
The resultant matrix we obtain is:
Invoking the A1Q2 function file after adding in the back-substitution algorithm, we obtain
our solutions.
3. Consider the function f(x) = x3 – 2x + 4 on the interval [-2, 2] with h = 0.25. Write the
MATLAB function file to find the first derivatives in the entire interval by all three
methods i.e., forward, backward, and centered finite difference approximations.
Explanation:
Firstly, we call the function file as A1Q3.
We include the term before -2 and after 2. We require the 'plus one term' for forward
difference when x = 2 and the ‘minus one term’ for backward difference approximations.
Therefore, we use the values of x from -2.25 to 2.25 with a step size of 0.25, adding them
into our row matrix for easy indexing. (shown below)
The interval for 𝑓 (𝑥𝑖+1 ) is [-1.75, 2.25], which are the 3rd to last term (3:end) in the full
interval of x.
The interval for 𝑓 (𝑥𝑖 ) is [-2, 2], which are the 2nd to 2nd last term (2:end-1) in the full interval
of x.
Invoking the A1Q3 function on the Forward Finite Difference Approximation, we get the
following array of values, named f1.
The interval for 𝑓 (𝑥𝑖 ) is [-2, 2], which are the 2nd to 2nd last term (2:end-1) in the full interval
of x.
The interval for 𝑓 (𝑥𝑖−1 ) is [-2.25, 1.75], which are the 1rd to the 3rd last term (1:end-2) in the
full interval of x.
Invoking the A1Q3 function on the Backward Finite Difference Approximation, we get the
following array of values, named f2.
The interval for 𝑓 (𝑥𝑖+1 ) is [-1.75, 2.25], which are the 3rd to last term (3:end) in the full
interval of x.
The interval for 𝑓 (𝑥𝑖−1 ) is [-2.25, 1.75], which are the 1rd to the 3rd last term (1:end-2) in the
full interval of x.
Invoking the A1Q3 function on the Centered Finite Difference Approximation, we get the
following array of values, named f3.
4. You are designing a spherical tank to hold water for a small village in a developing
country. The volume of liquid it can hold can be computed as follows:
(𝟑𝑹 − 𝒉)
𝑽 = 𝝅𝒉𝟐
𝟑
Where V=volume [m3], h=depth of water in tank [m], and R= the tank radius [m].
If R = 3m, what depth must the tank be filled to so that it holds 30 m 3? Write a function
file to solve for the depth of the tank using three iterations of the False- Position
Method.
Then we invoke the A1Q4 function written above:
5. Evaluate 𝑦 = (𝑥 + 10) (𝑥5 + 1) for values of x in the range of -1 to 1 in step sizes of 0.1.
Hence, plot y versus x.
Function file:
a) The values of 𝑦 = (𝑥 + 10) (𝑥5 + 1) for values of x in the range of -1 to 1 in step sizes of
0.1 are as follows:
b) The graph of 𝑦 = (𝑥 + 10) (𝑥5 + 1)
6. Write a function that takes numeric data as its input argument and prints a message
to the Command Window stating if the number is positive, or negative, or 0. This
function should transfer an output value to the Workspace ONLY when the input value
is negative.
Notice now that the output value was transferred to the Workspace as input is negative:
𝒙𝟐 𝒙𝟒 𝒙𝟔
7. 𝒄𝒐𝒔 𝒙 = 𝟏 − + 𝟒! −
𝟐 𝟔!
𝝅
Write a MATLAB script file to estimate cos ( 𝟑 ) and percentage error by adding terms one
at a time. Starting with the simplest version, cos x = 1, add terms one at a time.
i) By using syntax ‘taylor’ [Hint: You have to explore this function of MATLAB]
10. Many rides at amusement parks require riders to be a certain minimum height.
Assume that the minimum height is 120 cm for a certain ride. Write a function to
determine whether the rider is tall enough. When the function is called, it should
display one of the following information to the command window.
1 1 1
Let A = 2 5 3
3 −2 −2
𝑥
Let b = 𝑦
𝑧
10
Let C = 33
0
We know that 𝐴 ∙ 𝑏 = 𝐶, then making b the subject to find out the solutions,
𝑏 = 𝐴−1 𝐶