Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Assignment 1 Answers PDF

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

School of Chemical and Biomedical

Engineering

CH2107: Intro to Computational


Thinking

MATLAB ASSIGNMENT 1

NAME: KEOW ZHI HENG

MATRIC NUMBER: U1922880D


1. Solve the following differential equation in MATLAB using solver ‘ode45’
dy/dt = 2t. Solve this equation for the time interval [0 10] with a step size of 0.2 and the
initial condition is 0.

First, we set up the function file, naming the function A1Q1:

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.

We arrive at these answers:

t values:
y values:

2. Solve this system of equations using Gauss Elimination in MATLAB by writing a


Function file.

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:

However to fully solve this system of equations we need to do back substitution.


Lines 18 to 20 shows us the algorithm for back substitution.

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.

Full function file:

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)

Forward Finite Difference Approximation


The formula for the first derivative under this method is:
𝑓(𝑥𝑖+1 ) − 𝑓(𝑥𝑖 )
𝑓 ′ (𝑥 𝑖 ) =

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.

Backward Finite Difference Approximation


The formula for the first derivative under this method is:
𝑓(𝑥𝑖 ) − 𝑓(𝑥𝑖−1 )
𝑓 ′ (𝑥 𝑖 ) =

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.

Centered Finite Difference Approximation


The formula for the first derivative under this method is:
𝑓 (𝑥𝑖+1 ) − 𝑓(𝑥𝑖−1 )
𝑓 ′ (𝑥 𝑖 ) =
2ℎ

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:

Invoking the A1Q5 function, we get:

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.

The function file:

Test Positive Numbers:


Notice that the output value did not transfer to the Workspace as input is positive:

Test Negative Numbers:

Notice now that the output value was transferred to the Workspace as input is negative:

Now test zero.

𝒙𝟐 𝒙𝟒 𝒙𝟔
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.

First, to help us see the pattern, rewrite the expression as:


𝒙 𝟎 𝒙𝟐 𝒙𝟒 𝒙𝟔
𝒄𝒐𝒔 𝒙 = − + −
𝟎! 𝟐! 𝟒! 𝟔!
(−1)𝑛 (𝑥)2𝑛
The general formula for the nth term (where n = 0, 1, 2…) is (2𝑛)!
The approach to take for creating this script file is to start from the first term using the
above general formula and apply the accumulator concept.
Script file:
Command Window:
Invoking the abovenamed A1Q7 function, we get:

8. Let x = [x1 x2 x3 x4….xn] and y = [y1 y2 y3 y4…….yn]. Write a MATLAB function to


determine the following:
(x1y2-x2y1+x2y3-x3y2+….xny1-x1yn)
Now assume x = [1 2 3 4 5] and y = [10 20 30 40 50]. Evaluate the above in this case.
9. Find the fourth order approximation of log x using base point at x = 1 using the two
methods:

i) By using syntax ‘taylor’ [Hint: You have to explore this function of MATLAB]

Code can also be directly used in the command window.


ii) Write a script file and do not use syntax ‘taylor’

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.

i) Sorry, you are not tall enough.


ii) Height check successful.
11. Solve the linear system of equations in MATLAB. [Hint: You can use ‘inv’ syntax to do
it]
x + y + z =10
2x + 5y + 3z = 33
3x = 2y + 2z

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 𝐶

Using MATLAB to solve the above system of linear equations,

Solutions: x = 4; y = 3.5; z = 2.5


12. Write a MATLAB code where the MATLAB asked the user to specify the value of
acceleration due to gravity and time and store it in variable ‘g’ and ‘t’. Based on these
two values, MATLAB will calculate distance (d) which is 0.5*g*t*t and display the
output.

You might also like