lab exercise 1 introduction - university of otagojenlow/files/math361_exercises.pdf · lab exercise...

14
Lab Exercise 1 Introduction Due at 11am on Friday the 14th March NAME: This introductory exercise will be marked, and counts for internal assessment. Post hand-written or printed answers in the box on the ground floor of Science III. Always comment your code clearly, and submit evidence that you have completed all parts of each question. Please read Chapter 3 of the handouts thoroughly before attempting this exercise. Q1. To answer this question you may wish to read the manual pages for sqrt, cos, pow and ln (for example, at the command line type man sqrt). Remember to include the math library when compiling, and the math.h header file in your source code. (a) Write a C function with a single parameter ( a double x ). Your function should calculate and print the value of the following expressions: x 2 3 , x, ln(x), and 1 1 + cos(x) , where x is assumed to be in degrees. (b) Write a main routine to test your C function with x = 8 and also with x =0.713994. Q2. Write a function in C which calculates the roots of the quadratic f (x)= ax 2 + bx + c using variables a, b and c, then prints the calculated values to the screen. When calculating the roots, use expressions designed 1 to avoid subtractive cancellation for relatively small a or c. Test your function with the quadratic 4x 2 + 10 17 x + 3. Q3. Write a C program that uses the Standard Iterative Method (see handouts) for the equation x = f (x) where f (x)= e -x + 1 5 sin(20x). Start at x 0 =0.2 and show eleven iterations. Does the method appear to be converging? 1 HINT: One root in the quadratic formula is not prone to cancellation. You can derive a second formula from the quadratic formula by multiplying numerator and denominator by -bb 2 - 4ac. In this formulation the other root is not prone to subtractive cancellation.

Upload: dinhdang

Post on 24-Mar-2018

218 views

Category:

Documents


4 download

TRANSCRIPT

Lab Exercise 1

Introduction

Due at 11am on Friday the 14th March

NAME:

This introductory exercise will be marked, and counts for internal assessment. Posthand-written or printed answers in the box on the ground floor of Science III. Alwayscomment your code clearly, and submit evidence that you have completed all parts ofeach question. Please read Chapter 3 of the handouts thoroughly before attempting thisexercise.

Q1. To answer this question you may wish to read the manual pages for sqrt, cos,pow and ln (for example, at the command line type man sqrt). Remember toinclude the math library when compiling, and the math.h header file in yoursource code.

(a) Write a C function with a single parameter ( a double x ). Your functionshould calculate and print the value of the following expressions:

• x23 ,

• √x,

• ln(x), and

• 1

1 + cos(x), where x is assumed to be in degrees.

(b) Write a main routine to test your C function with x = 8 and also withx = 0.713994.

Q2. Write a function in C which calculates the roots of the quadratic f(x) =ax2 + bx + c using variables a, b and c, then prints the calculated values tothe screen. When calculating the roots, use expressions designed1 to avoidsubtractive cancellation for relatively small a or c. Test your function withthe quadratic 4x2 + 1017x + 3.

Q3. Write a C program that uses the Standard Iterative Method (see handouts)for the equation x = f(x) where

f(x) = e−x +1

5sin(20x).

Start at x0 = 0.2 and show eleven iterations. Does the method appear to beconverging?

1HINT: One root in the quadratic formula is not prone to cancellation. You can derive a secondformula from the quadratic formula by multiplying numerator and denominator by −b∓√b2 − 4ac.In this formulation the other root is not prone to subtractive cancellation.

Lab Exercise 2

Conditioning and Iteration

Due at 11am on Friday the 21st March

NAME:

NOTE: This exercise counts towards your internal assessment. Post hand-written or printed answers in the box on the ground

floor of Science III. Always comment your code clearly, and submit evidence that you have completed all parts of each question.

Q1. The relative condition number, CR, of a function is a measure of the sensitivityof the function to changes in its variables (see handouts, section 2.6).

(a) For a continuous function f(x), show that CR =∣∣∣x [f(x+α)−f(x)]

