Week 4 Exercises
These exercises practice numerical root-finding, integration, 3D plotting, and validation. Use AI only after you have written a first attempt yourself.
Exercise 1: Put An Equation In Root Form
Rewrite each equation in the form f(x) = 0.
cos(x) = xx^2 = 5exp(-x) = 0.1*xsin(x) + x/4 = 1
For one of them, define f as an anonymous MATLAB function.
Exercise 2: Find And Check A Root
For:
f = @(x) cos(x) - x;Write MATLAB code to:
- Plot
fon[0, 2]. - Use
fzeroto find a root. - Compute
f(root). - Report the root and residual.
Why is the residual important?
Exercise 3: Multiple Roots
For:
f = @(x) x.^3 - 6*x.^2 + 11*x - 6;- Plot the function on
[0, 4]. - Use brackets to find the three roots.
- Compute the residual at each root.
- Explain why one call to
fzerois not enough.
Exercise 4: Compare Integrals
For:
g = @(x) sin(x)./(1 + x.^2);Compute the integral from 0 to 6 using:
integral(g, 0, 6);trapzwith 13 equally spaced points;trapzwith 201 equally spaced points.
Which estimates are closest? Why?
Exercise 5: Solve A First-Order ODE
For:
y' = -y + sin(t), y(0) = 1
Write MATLAB code that:
- defines the right-hand side with
f = @(t,y) ...; - solves on
[0, 10]withode45; - plots
yagainstt; - checks that the first computed value matches the initial condition;
- reports the final value
y(end).
Which parts of your code represent the differential equation, the time interval, and the initial condition?
Exercise 6: Local Optimization
For:
h = @(x) (x - 2).^2 + 0.5*sin(5*x);Use fplot to inspect the function on [0, 4], then use fminbnd.
Explain why the result should be called a local minimum on the chosen interval, not automatically a global fact about the function.
Exercise 7: Build A Surface
Create a surface plot for:
z = x*exp(-x^2 - y^2)
on -2 <= x <= 2 and -2 <= y <= 2.
Your code should use:
linspace;meshgrid;- elementwise operations;
surf;- axis labels and a colorbar.
Then make a filled contour plot of the same surface.
Exercise 8: Critique Generated Code
An LLM produced:
f = @(x) x.^3 - 6*x.^2 + 11*x - 6;
root = fzero(f, 0);
disp(root)Find at least four issues or missing checks. Your answer should mention:
- plotting;
- initial guess or bracket;
- multiple roots;
- residual checks;
- whether the phrase “the root” would be justified.
Exit Ticket
Write 3-5 sentences answering:
What makes a numerical answer trustworthy enough to report?