THE CHINESE UNIVERSITY OF HONG KONG
Department of Mathematics
2024-25 Term 2 MATH2221C Mathematics Laboratory II
Lab Assignment 9 Suggested Solutions
• Full Mark: 40
1. In this question, we will use MATLAB to perform numerical differentiation.
Consider the function
f (x) = ex sin(x2 ). (1)
(a) (2 marks) Derive and write down the expression of f ′ (x) in the box below
(no MATLAB involved).
Solution: We have f ′ (x) = ex sin(x2 ) + 2xex cos(x2 ).
(b) (6 marks) Write a MATLAB script q1b.m to do the following:
• Construct a vector x with equally spaced values between 0 and 5 using
the step size h = 0.02.
• Construct a vector f representing the values of f (x) based on x and
Eq. (1).
• Using the diff function and the step size h, construct a vector dfdx that
approximates f ′ (x).
• Construct another vector g that represents f ′ (x) based on your derived
expression in part (a).
• Create a MATLAB figure and plot the values of dfdx versus x(1:end-1)
using red solid lines.
• On the same plot, plot the values of g versus x using blue circle markers.
• Label the two sets of values as “Approximated df/dx” and “Derived
df/dx” using legend.
Include the code file q1b.m in your submission.
2
Solution:
h = 0.02;
x = 0:h:5;
f = exp(x).*sin(x.ˆ2);
dfdx = diff(f)/h;
g = exp(x).*sin(x.ˆ2) + 2*x.*exp(x).*cos(x.ˆ2);
figure;
plot(x(1:end-1),dfdx,'r-');
hold on;
plot(x,g,'bo');
legend('Approximated df/dx','Derived df/dx');
The figure is as follows:
3
2. In this question, we will use MATLAB to perform numerical integration.
(a) (6 marks) Consider the integral
Z 1
ex sin(cos(x)) dx.
0
Write a MATLAB script q2a.m to do the following:
• Consider 100 equally spaced points X in [0,1] and store the values of the
integrand ex sin(cos(x)) at these points as a variable Y. Then, perform
numerical integration using trapz(X,Y). Denote the result as I1.
• Create a function handle fun for the integrand ex sin(cos(x)) and perform
numerical integration using the integral function. Denote the result as
I2.
• Display the absolute difference between I1 and I2.
Include the code file q2a.m in your submission.
Solution:
X = linspace(0,1,100);
Y = exp(X).*sin(cos(X));
I1 = trapz(X,Y);
fun = @(x) exp(x).*sin(cos(x));
I2 = integral(fun,0,1);
disp(abs(I1-I2))
The absolute difference is 1.1944e-05.
(b) (4 marks) Consider the triple integral
Z 1 Z 1+x2 Z 1+x+y
ex+y+z sin(cos(xyz)) dz dy dx.
0 1−x2 1−x−y
Write a MATLAB script q2b.m to evaluate the integral using the integral3
function. Also, write down the value of the integral in the box below.
4
Include the code file q2b.m in your submission.
Solution:
f = @(x,y,z) exp(x+y+z).*sin(cos(x.*y.*z));
xmin = 0;
xmax = 1;
ymin = @(x) 1-x.ˆ2;
ymax = @(x) 1+x.ˆ2;
zmin = @(x,y) 1-x-y;
zmax = @(x,y) 1+x+y;
I = integral3(f,xmin,xmax,ymin,ymax,zmin,zmax)
The value is −4.7959.
3. (6 marks) Consider the following system of differential equations involving two
functions y1 (t) and y2 (t):
y ′ (t) = 2y (t) − y (t)y (t),
1 1 1 2
y ′ (t) = y (t)y (t) − 3y (t),
2 1 2 2
with the initial conditions y1 (0) = 10 and y2 (0) = 3.
Write a MATLAB script q3.m to do the following:
• Use [t,y] = ode45(...) to solve the system of differential equations in
the time interval tspan = [0,10].
• Create a MATLAB figure to plot the values of y1 (t) (which are stored in
the first column of y versus t using red solid lines.
• On the same plot, also plot the values of y2 (t) (which are stored in the
second column of y versus t using blue solid lines.
• Label the two curves as y1 (t) and y2 (t).
Include the code file q3.m in your submission.
5
Solution:
odefun = @(t,y) [2*y(1)-y(1)*y(2); y(1)*y(2)-3*y(2)];
[t,y] = ode45(odefun, [0,10], [10;3]);
figure;
plot(t,y(:,1),'r-');
hold on;
plot(t,y(:,2),'b-');
legend('y 1(t)','y 2(t)')
The figure is as follows:
Remark: This system of differential equations is an example of the Lotka–
Volterra equations, also known as the Lotka–Volterra predator–prey model,
which are frequently used in the modelling of the dynamics of biological systems.
6
4. In this question, we will use MATLAB to solve different optimization problems.
(a) (4 marks) Write a MATLAB script q4a.m to find a local minimum of the
following function in the interval (0.5, 2):
f (x) = e−x sin x − x2 cos x.
Also, display the optimal solution x and the minimum value of f .
Include the code file q4a.m in your submission.
Solution:
f = @(x) exp(-x).*sin(x) - x.ˆ2.*cos(x);
[x,fval] = fminbnd(f,0.5,2)
We have x = 1.1195 and f (x) = −0.2528.
(b) (6 marks) Write a MATLAB script q4b.m to find a local maximum of the
following multivariable function using the initial guess (x0 , y0 ) = (1, 1):
f (x1 , x2 ) = 3 sin(x1 − 1) − 2 cos x2 + esin(x1 x2 ) .
Also, display the optimal solution (x1 , x2 ) and the maximum value of f .
Include the code file q4b.m in your submission.
Hint: Consider solving the minimization problem for the function g = −f .
Solution:
f = @(x) 3*sin(x(1)-1) - 2*cos(x(2)) + exp(sin(x(1)*x(2)));
g = @(x) -f(x);
x0 = [1,1]; % initial guess
[x,gval] = fminunc(g,x0);
fval = -gval;
disp([x,fval])
We have (x1 , x2 ) = (2.5372, 3.1003) and f (x1 , x2 ) = 7.7147.
7
(c) (6 marks) Write a MATLAB script q4c.m to solve the following constrained
optimization problem:
minimize f (x1 , x2 , x3 ) = (sin x1 )(cos x2 )x3 − sin(x2 x3 )
subject to
x1 + x2 + x3 ≤ 3
2x + x3 = 5
1
−5 ≤ x1 ≤ 5
−4 ≤ x2 ≤ 4
−2 ≤ x
3
For the initial guess, use (x1 , x2 , x3 ) = (0, 0, 0).
Also, display the optimal solution (x1 , x2 , x3 ) and the minimum value of f .
Include the code file q4c.m in your submission.
Solution:
fun = @(x) sin(x(1))*cos(x(2))*x(3) -sin(x(2)*x(3));
x0 = [0,0,0];
A = [1,1,1];
b = [3];
Aeq = [2,0,1];
beq = 5;
lb = [-5,-4,-2];
ub = [5,4,Inf];
[x,fval] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
We have (x1 , x2 , x3 ) = (3.0572, −1.3360, −1.1143) and f (x) = −1.0185.
End of Lab Assignment