α f(x)

∣∣∣ , whereα represents a small change in x.

(b) Write C code to generate a table of relative condition numbers for func-tions of the form f(x) = xp. Show CR for integer values of p from 1 to10 in your table, and for each x ∈ {1, 10, 100}. Use α = 0.0001.

(c) Based on the output of your C program, suggest an approximate rela-tionship between CR and p for f(x) = xp.

(d) By considering the limit as α → 0, derive the equation CR =∣∣∣xf ′(x)

f(x)

∣∣∣.(e) Use this equation to confirm the relationship between CR and p.

Q2. Consider the problem of calculating the following integrals:

In ≡∫ 1

0

xn

x + 5dx, n ∈ {0, 1, 2, . . . , 10}.

(a) Evaluate In + 5In−1 by hand.

(b) What behaviour do you expect In to have as n increases?

(c) Using your answer to part (a), write a C program to output I0 throughto I10, starting with I0 = 0.1823.

(d) Comment on the accuracy in the values, and explain why this method isunstable (identifying the source of the increasing error).

(e) Modify your C program to start with I100 = 0 and work backwardscalculating I99, I98, · · · I0. Display the values of I10 down to I0.

(f) Explain why this method is stable.

Q3. In the code directory on the server you will find the file ‘ex2q3.c’. Modify itto apply Newton’s method to f(x) = 4x3 + 8x2 − 51x + 45.

(a) Starting at x0 = 1, how many iterations until x converges to five decimalplaces?

(b) Modify the code to use the rule xi+1 = xi− 2 f(xi)f ′(xi)

. How many iterationsdoes it take to converge to 5 d.p. now?

(c) Briefly explain the observed behaviour, referring to the error analysis onthe back side of this sheet.

Error Analysis of Newton’s Method

Newton’s method uses the basic iterative equation

xi+1 = xi − f(xi)

f ′(xi).

Suppose that f(x) has a nearby root at s, so that f(s) = 0. Define ei = xi−s, then

0 = f(s) = f(xi − ei) = f(xi)− eif′(xi) + e2

i

f ′′(xi)

2+ O(e3

i ),

thus, f(xi) = eif′(xi)− e2

i

f ′′(xi)

2+ O(e3

i ).

Substituting this into Newton’s iterative equation gives

xi+1 = xi −eif

′(xi)− e2i

f ′′(xi)2

+ O(e3i )

f ′(xi)

= xi − ei + e2i

f ′′(xi)

2f ′(xi)+

O(e3i )

f ′(xi)

= s + e2i

f ′′(xi)

2f ′(xi)+

O(e3i )

f ′(xi)(1)

And subtracting s from both sides,

⇒ ei+1 = e2i

f ′′(xi)

2f ′(xi)+

O(e3i )

f ′(xi). (2)

Thus Newton’s method generally has quadratic convergence (the error is approxi-mately squared at each step), provided limi→∞ f ′(xi) 6= 0.

Repeated Roots (of order 2).

If we have a repeated root of order 2 at s, then f ′(s) = 0 and f ′′(s) 6= 0, and

0 = f ′(s) = f ′(xi − ei) = f ′(xi)− eif′′(xi) + O(e2

i ),

so thatf ′(xi) = eif

′′(xi) + . . .

Substitution into Eq 2 gives

ei+1 = e2i

f ′′(xi)

2(eif ′′(xi) + . . .)+

O(e3i )

eif ′′(xi) + . . .=

ei

2+ O(e2

i )

So now we have only linear convergence! A simple modification to the iterativeequation regains quadratic convergence in this case. Consider

xi+1 = xi − 2f(xi)

f ′(xi).

The development of error is as above, but now Eq 1 becomes

xi+1 = s− ei + 2e2i

f ′′(xi)

2f ′(xi)+

O(e3i )

f ′(xi).

Substituting f ′(xi) = eif′′(xi) + . . . then gives

ei+1 = −ei + 2e2i

1

2ei

