You are on page 1of 2

Jacobs University Bremen School of Engineering and Science Peter Oswald

Spring Term 2012

NatSciLab Numerical Software Problem Set 1 Issued: 19.04.2012 Due: Thursday 26.04.2012, 1pm

The edited diary le with the solution to the rst problem (name it YourNameWeek1.txt) needs to be submitted during the lab by Friday, April 20, 5pm, the rest by next Thursday, April 26, 1pm to a.ghazi@jacobs-university.de, with copy to the IoR. 1.1. a) Try the following commands, and insert comment lines into the diary to describe their eect: x=-1:0.3:3.5 y=linspace(-3,3,13) n=15;s1=sum(1:n);s3=sum((1:n).^3);s3-s1^2 [X,Y]=meshgrid(-1:0.3:1,y) x([end 1 2.4]) x=[x(1:2:end);x(2:2:end)]-reshape(x,2,prod(size(x))/2) A=pascal(5) diag(flipud(A)) A=diag(ones(1,3),-2) A=repmat(rand(3,1),1,2) A=5*randn(4)-2 [B,I]=max(A) min(A(1:end))-min(min(A)) find(A<0) A(find(A<0)) A<0 A(A<0) A(A>=0)=0 sqrt(-3:3) b) Use array operations to compute the general term of the sequence
n

sn =
k=0

(1)k (2k + 1)3

for several n of your choice (up to n = 106 should be possible). Then demonstrate numerically that sn 3 /32 as n . Use a plot to visualize this investigation. c) Any nite sequence of points {P0 , P1 , P2 , . . . , PN = P0 } in the plane denes an N -gon (just connect Pi1 with Pi by a straight line, i = 1, . . . , N ). Write a function that computes the area of the polygon (assuming that the edges do not intersect). To check, compare with Matlabs polyarea. Hint: Any polygon can be decomposed into triangles. 1.2. a) The quadratic equation x2 + 2px + q = 0 has the two roots x1/2 = p p2 q. Implement this formula, and apply it to the equation x2 + 1012 x + 1 = 0 (output the two roots as a 2 1 vector). Then check the quality of your computation by computing the product of the two roots (by Vietas formula, the result should be 1). What is the reason for the dierences? Can you propose a remedy (write a function that? Does Matlabs roots have the same deciency?

b) Another famous instability is inherent to the root-nding problem for polynomials (see Wilkinson polynomial). It does not come so much from the algorithm used for root nding but reects the fact that root-nding for polynomials of higher degree is in general an ill-conditioned problem (see Section 3.1 of the script for more information on condition and stability). Execute x=roots(poly(1:20))-(20:-1:1) x=roots(poly(1:20)+1.e-10*rand(1,21))-(20:-1:1) explain what these commands do, and interpret the observed results. 1.3. One of the important special functions is the error function used heavily in probability and statistics: x 2 (1)k 2 2 erf(x) = et dt = x2k+1 , x 0. k!(2k + 1) 0
k=0

a) Compute erf(x) using its Taylor polynomial of degree n = 400. To this end, write a function with input x (a vector of arguments) and n (the degree of the Taylor polynomial), this will come in handy when solving a problem in HW2. b) Find out (by using the built-in function erf for comparison) for which range of values x 0 your code is trustworthy (note that a theorem on alternating series tells you that T400 (x) should be equal to the limit erf(x) within double precision for at least x [0, 15]). Explain your observations. 1.4. Vectors and matrices can be considered as discrete functions with arguments from an equidistant grid with mesh-size h in one or two dimensions, respectively. Many notions from calculus carry over to this discrete setting. E.g., using the Matlab command diff you could try to dene (partial) derivatives, check for jumps, monotonicity, convexity, local minima/maxima etc.. In the following programming exercise, the matrix H represents a discrete height eld hij = h(xi , yj ) representing a discretized landscape, and we want to identify peaks (local maxima), gullies (local minima), and mountain passes (saddle points). You may assume that the height eld is highly accurate. a) Write a Matlab function [P,G,MP]=CriticalPoints(H,c) with a matrix H and constant c > 0 as input, and output arrays P, G, M P containing information about the set of indices (i, j), where you suspect peaks, gullies, and mountain passes, respectively. Here is a hint how to nd them: On a continuous level (assuming continuous dierentiability), all three types of points are points where the gradient, and thus both rst order partial derivatives, are zero. To nd these points from the discrete data, it is advisable to look for sign changes in the rows of diff(H) and columns of diff(H). b) Apply your function CriticalPoints some simple test functions z = h(x, y) of your choosing, and to h(x, y) = sin(x 4y) + cos(((x + y)2 1)2 /20), 2 x, y 2. Use meshgrid to generate an equidistant grid {(xi , yj )} of suciently small mesh-size lling the square [2, 2]2 , then nd H by evaluating h on this grid using array operations. Visualize the result, e.g., by plotting the dierent types of critical points by dierent color in a square using plot. For plotting the graph of z = h(x, y) you can use the ezsurf or ezmesh command. Comment on your observations from the tests.

You might also like