math 365, spring 2015name : homework project #5 math 365, spring 2015 due april 15th 1.(piecewise...

6
Name : Homework Project #5 Math 365, Spring 2015 Due April 15th 1. (Piecewise Cubic Hermite Interpolating Polynomial) Write a function that evaluates the piece- wise Hermite interpolating polynomial. You will pass into your function an array of values for your independent variable (’xdata’), an array of values for the dependent variable (’ydata’), and an array containing the x locations where you would like to evaluate your polynomial (’x’). Then, optionally, you will pass in a fourth argument which will be derivative values, if you have them. Your function should return the value of the interpolating polynomial. Your function will take one of two forms. If you supply derivative values, you will call your function like this: y = my_pchip(xdata,ydata,x,ypdata); where ypdata are the derivatives (or slopes) that you will use at each data point. If you do not have derivatives, you will let my pchip compute the derivatives for you. In this case, you will only pass in three arguments. y = my_pchip(xdata,ydata,x); To test whether you have 3 or 4 input arguments, use the Matlab command nargin. If you do not have derivatives available, you can approximate them using the following idea. Let h k = x k+1 - x k , and let δ k =(y k+1 - y k )/h k . Then let d k be an approximation to the slope at the point x k (i.e. f 0 (x k )). At each interior point k =2, 3, ...N - 1, compute a slope d k as follows. (a) If δ k-1 and δ k have different signs, set d k = 0. (b) If δ k-1 and δ k have the same sign, then w 1 + w 2 d k = w 1 δ k-1 + w 2 δ k where w 1 =2h k + h k-1 and w 2 = h k +2h k-1 Note that if h k = h k-1 for all k, the formula simplifies to 2 d k = 1 δ k-1 + 1 δ k You can set the derivatives at the end points to d 1 = δ 1 and d N = δ N-1 . For more details, see the chapter “Interpolation”, in the course text Numerical Computing with Matlab, or see the course website for lecture slides on Piecewise Polynomial interpolation. Use your function to solve the following problems. (a) Using known derivatives. If you are given an analytic expression for a function f (x) that you wish to interpolate, you can use the derivatives of this function directly. Interpolate the function f (x)= e -5x 2 sin(4) at 11 equally spaced points over the interval [-1, 1]. 1

Upload: others

Post on 12-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Math 365, Spring 2015Name : Homework Project #5 Math 365, Spring 2015 Due April 15th 1.(Piecewise Cubic Hermite Interpolating Polynomial) Write a function that evaluates the piece-wise

Name :

Homework Project #5Math 365, Spring 2015

Due April 15th

1. (Piecewise Cubic Hermite Interpolating Polynomial) Write a function that evaluates the piece-wise Hermite interpolating polynomial. You will pass into your function an array of values for yourindependent variable (’xdata’), an array of values for the dependent variable (’ydata’), and an arraycontaining the x locations where you would like to evaluate your polynomial (’x’). Then, optionally,you will pass in a fourth argument which will be derivative values, if you have them. Your functionshould return the value of the interpolating polynomial.

Your function will take one of two forms. If you supply derivative values, you will call your functionlike this:

y = my_pchip(xdata,ydata,x,ypdata);

where ypdata are the derivatives (or slopes) that you will use at each data point. If you do not havederivatives, you will let my pchip compute the derivatives for you. In this case, you will only pass inthree arguments.

y = my_pchip(xdata,ydata,x);

To test whether you have 3 or 4 input arguments, use the Matlab command nargin.

If you do not have derivatives available, you can approximate them using the following idea. Lethk = xk+1 − xk, and let δk = (yk+1 − yk)/hk. Then let dk be an approximation to the slope at thepoint xk (i.e. f ′(xk)). At each interior point k = 2, 3, ...N − 1, compute a slope dk as follows.

(a) If δk−1 and δk have different signs, set dk = 0.

(b) If δk−1 and δk have the same sign, then

w1 + w2

dk=

w1

δk−1+w2

δk

wherew1 = 2hk + hk−1 and w2 = hk + 2hk−1

Note that if hk = hk−1 for all k, the formula simplifies to

2

dk=

1

δk−1+

1

δk

You can set the derivatives at the end points to d1 = δ1 and dN = δN−1. For more details, see thechapter “Interpolation”, in the course text Numerical Computing with Matlab, or see the course websitefor lecture slides on Piecewise Polynomial interpolation.

Use your function to solve the following problems.

(a) Using known derivatives. If you are given an analytic expression for a function f(x) that youwish to interpolate, you can use the derivatives of this function directly. Interpolate the functionf(x) = e−5x

2

sin(4xπ) at 11 equally spaced points over the interval [−1, 1].

1