+ O(e2i ) = O(e2

i ),

and we have regained quadratic convergence. Similarly, for a root of order M , weuse xi+1 = xi −M f(xi)

f ′(xi).

Lab Exercise 3

Root Finding in 1D

Due at 11am on Friday the 28th March

NAME:

Q1. The bisection method is designed to find roots of a function. It solves thefollowing problem:

Given a continuous function f and points a and b, suchthat f(a) and f(b) are of opposite sign, we wish to find avalue of x between a and b where f(x) = 0.

The method is very simple - at each step calculate f(a+b2

). If this is the samesign as f(a) then a ← 1

2(a+ b), otherwise b ← 1

2(a+ b). The algorithm should

stop when f(a+b2

) is smaller than a given tolerance.

(a) Write a C program to solve the equation x = 3√

x + 2 using the bisectionmethod (choose a suitable f(x) through rearrangement). Start witha = 3 and b = 4, and stop when |f(a+b

2)| < 10−6.

(b) Consider a uniform sphere floating in water. By Archimedes’ principle,the buoyancy force on the sphere is equal to the weight of the displacedwater. When in equilibrium, the weight of the sphere is thus equal tothe weight of displaced water. Let m, V and ρ represent mass, volumeand density, with subscripts of s for the sphere and w for the displacedwater. Define k ≡ ρs

ρw. Then equilibrium requires

ms = mw ⇒ Vsρs = Vwρw ⇒ Vs k = Vw.

Let r be the radius of the sphere, so that Vs = 43πr3. Let d be the depth

of the lowest point on the sphere below the waterline, and assume thatk < 0.5 so that d < r.

i. Derive the following relationship from the above information:

d3 + 4r3k − 3d2r = 0

ii. Write a C function using the bisection method which calculates dgiven values for r and k.

iii. Using your function and taking r = 2, show a table of values of dfor k varying from 0.3 to 0.4 in steps of 0.01.

iv. A ball with radius 2 m is dropped in a large bath of water. Thedepth of the lowest point of the ball is 1.53847 m. From your answerto part iii., what is the value of k, the ratio of densities?

Q2. Use Newton’s method to calculate the two smallest positive solutions of

sin x + ln(cos x) + tan x =6

5,

starting from x0 = 0 and x0 = 7. Comment on the rate of convergence.

Lab Exercise 4

Newton in 3D, Bairstow

Due at 11am on Friday the 4th April

NAME:

Q1. Analysis of a radioactive compound yields the following three equations, wherea is the quantity of substance A, b is the quantity of substance B, and α is adecay rate.

a + b = 10,

a exp(−α) = 3,

b exp(−2α) = 5

(a) Write down the column vector ~F and the Jacobian matrix J for this

problem, where Newton’s method solves J ·~δ~x = −~F (~x) at each iterationand ~x = (a, b, α).

(b) Using the mtxinvert.c routine on the server, write a program that usesNewton’s method to solve the system to seven decimal places.

(c) Confirm your solution is valid by calculating a+b, a exp(−α) and b exp(−2α).

Q2. Write a function which implements Bairstow’s method to find one quadraticfactor of

p(x) = x4 − 10.1x3 − 124x2 + 762.5x− 75,

starting with u = 5 and v = 0. Use ten iterations, and show both the factorand the corresponding bi values.

Bairstow’s Method:Given pn(x) = anx

n+. . .+a0, and a tentative quadratic factor x2−u0x−v0,we determine a sequence {x2 − ukx− vk} of quadratics as follows:

For each k,determine {bi} from bn+2 = bn+1 = 0,

bi = ai + ukbi+1 + vkbi+2, i = n, . . . , 0,and {ci} from cn+2 = cn+1 = 0,

ci = bi + ukci+1 + vkci+2, i = n, . . . , 1.

Then set uk+1 = uk + δ and vk+1 = vk + ε, where

c2δ + c3ε = −b1 and c1δ + c2ε = −b0.

Lab Exercise 5

