

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Assignment; Class: Numerical Analysis; Subject: Mathematics; University: University of California - Berkeley; Term: Summer 2009;
Typology: Assignments
1 / 2
This page cannot be seen from the preview
Don't miss anything!


(1) Let’s change the notation for Newton’s method so it doesn’t conflict with the notation for ODEs: Newton’s method finds a root of g(x) using the iteration pj+1 = pj − (^) gg′((ppjj^ ) ). The equation for wi+1 is equivalent to g(x) = 0, where g(x) =
wi + h 2 [f (ti+1, x) + f (ti, wi)]
− x. To use Newton’s method, we need this function’s derivative: g′(x) = h 2 ∂f∂y (ti+1, x) − 1. Write down:
p 0 = wi
pj+1 = pj −
wi + h 2 [f (ti+1, pj ) + f (ti, wi)] − pj h 2
∂f ∂y (ti+1, pj^ )^ −^1 (2) In implicit_trap.m: function [ti,wi]=implicit_trap(f,dfdy,a,b,y0,N,tol,max_iterations) h = (b-a)/N; ti = linspace(a, b, N+1); wi(1) = y0; for i=1:N % Set up a root-finding problem, g(w(i+1)) = 0. g = @(x) -x + wi(i) + h/2 * ( f (ti(i+1),x) + f(ti(i),wi(i))); dg = @(x) -1 + h/2 * (dfdy(ti(i+1),x) ); wi(i+1) = newton(g, dg, wi(i), tol, max_iterations); end (3) f = @(t,y) y.(10-y); dfdy = @(t,y) 10-2y; y0 = 2; tExact = linspace(0, 4); wExact = 10 ./ (1 + 4exp(-10tExact)); [tEuler, wEuler] = euler(f, 0, 4, y0, 44); [tITrap, wITrap] = implicit_trap(f, dfdy, 0, 4, y0, 44, 1e-4, 20); plot(tExact, wExact, tEuler, wEuler, tITrap, wITrap) legend(’Exact’, ’Euler’, ’ITrap’,0)
Figure 1. Plot for problem 3 (MATLAB)
(^20) 0.5 1 1.5 2 2.5 3 3.5 4
3
4
5
6
7
8
9
10
11
12
Exact Euler ITrap
Date: Wednesday 8/12. 1
(4) Since the values of h are 1/ 2 i+2, 1 ≤ i ≤ 4:
for i=1: h(i) = 1/2^(i+2); [t, w] = implicit_trap(f, dfdy, 0, 1, y0, 1/h(i), 1e-4, 20); err(i) = abs( w(end) - 10 ./ (1 + 4exp(-101)) ); fprintf(’h = 1/%2d: Absolute error = %7.5e\n’, 2^(i+2), err(i)) end Output is h = 1/ 8: Absolute error = 1.06856e- h = 1/16: Absolute error = 2.98302e- h = 1/32: Absolute error = 7.62858e- h = 1/64: Absolute error = 1.91750e- The line “loglog(h,err,’-o’)” produces figure 2. We need to estimate its slope somehow. Exam- ples:
format long polyfit(log(h), log(err), 1) % Slope of best-fit line
ans =
1.936816828682484 -2.
( log(err(4))-log(err(3)) ) / ( log(h(4))-log(h(3)) ) % Slope between 2 points
ans =
... or even an “eyeball” estimate from the graph. Any estimate should indicate an order near O(h^2 ).
Figure 2. Log-log plot for problem 4 (MATLAB)
10 −2^10 −1^100
10 −
10 −
10 −
10 −
2