KTH Matematik Tentamen del 2 SF1511, 2018-03-16, kl 8.00-11.00, Numeriska metoder och grundläggande programmering Del 2, Max 50p + bonuspoäng (max 4p). Rättas ast om del 1 är godkänd. Betygsgränser inkl bonuspoäng: 10p D, 20p C, 30p B, 40p A. Svar skall motiveras och uträkningar redovisas. Korrekt svar utan motivering eller med felaktig motivering medför poängavdrag. Inga hjälpmedel är tillåtna (ej heller miniräknare). P1. Vi vill hitta lösningen (x, y, z) till följande icke-linjära system av ekvationer x 2 2x + y 2 z + 1 0 F (x, y, z) = xy 2 x 3y + yz + 2 = 0. xz 2 3z + yz 2 + xy 0 (8 p) a) Beräkna Jakobian-matrisen till funktionen F (x, y, z) för hand. b) När Newtons metod för system används för att lösa systemet får man felen e k efter iteration k, enligt följande tabell k 1 2 3 e k 0.2 0.04 1.6 10 3 Efter hur många ytterligare iterationer kommer felet bli mindre än 10 5? Motivera ditt svar noga! c) Skriv ett Matlab-program som löser F (x, y, z) = 0 med Newtons metod för system. Svaret ska ha ett fel som är mindre än 10 10 i alla komponenter. En bra startgissing är (x, y, z) = (2, 0, 1). d) Betrakta nu ekvationssystemet x 2 2x + y 2 z + 1 a F (x, y, z) = xy 2 x 3y + yz + 2 = b, xz 2 3z + yz 2 + xy c där a = 0.1 ± 0.1, b = 0.1 ± 0.05 och c = ±0.2. Skriv ett Matlabprogram som uppskattar osäkerheten i lösningens x-komponent. Utgå från programmet i deluppgift (c) och förklara vad som behöver läggas till/modifieras. Programmet skall vara effektivt för full poäng. Solution: a) The Jacobi matrix is given by 2x 2 2y 1 DF (x, y, z) = y 1 2xy 3 + z y. z 2 + y z 2 + x 2xz 3 + 2yz
b) For the errors e 1 and e 2 we observe e 2 e 2 1 = 4 10 2 (2 10 1 ) 2 = 1. and for e 2 and e 3 e 3 e 2 2 = 1.6 10 3 16 10 4 = = 1. (4 10 2 ) 2 16 10 4 Hence, we have a quadratic convergence rate and expect the error after k 1 iterations to be We obtain e k+1 = e 2k 1. e 4 e 23 1 = 2 8 10 8 = 256 10 8 < 10 3 10 8 = 10 5 Hence, after 4 iterations the error is expected to fall below 10 5. c) Matlab program: x = [ 2; 0 ; 1]; % starting values tol = 1e-10; error = 1.0; while ( error > tol ) F = [ x(1).^2-2*x(1) + x(2).^2 - x(3) + 1 ; x(1) * x(2).^2 - x(1) - 3*x(2) + x(2)*x(3) + 2; x(1) * x(3).^2-3*x(3) + x(2)*x(3).^2 + x(1)*x(2) ]; DF = [ 2*x(1) - 2, 2*x(2), -1 ; (x(2)^2-1), ( 2 * x(1) * x(2) - 3 + x(3)), x(2) ; (x(3).^2 + x(2)), (x(3).^2 + x(1)),... (2 * x(1) * x(3) - 3 + 2 * x(2) * x(3)) ]; deltax = DF \ F; x = x - deltax; error = norm( deltax ); d) Olofs solution: Jag börjar med att skriva om koden för Newtons metod till en funktion som för givna värden på a,b och c beräknar lösningen. Detta görs så att högerledet (a, b, c) subtraheras från F. Jakobianen är oförändrad. function wres=stordp3(a,b,c) w=[2;0;1]; korr=w; while norm(korr,inf) > 1.E-10 [F,J]=FoJp3c(w); F=F-[a;b;c]; korr=-j\f; w=w+korr; wres=w;
Därefter använder vi ovanståe rutin för att genomföra störningsräkning map de osäkra storheterna. a=0.1; Ea=0.1; b=-0.1; Eb=0.05; c=0; Ec=0.2; wres0=stordp3(a,b,c); x=wres0(1); wresa=stordp3(a+ea,b,c); wresb=stordp3(a,b+eb,c); wresc=stordp3(a,b,c+ec); Fel=abs(wresa(1)-x)+abs(wresb(1)-x)+abs(wresc(1)-x) Var god vänd
P2. Vi vill beräkna y(t) som satisfierar den ordinära differentialekvationen y + 3y 2e y + t 2 = 0, y(0) = 1, y (0) = 1, y (0) = 1. (8 p) (7 p) (2 p) a. Inför lämpliga beteckningar och skriv ekvationen som ett system av första ordningens differentialekvationer. Glöm inte begynnelsevillkoren! b. Skriv en detaljerad algoritm i Matlab som beräknar y(t) fram till t = 5 och plottar lösningen. Algoritmen ska vara baserad på Framåt Euler-metoden. c. Utöka Matlab-programmet så att det beräknar båglängden L av y(t) i intervallet t [0, 5]. Båglängden ges av L = 5 0 1 + y (t) 2 dt. Matlabs inbyggda funktioner för integration får ej användas här. d. Beskriv hur man kan gå tillväga för att uppskatta tillförlitligheten i approximationen av L. Solution: a) We set u 1 y u := u 2 = y u 3 y and obtain y u = y = y u 2 u 3 3u 2 + 2e u 1 t 2 1 with u(0) = 1. 1
b) Matlab program: T = 5.0; N = 1000; h = T / N; u_n = [ 1 ; -1 ; 1 ]; t_n = [ ]; f = @(t, u ) [ u(2) ; u(3) ; -3 * u(2) + 2 * exp(-u(1)) - t.^2 ]; t_n(1) = 0; for n = 1:N u_n(:,n+1) = u_n(:,n) + h * f( t_n(n), u_n(:,n) ); t_n(n+1) = n*h; plot( t_n, u_n(1,:), b ) hold on plot( t_n, u_n(2,:), r ) hold on plot( t_n, u_n(3,:), g ) hold off c) With L(t) = t 0 1 + y (s) 2 ds introduce the modified system with u 1 y u := u 2 u 3 = y y u 4 L and obtain y u 2 1 u = y y = u 3 3u 2 + 2e u 1 t 2 with u(0) = 1 1. L 1 + u 2 2 0 Matlab program: T = 5.0; N = 1000; h = T / N; u_n = [ 1 ; -1 ; 1 ; 0 ]; t_n = [ ]; f = @(t, u ) [ u(2) ; u(3) ; -3 * u(2) + 2 * exp(-u(1)) - t.^2; sqrt( 1 + u(2)^2 ) ]; t_n(1) = 0; for n = 1:N u_n(:,n+1) = u_n(:,n) + h * f( t_n(n), u_n(:,n) ); t_n(n+1) = n*h; % arc length L(5): u_n(4,)
d.) The accuracy is induced by the accuracy of the forward Euler method. We expect a linear convergence order to exact value of the arc length L(5). One way to guess the accuracy is to study the error between the Euler approximations for different steps sizes. If these errors show a linear reduction with h, we can assume that we are in the asymptotic regime and it approximately holds L(5) L h (5) L(5) L h/2 (5) + L h (5) L h/2 (5) which implies 1 2 L(5) L h(5) + L h (5) L h/2 (5), L(5) L h (5) 2 L h (5) L h/2 (5), where we can use the right hand side as an a-posteriori error estimator. P3. Du vill beräkna derivatan av en snäll funktion f(x). Du känner inte till dess precisa form, men har tillgång till en Matlab-funktion med funktionshuvud function f=func(x) som givet x ger dig f(x). Du har också tillgång till en funktion i Matlab där en numerisk metod för att approximera f (x) finns implementerad. Den har funktionshuvudet: function Dh=numder(x,h) där h är en steglängd och x punkten där man vill approximera derivatan. Till exemepel kan differensapproximationen D(h) = vara implementerad i numder. f(x + h) f(x) h (1) (5 p) (5 p) a. Visa att differensapproximationen i (??) minst har noggrannhetsordning ett. b. Du vill veta vad den numeriska metoden som är implementerad i numder har för noggrannhetsordning. Tyvärr är funktionen numder dåligt kommenterad. Förklara hur du skulle gå till väga för att bestämma noggrannhetsordningen genom numeriska experiment. Motivera ditt svar. c. Skriv en egen version av numder i Matlab som approximerar f (x) med en noggrannhetsordning som är minst två. Du behöver inte bevisa ordningen. Solution: a. Taylor yields f(x + h) = f(x) + hf (x) + O(h 2 ) and hence D(h) f(x + h) f(x) h = O(h).
b. Take a smooth function f where you know the exact derivative and compute q = log( f (x) D(2h) / f (x) D(h) )/ log(2) for various values of h. The closest integer to q is most likely the correct convergence rate. c. Central differences: function D = numder(f, x, h ) D = ( f(x+h) - f(x-h) ) / (2*h);