Newton in 3D, Bairstow

Due at 11am on Friday the 11th April

NAME:

MARKING : The question that you answer best will be marked out of 8, and the other outof 2. Completing only one question gives you a maximum possible mark of 8 out of 10.

Q1. Bairstow’s method.

(a) Write a function which takes (only) the following parameters:

• An array representing the coefficients of pn(x) = anxn+. . .+a1x+a0.

• An integer representing the order of the polynomial.

Your function should then use Bairstow’s method to find and output tothe screen quadratic factors of the polynomial, until the deflated poly-nomial is quadratic or linear. Also output the final deflated polynomial.Always start with u = 0 and v = 0. Hint: Use ‘bairstow_step’.

(b) Using your answer to part (a), write a function which takes the same ar-ray and integer, and prints to the screen all of the roots of the polynomial(whether complex or real).

(c) Test both functions on the following polynomial:

p7(x) = 612− 2401 x + 2418 x2 − 912 x3 + 152 x4 − 10 x5 − 4 x6 + x7

Q2. Recall Newton’s method from Exercise 4, where you manually entered theJacobian matrix that was calculated by hand. A more convenient methodnumerically approximates the Jacobian with finite differences:

∂Fi(~x)

∂xj

≈ Fi(~x + ~δj)− Fi(~x)

δ

where δ is some small number (say 10−8), and the elements of ~δj are all zero,except for the jth element which is equal to δ.

(a) Solve the problem of Q1 in Exercise 4 using this approximation of the

Jacobian (so that only ~F and a starting ~x are specified).

(b) Using the approximate Jacobian method, starting at ~x0 = ~0, solve thefollowing nonlinear problem.

2 x1 − x2 − 0.4 x23 + 0.2 x3

4 + 3 x6 = 19.6

− x21 − 4 x1 + 3.5 x2 − x4 = −5

x1 + x1 x2 + x3 + 0.2 x35 = 27

x31 − x2 x3 + x4 − x7 = −6

0.3 x22 + x5 = 5

0.1 x1 x2 + x23 + x6 = 7

0.5 x31 − 0.04( x3

2) x3 + x24 + x7 = 7.5

Lab Exercise 6

Divided Differences

Due at 11am on Friday the 18th April

NAME:

The program divdiff.c on the server outputs a divided difference table for thepoints (−1,−3), (1, 0), (2, 4), (0, 1). The maketable function creates the divideddifferences table, where the matrix dd has the following format for N data pointsspecified as x0, x1, . . . , xN−1:

dd =

f0 f0 1 f0 1 2 . . . f0...N−2 f0...N−1

f1 f1 2 . . . f1...N−2 f1...N−1

f2 . . . f2...N−2 f2...N−1...

...fN−2 fN−2 N−1

fN−1

i.e. dd[i][j] = fi...j, where 0 ≤ i, j ≤ N − 1. By modifying the program divdiff.c,write programs which answer the following questions.

Q1. Recall that the interpolating polynomial through (x0, f0), . . . , (xN−1, fN−1) is

pn(x) = f0 + (x− x0)f0 1 + . . . + (x− x0) . . . (x− xN−2)f0...N−1.

(a) Find the cubic which passes through (−1,−3), (1, 0), (2, 4), and (0, 1).

(b) Find the cubic which matches the function y = x1/4 at x = 0, 1, 16, 81.

Q2. Output a table of interpolated y values for x = {1, 2, 3, . . . , 9}, based on aquartic interpolating polynomial passing through the following data points:

x 0 1.2 4 5.8 10y 0 7 3 1 8

Q3. Let f(x) = xπ, and consider four samples of f at x0 = 2, x1 = 2+h, x2 = 2+2hand x3 = 2 + 3h.

(a) Output the first row of the divided difference table for h = 1.0, 0.1, 0.01, 0.001,and 0.0001.

(b) Do the values of f0...j approach 1j!f (j)(x0)?

(c) What happens when h =1.0e-10?

