Vous êtes sur la page 1sur 3
AMATH 301 Final Exam, Spring 2010, no notes or calculators MATLAB COMMANDS YOU MAY NEED ones: ONES(n,1) creates an nx1 column vector of all ones, diag: DIAG(B,0) puts a column vector of length(B) on the main diagonal of a matrix of size length(B) by length(B). DIAG(B.K), positive, puts a column vector of length(B) on the k-th superdiagonal of an (length(B)+k) by (length(B)+K) matrix. DIAG(B,k), k positive, puts a column vector of length(B) on the k-th subdiagonal of an (Iength(B)+k) by (length(B)+) matrix quad: QUAD - Numerically evaluate integral, adaptive Simpson quadrature q = quad(fun,a,b) Quadrature is a numerical method used to find the area under the graph of a function, that is, to compute a definite integral. q = quad(fun,a,b) tries to approximate the integral of function fun from ‘a to b to within an error of Je-6 using recursive adaptive Simpson quadrature. Limits a and b must be finite. The function y = fiun(x) should accept a vector argument x and return a vector result y, the integrand evaluated at each element of x. cumtrapz: CUMTRAPZ - Cumulative trapezoidal numerical integration = cumtrap2(Y) Z = cumtrape(X,Y) Z = cumtrap2(¥) computes an approximation of the cumulative integral of Y via the trapezoidal method with unit spacing. odeX¥Z: ode23, oded5, odel13, odel5s, ode23s, odle23t, ode23tb - Solve initial value problems for ordinary diferential equations (T,¥] = solver(odefun,tspan,y0,options) ‘where solver is one of ode45, ode23, odel13, odes, ode2Ss, ode2St, or ode2Stb, Name (LAST, FIRST) hee , Ke 2. Consider the following one-step methods for solving the initial value problem y’ = f(t,y), y(0) = 0, where y and f can be vectors if we have multiple ODEs. os Forward Euler: yosi = ya + fn yn doe Rise) Backward Buler: yo =n + hfott Heun: tats = Ya + 31 (tnt) + f(tnsivtn + AE (tm ¥n))] ‘Trapezoid: ner = Yn + (f(b ta) + fltnastmes)) “by gy! 4 (a) (+5) C ) W: Applying the Forvard and Backward Euler methods to the test problem vy =\ayfives [1 + hd] <1 and [1 hd] > 1 for the stability restrictions on hd, for the two methods, respectively. 1/009) © (hing hn a nn st gon ct i wg the exact utGwer tothe ODE at the grid points eee 2)-Ch 2) (2) (38)-() ) (48) 1 problem ( %4 ) = : =(1), me ZO o8) © pets Probl (¢ 0-100) \v ) \ yo) i)" Forward Euler method will be stable if we choose fh = }. Kea) (45) © w) All h > 0 will ensure the Trapezoid method is stable for test problems with Q0 deo, 6) (+5C)W: Heun's method is explicit and requires 3 evaluations ofthe function f each stepAodé two of those evaluations are dential (8) (+10) Consider the initial value problem y/ H(4a)ow-(S)e(4) 0 The following Matlab code takes 5 steps of Heun’s method to advance the solution from ¢ = 0 to t= 5h. Complete the code by writing out the Matlab statement that defines p. The code saves the y values each step, so at the end y is a 2 x 6 matrix which is stored in A2.dat. \y +5, where yOr(1;-2); Ae[-1 150-20; he.ty yC:A)=y0; 5 Ye ot ys 41) YG 4) C0/2) (Ray (5) + Dat G-1) 50] # Amp + Des 50]) end save A2.dat y ~aseid Yow asd: x) 4 0h LOY Wace er) = 6x) = 3 LEY 4. Consider, again, the 2nd order ODE, y+ 5y! + 6y = 0, with boundary conditions y(0) = 2 and wl) =1 Q (a) (+10) Discretize the ODE with 2nd order centered differences for y" and y/ and write out ZE= explicitly the 4 x 4 matrix system you would solve to find the four interior y values if the TO spocing in z were A= 2 tote uf Yorn) © YON) -2yl8 s{yosy —aeS) bya) = eee « yorrd\s yoo?) = yas) # YOR yosd Lay PAWN NOY “VON OHS ey wn k Yor 7 ye Ce%S | | 14 \ af we oe Nae yao ZN By Ke N ft 7 Bemy pe pye . UMOy Ys \yowre ve yale 2 - yale eV TE aH Vector bb. (©) (410) Bach equation above ean be scaled so the element on the diagonal of the matric in ‘each row is 2—6«h?. By doing so, complete the Matlab code below that sets up and solves this tridiagonal system of equations. - Main = (2-64h"2)+ones (451); Rh sw Mx mes Gil? sup= 7 xonec3,i). A= diag(Wain,0) + diag(Sub,-1) + diag(Sup,1); yeA\Rhs; Save Al.dat (2;y;1]; saving the boundary conditions as well

Vous aimerez peut-être aussi