Week 4: Numerical Methods and 3D Plotting
Today’s Question
When exact answers are unavailable, how do we compute responsibly?
Week 4 Plan
- Meeting 1: roots, integrals, and checks
- Meeting 2: surfaces, contours, and AI claims
Approximation Is A Claim
Numerical answers depend on:
- method;
- initial guess;
- tolerance;
- step size;
- conditioning.
Meeting 1 Question
What evidence makes a numerical answer trustworthy?
Root-Finding Pattern
f = @(x) x.*exp(-x) - 0.2;
x = linspace(0, 6, 300);
plot(x, f(x))
root = fzero(f, [0 1]);
residual = f(root);
Why Plot First?
The plot can reveal:
- no crossing;
- more than one crossing;
- a bad initial guess;
- a numerical answer that needs qualification.
One Root Or All Roots?
left_root = fzero(f, [0 1]);
right_root = fzero(f, [1 5]);
One solver call is not a proof that there is only one root.
Visual Check
Plot before solving. Plot again near the computed answer.
Substitute the answer back into the original function.
Numerical Integration
g = @(x) sin(x)./(1 + x.^2);
area = integral(g, 0, 6);
The answer depends on the method and the function behavior.
Data-Based Integration
x = linspace(0, 6, 13);
y = g(x);
area_data = trapz(x, y);
trapz is useful when the function is known only through sampled data.
Comparison Habit
Ask:
- What input did the method receive?
- What spacing or tolerance was used?
- Does a finer grid change the answer?
Optimization Pattern
h = @(x) (x - 2).^2 + 0.5*sin(5*x);
[xmin, hmin] = fminbnd(h, 0, 4);
fminbnd reports a local minimum on the interval you gave it.
ODE Initial-Value Problem
Write the problem as:
The solver needs the derivative rule, interval, and initial value.
ODE Solver Pattern
f = @(t,y) -y + sin(t);
tspan = [0 10];
y0 = 1;
[t, y] = ode45(f, tspan, y0);
plot(t, y)
ode45 is the first solver to try for many nonstiff problems.
ODE Check
initial_error = abs(y(1) - y0);
For every ODE solve, check that the reported solution starts where the problem says it should.
Meeting 2 Question
What does a 3D plot reveal, and what can it hide?
3D Line Plot
t = linspace(0, 6*pi, 400);
x = sqrt(t).*sin(2*t);
y = sqrt(t).*cos(2*t);
z = 0.5*t;
plot3(x, y, z)
The three coordinate vectors must have compatible sizes.
Surface Workflow
- Create a grid in the
x,y plane.
- Evaluate
z = f(x,y) on the grid.
- Plot the surface or contours.
Meshgrid Pattern
x = linspace(-3, 3, 100);
y = linspace(-3, 3, 100);
[X, Y] = meshgrid(x, y);
Z = sin(X).*cos(Y);
Elementwise operations matter.
Surface Plot
surf(X, Y, Z)
shading interp
colorbar
xlabel("x")
ylabel("y")
zlabel("z")
Use the plot to answer a question.
Contour Plot
contourf(X, Y, Z, 20)
colorbar
axis equal tight
Contours are often easier to read than a rotated 3D surface.
AI Check
Substitute any generated numerical answer back into the original problem.
Then ask:
- Did it find all relevant roots?
- Did it state the interval or initial guess?
- Did it check the residual?
- Did it explain the numerical method?
Plausible But Weak AI Code
f = @(x) x.^3 - 6*x.^2 + 11*x - 6;
root = fzero(f, 0);
fprintf("The solution is %.4f\n", root)
What is missing?
Exit Ticket
A numerical answer is trustworthy enough to report when …