#include.<stdio.h>............../∗.COMPILE.WITH.G++..(.not.gcc.).∗/const.int.N.=.4;................/∗.The.number.of.data.points.to.use....∗//∗.Creates.a.divided.differences.table,.where..dd[i][j].=.f {x i...x j}.∗/void.maketable(double.x[N],.double.y[N],.double.dd[N][N]){....int.i,.j;

....for.(i.=.0;.i.<.N;.i++)

........dd[i][i].=.y[i];

....for.(j.=.0;.j.<.N;.j++)

........for.(i.=.j.−.1;.i.>=.0;.i−−)

............/∗.f[xi,..,xj].=.(.f[xi+1,..,xj].−.f[xi,..,xj−1].)/(xj−xi).∗/

............dd[i][j].=.(dd[i.+.1][j].−.dd[i][j.−.1])./.(x[j].−.x[i]);}void.printtable(double.x[N],.double.dd[N][N]){.............................../∗.print.out.the.divided.difference.table.∗/....int.i,.j;....printf("\n.%7s.|..%7s.\n−−−−−−−−−+",."x i.",."f[x i]");....printf("−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−");....for.(i.=.0;.i.<.N;.i++).{........printf("\n.%7.4f.|.",.x[i]);........for.(j.=.i;.j.<.N;.j++)............printf(".%7.4f.",.dd[i][j]);....}....printf("\n\n");}int.main(void){....double.x[N].=.{.−1,.0,.1,.2.};....double.y[N].=.{.−3,.1,.0,.4.};....double.dd[N][N];

....maketable(x,.y,.dd);

....printtable(x,.dd);

....return.0;}

[j@aythya code]$ g++ -o divdiff -Wall divdiff.c

[j@aythya code]$ ./divdiff

x_i | f[x_i]

---------+---------------------------------------

-1.0000 | -3.0000 1.5000 0.8333 1.6667

1.0000 | 0.0000 4.0000 2.5000

2.0000 | 4.0000 1.5000

0.0000 | 1.0000

Lab Exercise 6 (II)

Quadrature

Due at 11am on Friday the 2nd May

NAME:

You will see in lecture that the composite trapezoid rule is given by

∫ b

af(x) dx ≈ T (h) = h

(f0

2+ f1 + f2 + f3 + . . . + fN−1 +

fN

2

),

where x0 = a and xN = b and the nodes are evenly spaced.

Q1. Write a C function which, given values for a, b, N , and a vector ~f = (f0, . . . , fN),evaluates the above expression and returns the result.

Q2. Test your function with an integral of your choice, checking the answer byhand.

Q3. Consider the polynomial

p(x) = 0.9x3 − 3.7x2 + 17.4x + 99.1, where∫ 1

0p(x)dx =

2563

24.

(a) For the above integral, write a program which outputs a table of T (h)values corresponding to each N ∈ {10, 20, . . . , 100}. Also output theerror in each T (h).

(b) Propose a (proportional) relationship between the error and N .

(c) Give a relationship between the error and h.

Lab Exercise 7

Quadrature II

Due at 11am on Friday the 9th May

NAME:

Recall from exercise 6 (II) that the composite trapezoid rule is given by

∫ b

af(x) dx ≈ TN = h

(f0

2+ f1 + f2 + f3 + . . . + fN−1 +

fN

2

),

where the nodes are evenly spaced, fi = f(a + h i) and h = b−aN

.

Q1. (a) Assuming that N is even, write down an expression for TN/2 in terms ofthe h and fi values that are used for TN .

(b) Briefly outline a method of efficiently calculating TN assuming that TN/2

has already been calculated.

Q2. On the previous exercise you wrote a trapezoid routine which assumed thatthe integrand was known only at a fixed set of x values. Suppose now thatyou are given a numerical or analytical method for calculating the inte-grand f(x), and that you have already written a corresponding C functiondouble f(double x). This allows writing a trapezoid routine which refinesthe number of nodes until a desired accuracy is reached.

