MCD4140
Computing For Engineers
Background
The questions are designed to test your recollection of the lecture material. The teachers will
help you during your practical session. You will be assessed on the quality of your
programming style as well as the results produced by your programs. Save your work in M-
Files named prac10Task1.m, prac10Task2.m, etc.
Note: Students are expected to attempt these questions during their own self-study time. There may
be questions which require functions that may not have been covered in the lecture slides. If so, use
MATLAB’s built-in help to learn how to use them.
Instructions
You have to attempt all the pre-lab tasks in this laboratory sheet before your laboratory
class and show your work to your teacher at the start of the class.
Your teacher will discuss the solutions during the class, after checking your pre-lab work.
You have to attempt all the rest of the assessed tasks in this laboratory sheet during the
class and submit to Moodle.
After the assessed tasks there are supplementary questions that you are expected to try in your own
self-study time for additional practice and learning.
Learning outcomes
1. To revise user-defined functions and apply good programming practices
2. To identify the mathematics that represents the problem to be solved
3. To summarise the requirements and limitations of each ODE-solving method
4. To apply ODE-solving methods to solve ODEs both by hand and with MATLAB
MCD4140 Practical Session 10
MCD4140
MCD4140
Computing For Engineers
Pre-Lab Tasks
Complete these tasks before the laboratory class.
Task P1
Solve ( ) ( ) ( )3 11 2 ln , 0 2, 0.5, [0,2]t
dy
t y y e y h tspan
dt
++ = + = = =
a) By using Euler’s method
b) By using Heun’s method
c) By using Mid-Point method
This task should be done on a paper and bring to the practical session. Clearly show all the
calculations that you used. You may check your calculation by writing an M-file.
Solve the above ordinary differential equation (ODE) using Euler’s method with step sizes of:
1. h = 1
2. h = 0.75
3. h = 0.5
4. h = 0.001
a) For each step size, plot the results at each step starting from y(0)=3 to y(3).
b) Plot on the same figure as part a) the analytical solution which is given by:
c) Calculate and print the percentage error between the Euler’s method and the analytical
result at y(3).
MCD4140 Practical Session 10
MCD4140
MCD4140
Computing For Engineers
Laboratory Tasks
Complete these tasks during the laboratory class (you may work on them before the class if
you prefer to).
Task 1
The material chosen for the body panels of an automobile will be subjected to cyclic loading
(alternating positive and negative forces or stresses) during use. To test the material’s behaviour in
the laboratory under such cyclic loading, the force acting on the material is measured over time. A
computer precisely determines when the material sample fractures. The equation describing the
loading is:
)2cos()sin( 21 tFtF
dt
df
?? +=
where F1 = 34 kN, F2 = -18 kN and ω = 33 rad/s. The material was observed to fracture after 3 hours,
15 minutes and 5 seconds.
Write an M-file that uses ode45 to determine what the force acting on the material was at the instant
it fractures.
Answer:
The force acting on the material at 11705 second is -0.27162756 kN.
Task 2
A storage tank (shown below) contains a liquid at depth y where y = 0 when the tank is half full. Liquid
is withdrawn at a constant flow rate Q to meet demands. The contents are resupplied at a sinusoidal
rate 3Qsin2(t).
a) The change in volume can be written as:
MCD4140 Practical Session 10
MCD4140
MCD4140
Computing For Engineers
where A is the surface area and is assumed to be constant.
Use Euler’s method to solve for the depth y from t=0 to t=10 days with a step size of 0.1 days.
The parameter values are A=1250 m2 and Q=450 m3/day. Assume that the initial condition is
y=0.
Plot the solution.
b) For the same storage tank, suppose that the outflow is not constant but rather depends on
the depth. For this case, the differential equation for the change in volume can be written as
()
= 3 sin2() ? (1 + )1.5
Use Euler’s method to solve for the depth y from t=0 to 10 days with a step size of 0.1 days.
The parameter values are A=1250 m2, Q=450 m3/day and α=150. Assume that the initial
condition is y=0. Plot this solution on the previous figure. Remember to include a legend and
place it in the north-west corner of the figure.
Answer:
5
MCD4140 Practical Session 10
MCD4140
MCD4140
Computing For Engineers
Task 3
Newton's law describes the motion of a rocket-propelled sled as
where m is the sled mass, f is force of the rocket thrust, and c is the drag constant due to air resistance.
Assume = 1000 kg, = 75000 N and (0) = 0 m/s. Determine the velocity of the sled from t
= 0s to 10s (increments of 0.1s) using the midpoint method for = 50, 100, 500, 1000 and 2000 Ns/m.
You must use a for loop to go through each value of .
On a single figure, plot the velocity against time for each c and provide a statement that contains the
velocity of the sled at 10s for each c. E.g.
v=???m/s at t=10s if c=50
v=???m/s at t=10s if c=100
Answer:
Task 4
Solve the following ODE:in (5)
with y(0)=1 and t between 0 and 3 (inclusive) using the Euler, Heun and midpoint methods. Create a
1-by-3 subplot arrangement where each subplot contains the solutions of the 3 methods for h = 0.5
(first subplot), h = 0.2 (second subplot) and h = 0.01 (third subplot). In addition, each subplot should
plot the analytical solution of
= ?cos (5)
using a step size of 0.001 and a line thickness of 3. Remember to include a legend for each subplot.
6
MCD4140 Practical Session 10
MCD4140
MCD4140
Computing For Engineers
Create a table using fprintf which displays the step size, the midpoint method solution for t = 3 and
the absolute difference between the midpoint method and the analytical solution at t = 3. The table
should look similar to the following.
Step size Y(3) Absolute difference
0.5
0.2
0.01
Answer:
5 marks deducted for poor programming practices (missing comments,
unnecessary outputs, no axis labels, inefficient coding, etc.)
END OF ASSESSED TASKS
The remainder of this document contains supplementary tasks for extended learning. Use your
allocated lab time wisely!
7
MCD4140 Practical Session 10
MCD4140
MCD4140
Computing For Engineers
Supplementary questions
These questions are provided for your additional learning and are not assessed in any way. You may
find some of these questions challenging and may need to seek and examine functions that are not
taught in this unit. Remember to use the help documentation. Coded solutions will not be provided
on Moodle. Talk to your teachers to discuss any issues you are encountering.
TASK 1S
Solve the following equation, by hand, over the interval x=0 to x=1 using a step size of 0.25 assuming
y(0)=1.
d
d
= (1 + 4)√
Do this:
A. analytically,
B. using Euler’s method,
C. using Heun’s method, and
D. using the midpoint method.
Check your hand-calculated answers in MATLAB using your function files. Plot the solutions on the
same plot in addition to MATLAB’s ode45 solution.
SOLUTION
x y_analyt y_euler y_heun y_midpt
0.0000 1.0000 1.0000 1.0000 1.0000
0.2500 1.4102 1.2500 1.4045 1.3977
0.5000 2.2500 1.8090 2.2307 2.2110
0.7500 3.7539 2.8178 3.7061 3.6670
1.0000 6.2500 4.4964 6.1518 6.0862
The plot shows Euler's approach to be the worst approximation