Week 2 Programming Assignment 1: Expected Learning Outcomes From This Assignment

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

Week 2 Programming assignment 1

1
1) Consider the functions 𝑓(𝑥) = 6𝑥3 and 𝑔(𝑦) = 𝑠𝑖𝑛(2𝜋𝑦).
∂𝑓 ∂𝑔
a) Determine the values of derivatives ∂𝑥
at x = 3 and ∂𝑦 at y = 0.45, where y is in
radians, analytically.
∂𝑓 ∂𝑔
b) Write a MATLAB program to compute the derivatives ∂𝑥
at x = 3 and ∂𝑦 at y =
0.45, where y is in radians, using Finite Difference methods namely i) Forward
difference, ii) Backward difference and iii) Central differencing schemes. Use step
sizes of 0.01, 0.1 and 0.25 for both functions.
c) Create a table to compare and analyse the impact of the choice of the various
schemes and time steps used. Kindly use the below table format as reference.
d) Compare the results of the Finite difference schemes with that of the Analytical
solution and write down the percentage error obtained in each case.
% Error = {(FDM result – Analytical result)/ Analytical result}*100%

S. Function Analytical dx Forward FD % Backwar BD % Central CD %


No solution diff. error d diff. error diff. error
solution solution solution
1 1 3 0.01
𝑥
6 0.1
0.25
2 𝑠𝑖𝑛(2𝜋𝑦) 0.01
0.1
0.25

e) Using the results from the MATLAB program, as documented in the table
mentioned in sub-division c), answer the ‘Fill in the blanks questions’ in the
Programming assignment-1 portal.

Expected learning outcomes from this assignment:


1. To setup and use the MATLAB online module.
2. To write a basic .m file to solve the assignment problem. The code could follow
roughly the following algorithm although the candidate is encouraged to try and
experiment with other methods too.
Code Algorithm:

1. Obtain input from the user for the value of ‘x’ and ‘time step size’
2. Write three separate function blocks for solving the given equation in the question
using Forward, Backward and Central difference schemes using values of ‘x’ and
‘time step size’ obtained from the user. Each function block must contain only the
discretization formulas and not the equation itself. Eg:
∂𝑓 𝑓(𝑥 + ∆𝑥) ― 𝑓(𝑥)
=
∂𝑥 ∆𝑥

Where the 𝑓(𝑥 + ∆𝑥) must call another function with the equation given in the
1 3
problem. This avoids having to write the equation, say f(x) = 6
𝑥 , in the
discretization formula repeatedly.
1
3. Write a separate function for the equation, say f(x) = 6𝑥3, where the value of x
must accommodate 𝑥 + ∆𝑥 or 𝑥 ― ∆𝑥 depending on the scheme being solved. This
avoids having to write lengthy equations in the discretization formulae as such.
4. Write a function to calculate the percentage error based on the formula given in
the question. This function helps avoid having to write the same formula three
times for each of the discretization scheme.
5. Thus the code totally has 5 functions (3 for discretization schemes, 1 for the
equation to be solved and 1 for the percentage error calculation).
6. Display the values of the three schemes and their respective errors obtained for the
supplied values of ‘x’ and ‘time step size’.
7. Repeat the same procedure and write a separate script file for the function g(y).

3. The above algorithm is just a guideline and the candidate is encouraged to write the
MATLAB code as per his/ her interest. In fact, such different attempts are welcomed
as the candidates can share their code with their peers for discussion once the
assignment is closed. This will help everyone in learning better.
4. On successful completion of this code, the candidate would be familiar with writing
and compiling a MATLAB script file, writing algebraic equations and functions.
5. The candidate can also compare the impact of the various discretization schemes and
the time step on the accuracy of the solution obtained.

You might also like