Write an efficient trapezoid routine which does the following:

• Starts with N = 1, then repeatedly doubles N until

∣∣∣TN − TN/2

∣∣∣|TN | < ε,

where ε is passed to your trapezoid function as a parameter.

• Returns the final TN (the value of the integral).

Hint: Arrays are not needed!

Q3. Test your function by numerically evaluating the following integral, usingε = 1.0e− 5. ∫ b

af(x)dx =

∫ 5

0

1√2π

exp

(−x2

2

)dx

Q4. Would it have been better to specify a minimum value for N in addition tothe terminating condition above? Give an example to illustrate your answer.

Lab Exercise 8

Derivatives

Due at 11am on Friday the 16th May

NAME:

Recall that the Newton interpolating polynomial through the evenly spaced points(x0, y0) . . . (xn, yn), where x = x0 + sh, is

pn(x) = y0 +

(s1

)∆y0 +

(s2

)∆2y0 +

(s3

)∆3y0 + . . . +

(sn

)∆ny0.

Suppose that we have a set of (xi, yi) values representing an unknown function f(x),and that we wish to approximate the derivative of f at some value x = x∗. We cando so by fitting the Newton interpolating polynomial to a few of the (xi, yi) pointsnear to x∗, then differentiating the polynomial at x∗. We might hope that usingmore data points when calculating the polynomial would improve the accuracy ofthe approximation to f ′(x∗).

Q1. Suppose that x∗ lies on one of the nodes (so that x∗ = xi for some i). Im-plement this method for one, three and five points (using a separate functionfor each) with x∗ as the middle point. For example, your five-point functionmight have declaration:

double dfdx_fivepoint( double h, double y[5] );

where x∗ is at x[2].

Q2. Test your three functions by approximating f ′(1) where f(x) = sin(x). Useh = 0.1. The true value is f ′(1) = 0.54030230586813971740....

Q3. By looking at h = 0.1, 0.01, 0.001, and 0.0001, deduce a proportional relation-ship between the error and h for the one and three point rules.

Q4. What happens if you try to deduce a similar relationship for the five pointrule? Explain the observed behaviour. (Note that the five point rule actuallyhas error proportional to h4.)

Note: See ‘ex8help.nb’ on the server for derivation of the appropriate mid-point rules.

Lab Exercise 9

Derivatives II

Due at 11am on Friday the 23rd May

NAME:

Before attempting this assignment please read the handout page that discussesthe math361 library.

Q1. (a) Derive the differentiation formula for f ′(x0) based on interpolation atx0, x1, and x2. Assume that the nodes are evenly spaced.

(b) Write a program which graphs the error of your formula over the domain10−6 ≤ h ≤ 10−4. Use the function f(x) = x3/16 and approximate thederivative at x0 = 5.0.

(c) From your graph, state the approximate optimal h value to use in thiscase. (This value is known as hopt.)

(d) Analysis of error for the formula in (a) indicates that with fi valuesaccurate to 15 decimal places hopt = 2.5 · 10−5, and with 16 accuratedecimal places hopt = 1.2 · 10−5.

i. Assuming that the analysis is good (i.e. that the bounds used aretight), roughly what decimal place accuracy does the computer ap-pear to have?

ii. How many significant figures does this correspond to?

Q2. A bacteria colony lives near a volcanic vent in a cold climate. The growthrate of the colony is equal to twice the current colony size plus the squareof the environmental temperature (in Celsius). At the start of a period ofincreased volcanic activity the temperature is 0oC. The rate of change of thetemperature is measured to be 1

2√

twhere t is the time in hours since the start

of the activity.

(a) Give a simple differential equation which governs the bacterial growth,and involves the colony size and temperature.

(b) Consider an initial colony size of 100. Write a C program which:

• Uses Euler’s method to solve the differential equation from t = 0through to t = 1 hour.

• Graphs the approximated solutions for step sizes of 0.25, 0.1 and0.01 hours, together with the true solution:

−1

4− t