Page 2: Math 365, Spring 2015Name : Homework Project #5 Math 365, Spring 2015 Due April 15th 1.(Piecewise Cubic Hermite Interpolating Polynomial) Write a function that evaluates the piece-wise

i. Use your function my chip to approximate this function over a range [−1, 1]. Plot the resultsof your interpolation.

ii. On the same graph, plot the exact function f(x). Add a legend, title and axes labels to yourgraph.

(b) Derivatives are not known. Use your interpolating polynomial to interpolate the data insnow.dat, available on the course website. The first column of this file is a measurement of snowdepth (cm), and second column is the percentage of liquid water content p, 0 < p < 100 found atthat depth. Plot your interpolating polynomial and include the following

• Include your data points in your plot (use markersize).

• Plot the snow depth on the y-axis so that zero depth is at the top of the plot, and 200 cm isat the bottom. Hint: Use set(gca,’ydir’,’reverse’).

• Plot the water content along the x-axis.

• Add a title and axis labels.

• Use your interpolation routine to approximate the snow water content at a depth of 106.3centimeters. Write your results to the file watercontent.out (using write file).

• Why is this particular choice of derivatives particularly advantageous for this data set?

(Thanks to Santiago Rodriguez, Department of Geophysics (BSU) and former Math 365 student, forthe snow data.)

2. (Roller Coaster!) For this problem, you will use the spline function to construct a closed-loop rollercoaster track. On this track, you’ll plot vectors indicating the strength of the centrifugal force as yougo around the track.

To construct the track, download the file rollercoaster.dat. This file contains a list of points,(t, x(t), y(t), z(t)) for the parameterized curve describing the track.

Then do the following.

(a) Plot a closed curve through these points using the spline function and the plot3 function. Thiscurve should resemble a roller-coaster.

(b) Assume that the cars on this roller coaster are attached to a cable on the tracks, and this cablepulls the cars around the roller coaster track at constant speed S = 60. Also, assume that the carsare always oriented outwards in the direction of the normal to the curve. Sitting in the car, youwon’t experience any acceleration in the direction tangent to the tracks, but you will experiencecentrifugal force as you bend around the curves. This acceleration can be described as

a(α) = S2κ(α)N(α)

where α is the parameter you have used to model the spline, κ(α) is the curvature of the track,and N(α) is a vector perpendicular to the direction of travel.

Plot acceleration vectors on your roller coaster at several points on the ride. The plot in Figure 1shows 50 vectors per polynomial segment. These vectors illustrate how the acceleration is largerin regions with tight curvature. Use the cubic spline you found, and notes on the course websiteto compute the normal N(α) and the curvature κ(α). Scale your acceleration vectors by gravityg = 9.81 so they plot nicely.

(c) (Extra credit - 10 points) You will notice that your acceleration vectors are not continuous atthe point where the first and last data point meet. Figure out a way to get at least continuousfirst and second derivatives at this point. Hint: One way to do this is to use the function csape

from the curve-fitting toolbox. This function gives you many more options for end-conditions(including periodic end point conditions) than does the spline function. A second way to dothis is to implement the cubic spline, along with periodic boundary conditions as we discussed inclass.

2

Page 3: Math 365, Spring 2015Name : Homework Project #5 Math 365, Spring 2015 Due April 15th 1.(Piecewise Cubic Hermite Interpolating Polynomial) Write a function that evaluates the piece-wise

Figure 1: Roller Coaster created from a spline. Red and blue normal vectors show the acceleration aroundthe curves. Blue vectors exceeded a maximum G-force of 3, while green vectors have a G-force less than 1.The roller coaster is traveling at 50 meters per second.

For this problem, you will need the functions spline, norm, polyder, polyval, cross, mkpp and ppval.

For hints on how to do this problem, see the tutorial on the course website on “Splines and the geometryof curves”. See Figure 1 for an example of what your final plot should look like.

3. (Newton’s method) The almost universally used algorithm to compute√a, where a > 0, is the

recursion

xn+1 =1

2

(xn +

a

xn

), (1)

easily obtained by means of Newton’s method for the function f(x) = x2 − a. One potential problemwith this method is that it requires a floating point division, which not all computer processors support,or which may too expensive for a particular application.

For these reasons, it is advantageous to devise a method for computing the square root that does notrequire any floating point divisions, (except division by 2, which can be easily done by shifting thebinary representation one bit to the right), but only addition, substraction and multiplication. Thetrick for doing this is to use Newton’s method to compute 1√

a, and then obtain

√a by multiplying by

a.

Your goal is to write an algorithm for computing the square root using this trick above. Division by 2is allowed, but no other floating point division. Your algorithm should work for any input value a > 0.

• Write your algorithm as a function so you can call it any starting value.

function y = squareroot(x)

% Your algorithm goes here

end

Set the tolerance for the relative error in your solution to τ = 10−12. The relative error can becomputed as

ek =|xk − xk+1|

xk.

3

Page 4: Math 365, Spring 2015Name : Homework Project #5 Math 365, Spring 2015 Due April 15th 1.(Piecewise Cubic Hermite Interpolating Polynomial) Write a function that evaluates the piece-wise

where xk and xk+1 are successive iterates in your Newton iteration.

• Use your function to compute√

35,√

2.3× 10−6, and√

1715. Write out your three solutions to afile squareroot.out.

To see where this sort of software assisted accelaration is used in gaming, see the course webpage fora link to the article : Origin of Quake3’s Fast InvSqrt().

4. (Julia sets and Newton fractals) You may have seen the beautiful pictures that are produced byfractals. These are typically produced by iterating on values in the complex plane and then coloringthese values according to how long they take to diverge to infinity, which value they converge to, or someother coloring scheme. In this project, you will investigate two different types of fractal patterns, theJulia sets associated with rational functions and Newton Fractals, associated with Newton’s Method.

Julia sets. One type of fractals, called Julia sets (named after the French mathematician GastonJulia), are computed by generating a sequence of iterates zk

zk+1 = f(zk) (2)

where zk = xk + iyk is a complex number and f(z) is a rational function. The “Julia set” is the setof values z0 for which the iteration above generates values that do not diverge to infinity. A typicalJulia set for the function f(z) = z2 + c, for a complex number c is shown in Figure 2. In this figure,the black points are those points that do not “escape” after some maximum number of iterations. Thecolored points are those starting values that “escape” after some number of iterations. The colors areassigned according to how long it takes to escape. For example, a coloring algorithm for this fractalmight look like :

k_escape = Mmax;

z = z0; % Point in the complex plane

escape_value = 2; % Typical value

for k = 1:Mmax

z = f(z);

if (abs(z) > escape_value)

k_escape = k;

break;

end

end

% if k_escape == Mmax, color the point black.

% else, assign a color based on value of k_escape.

Newton Fractals. Other interesting patterns can be generated by considering the Newton iterationfor the function g(z) = 0. For example, if we let g(z) = z3+1, then the corresponding Newton iterationis given by

f(z) =1

3

(2z − 1

z2

)It is easy to show that if f(z) = z, then g(z) = 0. The equation g(z) = z3 + 1 has three roots: one realroot, and two complex conjugate roots. These roots are given by

r1 = −1, r2 = exp

(iπ

3

)= cos

(π3

)+ i sin

(π3

), r3 = exp

(− iπ

3

)= cos

(π3

)− i sin

(π3

),

where i =√−1 and exp(iπ) = −1. If we start with an arbitrary complex number z = x + iy as a

starting value for Newton’s Method, the iterates that Newton’s Method produces will converge to one

4

Page 5: Math 365, Spring 2015Name : Homework Project #5 Math 365, Spring 2015 Due April 15th 1.(Piecewise Cubic Hermite Interpolating Polynomial) Write a function that evaluates the piece-wise

Figure 2: Julia set produced by f(z) = z2 + 0.3 + 0.5423i. The black region represents those starting valueswhich do not escape to infinity.

of the three roots of g(z), or fail to converge. To generate the Newton Fractal, we color each startingvalue in the complex plane according to which root it converges to. The boundary between regionsof different colors form the Julia set and is colored black. See Figure 3 for an example of a NewtonFractal.

For this assignment, you can choose to create a fractal like that in Figure 2 or in Figure 3. In eithercase, your code should be as efficient as possible, and generate a fractal with reasonable resolution inunder a minute. Also, include a second image showing a zoomed in portion of your fractal.

This is a very open ended problem, and designed to encourage your to explore many Matlab functionsyou may not have yet had a chance to learn about. For this, you find the functions colormap, meshgrid,complex among others useful functions. The code julia plot, available on the course website, shouldserve as a useful starting point.

Extra credit - 5 points. Create both Julia set fractals and Newton Fractal.

5

Page 6: Math 365, Spring 2015Name : Homework Project #5 Math 365, Spring 2015 Due April 15th 1.(Piecewise Cubic Hermite Interpolating Polynomial) Write a function that evaluates the piece-wise

Figure 3: The Newton fractal set produced by finding the roots of g(z) = z3 + 1 using Newton’s Method.The black dots are the three roots in the complex plane, and the colored regions correspond to initial startingvalues for Newton’s Method which converge to a given root. The boundary between colored regions formsthe Julia set this region. The surprising fact is that each point in the Julia set touches all three coloredregions!

6