2+

401

4e2t.

Lab Exercise 10

Runge-Kutta

Due at 11am on Friday the 30th May

NAME:

While there are many general techniques for analytically solving classes of ODEs,the only practical solution technique for complicated equations is to use numericalmethods. The most popular of these is the Runge-Kutta method.

Q1. Consider the initial value problem y′ = −y

x, y(1) = 1. Use the 4th order

Runge-Kutta method to answer the following.

(a) Estimate y(2) using h = 14.

(b) Estimate y(5). How many steps are needed for 10 figure accuracy in thesolution?

Q2. Based on the Haissinki equation2, the bunch shape of a high current beamproduced at an accelerator test facility can be shown to be governed by a firstorder non-linear differential equation:

y′ = −yx + ry

1 + ly.

Here r is the dimensionless resistance, l the dimensionless inductance and x isthe longitudinal position in the beam (x < 0 is toward the front of the bunch).

(a) Using the fourth order Runge-Kutta method, solve this differential equa-tion over x ∈ [−6, 6] for r = 5, l = 3, and taking y0 = 3.6 · 10−5.

(b) Plot the solution curve for each value of r ∈ {0, 2, 5, 8}, showing allcurves on a single graph and keeping r + l = 8. Take y0 = 10−6 · (r +1)2.

Runge-Kutta method:Given the initial value problem y′(x) = f(x, y), where y0 is known,the Runge-Kutta method allows calculation of yn+1 in terms of fevaluated at xn, xn + 1

2h and xn+1. The fourth order method can

be expressed as:

yn+1 = yn +h

6(k1 + 2k2 + 2k3 + k4)

where

k1 = f(xn, yn)

k2 = f(xn + h/2, yn + k1h/2)

k3 = f(xn + h/2, yn + k2h/2)

k4 = f(xn + h, yn + k3h)

2See “Impedance Analysis of Bunch Length Measurements at the ATF Damping Ring” byK.L.F. Bane et al., 30th May 2001 (readily found on the internet).

Lab Exercise 11

Boundary Value Problems

Due at 11am on Friday the 6th June

NAME:

NOTE: This assignment does not count for your internal assessment, but covers examinable material.

Consider the following non-linear boundary value problem, whose differential equa-tion represents the derivative of the Bernoulli equation.

y′′(x) = y(x)2 + y′(x) + 2 x y(x) y′(x), y(0) = 1.7012, y(0.5) = 4.0.

Q1. Using y′n ≈ 12h

(yn+1 − yn−1) and y′′n ≈ 1h2 (yn+1 − 2yn + yn−1), show that

yn+1 =yn−1(2 + h + 2hxnyn)− 2yn(2 + h2yn)

h + 2hxnyn − 2.

Q2. In order to use the above relationship we need to choose a value γ for y1 (sothat y2 can then be calculated using both y0 = 1.7012 and y1 = γ). Ideallywe wish to choose γ so that we’ll end up with y(0.5) = 4.0. However, untilwe solve the IVP for a particular γ, we don’t know what y(0.5) will be!

(a) Write a C program which:

• Inputs a value for γ from the user.

• Solves the IVP using 100 steps over [0, 0.5].

• Graphs the solution.

• Prints the value of y(0.5).

(b) Find a value of γ which gives you y(0.5) = 4.0 (to 3 s.f.).

Q3. The Bernoulli equation is given by y′(x)−xy(x)2− y(x) = 0, and has generalsolution

y(x) =−ex

−ex + x ex − C.

It turns out that the solution you have found with the shooting method is alsoa solution to the Bournoulli equation. Which value of C does your solutioncorrespond to?

#include <stdio.h>

#include <stdlib.h>

int main(void) {

double gamma;

char s[20];

printf("Enter a number: "); /* print message to screen */

fflush(stdout); /* ensure message is displayed */

fgets(s,19,stdin); /* get a string from user */

gamma = atof(s); /* convert string to double */

printf("Gamma = %f.\n",gamma);

}